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 opts = [p.name for p in self._site[FACILITY]['enabled']]
38 return self._template('admin/login_order.html',
39 title='login plugins order',
40 name='admin_login_order_form',
41 menu=self.menu, action=self.url,
45 def POST(self, *args, **kwargs):
46 message = "Nothing was modified."
48 plugins_by_name = {p.name: p for p in self._site[FACILITY]['enabled']}
51 order = kwargs['order'].split(',')
58 if val not in plugins_by_name:
59 error = "Invalid plugin name: %s" % val
60 raise ValueError(error)
62 new_plugins.append(plugins_by_name[val])
63 if len(new_names) < len(plugins_by_name):
64 for val in plugins_by_name:
65 if val not in new_names:
67 new_plugins.append(plugins_by_name[val])
72 globalconf['order'] = ','.join(new_names)
73 po.set_config(globalconf)
74 po.save_plugin_config(FACILITY)
76 # When all is saved update also live config. The
77 # live config is a list of the actual plugin
79 self._site[FACILITY]['enabled'] = new_plugins
81 message = "New configuration saved."
82 message_type = "success"
86 message_type = "error"
88 except Exception, e: # pylint: disable=broad-except
89 message = "Failed to save data!"
90 message_type = "error"
92 opts = [p.name for p in self._site[FACILITY]['enabled']]
93 return self._template('admin/login_order.html',
95 message_type=message_type,
96 title='login plugins order',
97 name='admin_login_order_form',
98 menu=self.menu, action=self.url,
102 class LoginPlugins(Page):
103 def __init__(self, site, parent):
104 super(LoginPlugins, self).__init__(site)
105 self._master = parent
106 self.title = 'Login Plugins'
107 self.url = '%s/login' % parent.url
108 self.facility = FACILITY
109 parent.add_subtree('login', self)
111 for plugin in self._site[FACILITY]['available']:
112 cherrypy.log.error('Admin login plugin: %s' % plugin)
113 obj = self._site[FACILITY]['available'][plugin]
114 self.__dict__[plugin] = AdminPluginPage(obj, self._site, self)
116 self.order = LoginPluginsOrder(self._site, self)
118 def root_with_msg(self, message=None, message_type=None):
119 login_plugins = self._site[FACILITY]
121 for p in login_plugins['enabled']:
122 ordered.append(p.name)
123 return self._template('admin/login.html', title=self.title,
125 message_type=message_type,
126 available=login_plugins['available'],
128 menu=self._master.menu)
130 def root(self, *args, **kwargs):
131 return self.root_with_msg()
133 def enable(self, plugin):
135 plugins = self._site[FACILITY]
136 if plugin not in plugins['available']:
137 msg = "Unknown plugin %s" % plugin
138 return self.root_with_msg(msg, "error")
139 obj = plugins['available'][plugin]
140 if obj not in plugins['enabled']:
141 obj.enable(self._site)
142 msg = "Plugin %s enabled" % obj.name
143 return self.root_with_msg(msg, "success")
144 enable.exposed = True
146 def disable(self, plugin):
148 plugins = self._site[FACILITY]
149 if plugin not in plugins['available']:
150 msg = "Unknown plugin %s" % plugin
151 return self.root_with_msg(msg, "error")
152 obj = plugins['available'][plugin]
153 if obj in plugins['enabled']:
154 obj.disable(self._site)
155 msg = "Plugin %s disabled" % obj.name
156 return self.root_with_msg(msg, "success")
157 disable.exposed = True