6e36669d5c602bb94e2a1bf493a04b5281008a18
[cascardo/ipsilon.git] / ipsilon / admin / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from ipsilon.util.data import Store
21 from ipsilon.util.page import Page
22 from ipsilon.util.user import UserSession
23 import cherrypy
24 from ipsilon.login.common import FACILITY as LOGIN_FACILITY
25
26
27 def admin_protect(fn):
28
29     def check(*args, **kwargs):
30         if UserSession().get_user().is_admin:
31             return fn(*args, **kwargs)
32
33         raise cherrypy.HTTPError(403)
34
35     return check
36
37
38 class LoginPluginPage(Page):
39
40     def __init__(self, obj, site, baseurl):
41         super(LoginPluginPage, self).__init__(site)
42         self._obj = obj
43         self.url = '%s/%s' % (baseurl, obj.name)
44
45         # Get the defaults
46         self.plugin_config = obj.get_config_desc()
47         if not self.plugin_config:
48             self.plugin_config = []
49
50         # Now overlay the actual config
51         for option in self.plugin_config:
52             self.plugin_config[option][2] = obj.get_config_value(option)
53
54     @admin_protect
55     def GET(self, *args, **kwargs):
56         return self._template('admin/login_plugin.html',
57                               title='%s plugin' % self._obj.name,
58                               name='admin_login_%s_form' % self._obj.name,
59                               action=self.url,
60                               options=self.plugin_config)
61
62     @admin_protect
63     def POST(self, *args, **kwargs):
64
65         message = "Nothing was modified."
66         message_type = "info"
67         new_values = dict()
68
69         for key, value in kwargs.iteritems():
70             if key in self.plugin_config:
71                 if value != self.plugin_config[key][2]:
72                     cherrypy.log.error("Storing [%s]: %s = %s" %
73                                        (self._obj.name, key, value))
74                     new_values[key] = value
75
76         if len(new_values) != 0:
77             # First we try to save in the database
78             try:
79                 store = Store()
80                 store.save_plugin_config(LOGIN_FACILITY,
81                                          self._obj.name, new_values)
82                 message = "New configuration saved."
83                 message_type = "success"
84             except Exception:  # pylint: disable=broad-except
85                 message = "Failed to save data!"
86                 message_type = "error"
87
88             # And only if it succeeds we change the live object
89             for name, value in new_values.items():
90                 self._obj.set_config_value(name, value)
91                 self.plugin_config[name][2] = value
92
93         return self._template('admin/login_plugin.html',
94                               message=message,
95                               message_type=message_type,
96                               title='%s plugin' % self._obj.name,
97                               name='admin_login_%s_form' % self._obj.name,
98                               action=self.url,
99                               options=self.plugin_config)
100
101     def root(self, *args, **kwargs):
102         cherrypy.log.error("method: %s" % cherrypy.request.method)
103         op = getattr(self, cherrypy.request.method, self.GET)
104         if callable(op):
105             return op(*args, **kwargs)
106
107
108 class LoginPlugins(Page):
109     def __init__(self, site, baseurl):
110         super(LoginPlugins, self).__init__(site)
111         self.url = '%s/login' % baseurl
112
113         for plugin in self._site[LOGIN_FACILITY]['available']:
114             cherrypy.log.error('Admin login plugin: %s' % plugin)
115             obj = self._site[LOGIN_FACILITY]['available'][plugin]
116             self.__dict__[plugin] = LoginPluginPage(obj, self._site, self.url)
117
118
119 class Admin(Page):
120
121     def __init__(self, *args, **kwargs):
122         super(Admin, self).__init__(*args, **kwargs)
123         self.url = '%s/admin' % self.basepath
124         self.login = LoginPlugins(self._site, self.url)
125
126     def root(self, *args, **kwargs):
127         login_plugins = self._site[LOGIN_FACILITY]
128         return self._template('admin/index.html', title='Administration',
129                               available=login_plugins['available'],
130                               enabled=login_plugins['enabled'])