3 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
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.
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.
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/>.
20 from ipsilon.util.data import Store
21 from ipsilon.util.page import Page
22 from ipsilon.util.user import UserSession
24 from ipsilon.login.common import FACILITY as LOGIN_FACILITY
27 def admin_protect(fn):
29 def check(*args, **kwargs):
30 if UserSession().get_user().is_admin:
31 return fn(*args, **kwargs)
33 raise cherrypy.HTTPError(403)
38 class LoginPluginPage(Page):
40 def __init__(self, obj, site, baseurl):
41 super(LoginPluginPage, self).__init__(site)
43 self.url = '%s/%s' % (baseurl, obj.name)
46 self.plugin_config = obj.get_config_desc()
47 if not self.plugin_config:
48 self.plugin_config = []
50 # Now overlay the actual config
51 for option in self.plugin_config:
52 self.plugin_config[option][2] = obj.get_config_value(option)
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,
60 options=self.plugin_config)
63 def POST(self, *args, **kwargs):
65 message = "Nothing was modified."
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
75 if len(new_values) != 0:
76 # First we try to save in the database
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!"
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
90 return self._template('admin/login_plugin.html',
92 title='%s plugin' % self._obj.name,
93 name='admin_login_%s_form' % self._obj.name,
95 options=self.plugin_config)
97 def root(self, *args, **kwargs):
98 cherrypy.log.error("method: %s" % cherrypy.request.method)
99 op = getattr(self, cherrypy.request.method, self.GET)
101 return op(*args, **kwargs)
104 class LoginPlugins(Page):
105 def __init__(self, site, baseurl):
106 super(LoginPlugins, self).__init__(site)
107 self.url = '%s/login' % baseurl
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)
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)
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'])