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)
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=self._site[FACILITY]['enabled'])
44 def POST(self, *args, **kwargs):
45 message = "Nothing was modified."
47 valid = self._site[FACILITY]['enabled']
50 order = kwargs['order'].split(',')
57 error = "Invalid plugin name: %s" % val
58 raise ValueError(error)
59 new_values.append(val)
60 if len(new_values) < len(valid):
62 if val not in new_values:
63 new_values.append(val)
68 globalconf['order'] = ','.join(new_values)
69 po.set_config(globalconf)
70 po.save_plugin_config(FACILITY)
72 # When all is saved update also live config
73 self._site[FACILITY]['enabled'] = new_values
75 message = "New configuration saved."
76 message_type = "success"
80 message_type = "error"
82 except Exception, e: # pylint: disable=broad-except
83 message = "Failed to save data!"
84 message_type = "error"
86 return self._template('admin/login_order.html',
88 message_type=message_type,
89 title='login plugins order',
90 name='admin_login_order_form',
91 menu=self.menu, action=self.url,
92 options=self._site[FACILITY]['enabled'])
94 def root(self, *args, **kwargs):
95 cherrypy.log.error("method: %s" % cherrypy.request.method)
96 op = getattr(self, cherrypy.request.method, self.GET)
98 return op(*args, **kwargs)
101 class LoginPlugins(Page):
102 def __init__(self, site, parent):
103 super(LoginPlugins, self).__init__(site)
104 self._master = parent
105 self.title = 'Login Plugins'
106 self.url = '%s/login' % parent.url
107 self.facility = FACILITY
108 parent.add_subtree('login', self)
110 for plugin in self._site[FACILITY]['available']:
111 cherrypy.log.error('Admin login plugin: %s' % plugin)
112 obj = self._site[FACILITY]['available'][plugin]
113 self.__dict__[plugin] = AdminPluginPage(obj, self._site, self)
115 self.order = LoginPluginsOrder(self._site, self)
117 def root_with_msg(self, message=None, message_type=None):
118 login_plugins = self._site[FACILITY]
120 for p in login_plugins['enabled']:
121 ordered.append(p.name)
122 return self._template('admin/login.html', title=self.title,
124 message_type=message_type,
125 available=login_plugins['available'],
127 menu=self._master.menu)
129 def root(self, *args, **kwargs):
130 return self.root_with_msg()
132 def enable(self, plugin):
134 plugins = self._site[FACILITY]
135 if plugin not in plugins['available']:
136 msg = "Unknown plugin %s" % plugin
137 return self.root_with_msg(msg, "error")
138 obj = plugins['available'][plugin]
139 if obj not in plugins['enabled']:
140 obj.enable(self._site)
141 msg = "Plugin %s enabled" % obj.name
142 return self.root_with_msg(msg, "success")
143 enable.exposed = True
145 def disable(self, plugin):
147 plugins = self._site[FACILITY]
148 if plugin not in plugins['available']:
149 msg = "Unknown plugin %s" % plugin
150 return self.root_with_msg(msg, "error")
151 obj = plugins['available'][plugin]
152 if obj in plugins['enabled']:
153 obj.disable(self._site)
154 msg = "Plugin %s disabled" % obj.name
155 return self.root_with_msg(msg, "success")
156 disable.exposed = True