Add infrastructure to configure server
[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         new_values = dict()
67
68         for key, value in kwargs.iteritems():
69             if key in self.plugin_config:
70                 if value != self.plugin_config[key][2]:
71                     cherrypy.log.error("Storing [%s]: %s = %s" %
72                                        (self._obj.name, key, value))
73                     new_values[key] = value
74
75         if len(new_values) != 0:
76             # First we try to save in the database
77             try:
78                 store = Store()
79                 store.save_plugin_config(LOGIN_FACILITY,
80                                          self._obj.name, new_values)
81                 message = "New configuration saved."
82             except Exception:  # pylint: disable=broad-except
83                 message = "Failed to save data!"
84
85             # And only if it succeeds we change the live object
86             for name, value in new_values.items():
87                 self._obj.set_config_value(name, value)
88                 self.plugin_config[name][2] = value
89
90         return self._template('admin/login_plugin.html',
91                               message=message,
92                               title='%s plugin' % self._obj.name,
93                               name='admin_login_%s_form' % self._obj.name,
94                               action=self.url,
95                               options=self.plugin_config)
96
97     def root(self, *args, **kwargs):
98         cherrypy.log.error("method: %s" % cherrypy.request.method)
99         op = getattr(self, cherrypy.request.method, self.GET)
100         if callable(op):
101             return op(*args, **kwargs)
102
103
104 class LoginPlugins(Page):
105     def __init__(self, site, baseurl):
106         super(LoginPlugins, self).__init__(site)
107         self.url = '%s/login' % baseurl
108
109         for plugin in self._site[LOGIN_FACILITY]['available']:
110             cherrypy.log.error('Admin login plugin: %s' % plugin)
111             obj = self._site[LOGIN_FACILITY]['available'][plugin]
112             self.__dict__[plugin] = LoginPluginPage(obj, self._site, self.url)
113
114
115 class Admin(Page):
116
117     def __init__(self, *args, **kwargs):
118         super(Admin, self).__init__(*args, **kwargs)
119         self.url = '%s/admin' % self.basepath
120         self.login = LoginPlugins(self._site, self.url)
121
122     def root(self, *args, **kwargs):
123         login_plugins = self._site[LOGIN_FACILITY]
124         return self._template('admin/index.html', title='Administration',
125                               available=login_plugins['available'],
126                               enabled=login_plugins['enabled'])