5b6a1ff63644bec19741e2e39f9222ed98ab776d
[cascardo/ipsilon.git] / ipsilon / admin / login.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
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.
11 #
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.
16 #
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/>.
19
20 import cherrypy
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
26
27
28 class LoginPluginsOrder(Page):
29
30     def __init__(self, site, parent):
31         super(LoginPluginsOrder, self).__init__(site, form=True)
32         self.url = '%s/order' % parent.url
33         self.menu = [parent]
34
35     @admin_protect
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']])
42
43     @admin_protect
44     def POST(self, *args, **kwargs):
45         message = "Nothing was modified."
46         message_type = "info"
47         plugins_by_name = {p.name: p for p in self._site[FACILITY]['enabled']}
48
49         if 'order' in kwargs:
50             order = kwargs['order'].split(',')
51             if len(order) != 0:
52                 new_names = []
53                 new_plugins = []
54                 try:
55                     for v in order:
56                         val = v.strip()
57                         if val not in plugins_by_name:
58                             error = "Invalid plugin name: %s" % val
59                             raise ValueError(error)
60                         new_names.append(val)
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:
65                                 new_names.append(val)
66                                 new_plugins.append(plugins_by_name[val])
67
68                     po = PluginObject()
69                     po.name = "global"
70                     globalconf = dict()
71                     globalconf['order'] = ','.join(new_names)
72                     po.set_config(globalconf)
73                     po.save_plugin_config(FACILITY)
74
75                     # When all is saved update also live config. The
76                     # live config is a list of the actual plugin
77                     # objects.
78                     self._site[FACILITY]['enabled'] = new_plugins
79
80                     message = "New configuration saved."
81                     message_type = "success"
82
83                 except ValueError, e:
84                     message = str(e)
85                     message_type = "error"
86
87                 except Exception, e:  # pylint: disable=broad-except
88                     message = "Failed to save data!"
89                     message_type = "error"
90
91         return self._template('admin/login_order.html',
92                               message=message,
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']])
98
99
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)
108
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)
113
114         self.order = LoginPluginsOrder(self._site, self)
115
116     def root_with_msg(self, message=None, message_type=None):
117         login_plugins = self._site[FACILITY]
118         ordered = []
119         for p in login_plugins['enabled']:
120             ordered.append(p.name)
121         return self._template('admin/login.html', title=self.title,
122                               message=message,
123                               message_type=message_type,
124                               available=login_plugins['available'],
125                               enabled=ordered,
126                               menu=self._master.menu)
127
128     def root(self, *args, **kwargs):
129         return self.root_with_msg()
130
131     def enable(self, plugin):
132         msg = None
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
143
144     def disable(self, plugin):
145         msg = None
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