1f5e9fad05959e4cc53c61e726908a9561ec815a
[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, baseurl):
31         super(LoginPluginsOrder, self).__init__(site)
32         self.url = '%s/order' % baseurl
33
34     @admin_protect
35     def GET(self, *args, **kwargs):
36         return self._template('admin/login_order.html',
37                               title='login plugins order',
38                               name='admin_login_order_form',
39                               action=self.url,
40                               options=self._site[FACILITY]['enabled'])
41
42     @admin_protect
43     def POST(self, *args, **kwargs):
44         message = "Nothing was modified."
45         message_type = "info"
46         valid = self._site[FACILITY]['enabled']
47
48         if 'order' in kwargs:
49             order = kwargs['order'].split(',')
50             if len(order) != 0:
51                 new_values = []
52                 try:
53                     for v in order:
54                         val = v.strip()
55                         if val not in valid:
56                             error = "Invalid plugin name: %s" % val
57                             raise ValueError(error)
58                         new_values.append(val)
59                     if len(new_values) < len(valid):
60                         for val in valid:
61                             if val not in new_values:
62                                 new_values.append(val)
63
64                     po = PluginObject()
65                     po.name = "global"
66                     globalconf = dict()
67                     globalconf['order'] = ','.join(new_values)
68                     po.set_config(globalconf)
69                     po.save_plugin_config(FACILITY)
70
71                     # When all is saved update also live config
72                     self._site[FACILITY]['enabled'] = new_values
73
74                     message = "New configuration saved."
75                     message_type = "success"
76
77                 except ValueError, e:
78                     message = str(e)
79                     message_type = "error"
80
81                 except Exception, e:  # pylint: disable=broad-except
82                     message = "Failed to save data!"
83                     message_type = "error"
84
85         return self._template('admin/login_order.html',
86                               message=message,
87                               message_type=message_type,
88                               title='login plugins order',
89                               name='admin_login_order_form',
90                               action=self.url,
91                               options=self._site[FACILITY]['enabled'])
92
93     def root(self, *args, **kwargs):
94         cherrypy.log.error("method: %s" % cherrypy.request.method)
95         op = getattr(self, cherrypy.request.method, self.GET)
96         if callable(op):
97             return op(*args, **kwargs)
98
99
100 class LoginPlugins(Page):
101     def __init__(self, site, parent):
102         super(LoginPlugins, self).__init__(site)
103         parent.login = self
104         self.url = '%s/login' % parent.url
105
106         for plugin in self._site[FACILITY]['available']:
107             cherrypy.log.error('Admin login plugin: %s' % plugin)
108             obj = self._site[FACILITY]['available'][plugin]
109             self.__dict__[plugin] = AdminPluginPage(obj, self._site,
110                                                     self.url, FACILITY)
111
112         self.order = LoginPluginsOrder(self._site, self.url)
113
114     def root(self, *args, **kwargs):
115         login_plugins = self._site[FACILITY]
116         return self._template('admin/login.html', title='Login Plugins',
117                               available=login_plugins['available'],
118                               enabled=login_plugins['enabled'])