Convert all forms to use util.Page form support
[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=self._site[FACILITY]['enabled'])
42
43     @admin_protect
44     def POST(self, *args, **kwargs):
45         message = "Nothing was modified."
46         message_type = "info"
47         valid = self._site[FACILITY]['enabled']
48
49         if 'order' in kwargs:
50             order = kwargs['order'].split(',')
51             if len(order) != 0:
52                 new_values = []
53                 try:
54                     for v in order:
55                         val = v.strip()
56                         if val not in valid:
57                             error = "Invalid plugin name: %s" % val
58                             raise ValueError(error)
59                         new_values.append(val)
60                     if len(new_values) < len(valid):
61                         for val in valid:
62                             if val not in new_values:
63                                 new_values.append(val)
64
65                     po = PluginObject()
66                     po.name = "global"
67                     globalconf = dict()
68                     globalconf['order'] = ','.join(new_values)
69                     po.set_config(globalconf)
70                     po.save_plugin_config(FACILITY)
71
72                     # When all is saved update also live config
73                     self._site[FACILITY]['enabled'] = new_values
74
75                     message = "New configuration saved."
76                     message_type = "success"
77
78                 except ValueError, e:
79                     message = str(e)
80                     message_type = "error"
81
82                 except Exception, e:  # pylint: disable=broad-except
83                     message = "Failed to save data!"
84                     message_type = "error"
85
86         return self._template('admin/login_order.html',
87                               message=message,
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'])
93
94
95 class LoginPlugins(Page):
96     def __init__(self, site, parent):
97         super(LoginPlugins, self).__init__(site)
98         self._master = parent
99         self.title = 'Login Plugins'
100         self.url = '%s/login' % parent.url
101         self.facility = FACILITY
102         parent.add_subtree('login', self)
103
104         for plugin in self._site[FACILITY]['available']:
105             cherrypy.log.error('Admin login plugin: %s' % plugin)
106             obj = self._site[FACILITY]['available'][plugin]
107             self.__dict__[plugin] = AdminPluginPage(obj, self._site, self)
108
109         self.order = LoginPluginsOrder(self._site, self)
110
111     def root_with_msg(self, message=None, message_type=None):
112         login_plugins = self._site[FACILITY]
113         ordered = []
114         for p in login_plugins['enabled']:
115             ordered.append(p.name)
116         return self._template('admin/login.html', title=self.title,
117                               message=message,
118                               message_type=message_type,
119                               available=login_plugins['available'],
120                               enabled=ordered,
121                               menu=self._master.menu)
122
123     def root(self, *args, **kwargs):
124         return self.root_with_msg()
125
126     def enable(self, plugin):
127         msg = None
128         plugins = self._site[FACILITY]
129         if plugin not in plugins['available']:
130             msg = "Unknown plugin %s" % plugin
131             return self.root_with_msg(msg, "error")
132         obj = plugins['available'][plugin]
133         if obj not in plugins['enabled']:
134             obj.enable(self._site)
135             msg = "Plugin %s enabled" % obj.name
136         return self.root_with_msg(msg, "success")
137     enable.exposed = True
138
139     def disable(self, plugin):
140         msg = None
141         plugins = self._site[FACILITY]
142         if plugin not in plugins['available']:
143             msg = "Unknown plugin %s" % plugin
144             return self.root_with_msg(msg, "error")
145         obj = plugins['available'][plugin]
146         if obj in plugins['enabled']:
147             obj.disable(self._site)
148             msg = "Plugin %s disabled" % obj.name
149         return self.root_with_msg(msg, "success")
150     disable.exposed = True