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."
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
76 if len(new_values) != 0:
77 # First we try to save in the database
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"
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
93 return self._template('admin/login_plugin.html',
95 message_type=message_type,
96 title='%s plugin' % self._obj.name,
97 name='admin_login_%s_form' % self._obj.name,
99 options=self.plugin_config)
101 def root(self, *args, **kwargs):
102 cherrypy.log.error("method: %s" % cherrypy.request.method)
103 op = getattr(self, cherrypy.request.method, self.GET)
105 return op(*args, **kwargs)
108 class LoginPlugins(Page):
109 def __init__(self, site, baseurl):
110 super(LoginPlugins, self).__init__(site)
111 self.url = '%s/login' % baseurl
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)
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)
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'])