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/>.
21 from ipsilon.util.page import Page
22 from ipsilon.util.page import admin_protect
23 from ipsilon.util.plugin import PluginObject
24 from ipsilon.admin.common import AdminPluginPage
25 from ipsilon.login.common import FACILITY
28 def save_enabled_plugins(names):
32 globalconf['order'] = ','.join(names)
33 po.set_config(globalconf)
34 po.save_plugin_config(FACILITY)
37 class LoginPluginsOrder(Page):
39 def __init__(self, site, parent):
40 super(LoginPluginsOrder, self).__init__(site, form=True)
41 self.url = '%s/order' % parent.url
44 def _reorder_plugins(self, order):
45 plugins = self._site[FACILITY]['available']
46 root = self._site[FACILITY]['root']
50 root.first_login = plugins[name]
52 prev_obj.next_login = plugins[name]
53 prev_obj = plugins[name]
54 prev_obj.next_login = None
57 def GET(self, *args, **kwargs):
58 opts = [p.name for p in self._site[FACILITY]['enabled']]
59 return self._template('admin/login_order.html',
60 title='login plugins order',
61 name='admin_login_order_form',
62 menu=self.menu, action=self.url,
66 def POST(self, *args, **kwargs):
67 message = "Nothing was modified."
69 plugins_by_name = {p.name: p for p in self._site[FACILITY]['enabled']}
72 order = kwargs['order'].split(',')
79 if val not in plugins_by_name:
80 error = "Invalid plugin name: %s" % val
81 raise ValueError(error)
83 new_plugins.append(plugins_by_name[val])
84 if len(new_names) < len(plugins_by_name):
85 for val in plugins_by_name:
86 if val not in new_names:
88 new_plugins.append(plugins_by_name[val])
90 save_enabled_plugins(new_names)
91 self._reorder_plugins(new_names)
93 # When all is saved update also live config. The
94 # live config is a list of the actual plugin
96 self._site[FACILITY]['enabled'] = new_plugins
98 message = "New configuration saved."
99 message_type = "success"
101 except ValueError, e:
103 message_type = "error"
105 except Exception, e: # pylint: disable=broad-except
106 message = "Failed to save data!"
107 message_type = "error"
109 opts = [p.name for p in self._site[FACILITY]['enabled']]
110 return self._template('admin/login_order.html',
112 message_type=message_type,
113 title='login plugins order',
114 name='admin_login_order_form',
115 menu=self.menu, action=self.url,
119 class LoginPlugins(Page):
120 def __init__(self, site, parent):
121 super(LoginPlugins, self).__init__(site)
122 self._master = parent
123 self.title = 'Login Plugins'
124 self.url = '%s/login' % parent.url
125 self.facility = FACILITY
126 parent.add_subtree('login', self)
128 for plugin in self._site[FACILITY]['available']:
129 cherrypy.log.error('Admin login plugin: %s' % plugin)
130 obj = self._site[FACILITY]['available'][plugin]
131 self.__dict__[plugin] = AdminPluginPage(obj, self._site, self)
133 self.order = LoginPluginsOrder(self._site, self)
135 def root_with_msg(self, message=None, message_type=None):
136 login_plugins = self._site[FACILITY]
138 for p in login_plugins['enabled']:
139 ordered.append(p.name)
140 return self._template('admin/login.html', title=self.title,
142 message_type=message_type,
143 available=login_plugins['available'],
145 menu=self._master.menu)
147 def root(self, *args, **kwargs):
148 return self.root_with_msg()
150 def enable(self, plugin):
152 plugins = self._site[FACILITY]
153 if plugin not in plugins['available']:
154 msg = "Unknown plugin %s" % plugin
155 return self.root_with_msg(msg, "error")
156 obj = plugins['available'][plugin]
157 if obj not in plugins['enabled']:
158 obj.enable(self._site)
159 save_enabled_plugins(list(x.name for x in plugins['enabled']))
160 msg = "Plugin %s enabled" % obj.name
161 return self.root_with_msg(msg, "success")
162 enable.exposed = True
164 def disable(self, plugin):
166 plugins = self._site[FACILITY]
167 if plugin not in plugins['available']:
168 msg = "Unknown plugin %s" % plugin
169 return self.root_with_msg(msg, "error")
170 obj = plugins['available'][plugin]
171 if obj in plugins['enabled']:
172 obj.disable(self._site)
173 save_enabled_plugins(list(x.name for x in plugins['enabled']))
174 msg = "Plugin %s disabled" % obj.name
175 return self.root_with_msg(msg, "success")
176 disable.exposed = True