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 class LoginPluginsOrder(Page):
30 def __init__(self, site, parent):
31 super(LoginPluginsOrder, self).__init__(site, form=True)
32 self.url = '%s/order' % parent.url
36 def GET(self, *args, **kwargs):
37 return self._template('admin/login_order.html',
38 title='login plugins order',
39 name='admin_login_order_form',
40 menu=self.menu, action=self.url,
41 options=[p.name for p in self._site[FACILITY]['enabled']])
44 def POST(self, *args, **kwargs):
45 message = "Nothing was modified."
47 plugins_by_name = {p.name: p for p in self._site[FACILITY]['enabled']}
50 order = kwargs['order'].split(',')
57 if val not in plugins_by_name:
58 error = "Invalid plugin name: %s" % val
59 raise ValueError(error)
61 new_plugins.append(plugins_by_name[val])
62 if len(new_names) < len(plugins_by_name):
63 for val in plugins_by_name:
64 if val not in new_names:
66 new_plugins.append(plugins_by_name[val])
71 globalconf['order'] = ','.join(new_names)
72 po.set_config(globalconf)
73 po.save_plugin_config(FACILITY)
75 # When all is saved update also live config. The
76 # live config is a list of the actual plugin
78 self._site[FACILITY]['enabled'] = new_plugins
80 message = "New configuration saved."
81 message_type = "success"
85 message_type = "error"
87 except Exception, e: # pylint: disable=broad-except
88 message = "Failed to save data!"
89 message_type = "error"
91 return self._template('admin/login_order.html',
93 message_type=message_type,
94 title='login plugins order',
95 name='admin_login_order_form',
96 menu=self.menu, action=self.url,
97 options=[p.name for p in self._site[FACILITY]['enabled']])
100 class LoginPlugins(Page):
101 def __init__(self, site, parent):
102 super(LoginPlugins, self).__init__(site)
103 self._master = parent
104 self.title = 'Login Plugins'
105 self.url = '%s/login' % parent.url
106 self.facility = FACILITY
107 parent.add_subtree('login', self)
109 for plugin in self._site[FACILITY]['available']:
110 cherrypy.log.error('Admin login plugin: %s' % plugin)
111 obj = self._site[FACILITY]['available'][plugin]
112 self.__dict__[plugin] = AdminPluginPage(obj, self._site, self)
114 self.order = LoginPluginsOrder(self._site, self)
116 def root_with_msg(self, message=None, message_type=None):
117 login_plugins = self._site[FACILITY]
119 for p in login_plugins['enabled']:
120 ordered.append(p.name)
121 return self._template('admin/login.html', title=self.title,
123 message_type=message_type,
124 available=login_plugins['available'],
126 menu=self._master.menu)
128 def root(self, *args, **kwargs):
129 return self.root_with_msg()
131 def enable(self, plugin):
133 plugins = self._site[FACILITY]
134 if plugin not in plugins['available']:
135 msg = "Unknown plugin %s" % plugin
136 return self.root_with_msg(msg, "error")
137 obj = plugins['available'][plugin]
138 if obj not in plugins['enabled']:
139 obj.enable(self._site)
140 msg = "Plugin %s enabled" % obj.name
141 return self.root_with_msg(msg, "success")
142 enable.exposed = True
144 def disable(self, plugin):
146 plugins = self._site[FACILITY]
147 if plugin not in plugins['available']:
148 msg = "Unknown plugin %s" % plugin
149 return self.root_with_msg(msg, "error")
150 obj = plugins['available'][plugin]
151 if obj in plugins['enabled']:
152 obj.disable(self._site)
153 msg = "Plugin %s disabled" % obj.name
154 return self.root_with_msg(msg, "success")
155 disable.exposed = True