Allow turning off security at install time
[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         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,
42                               options=opts)
43
44     @admin_protect
45     def POST(self, *args, **kwargs):
46         message = "Nothing was modified."
47         message_type = "info"
48         plugins_by_name = {p.name: p for p in self._site[FACILITY]['enabled']}
49
50         if 'order' in kwargs:
51             order = kwargs['order'].split(',')
52             if len(order) != 0:
53                 new_names = []
54                 new_plugins = []
55                 try:
56                     for v in order:
57                         val = v.strip()
58                         if val not in plugins_by_name:
59                             error = "Invalid plugin name: %s" % val
60                             raise ValueError(error)
61                         new_names.append(val)
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:
66                                 new_names.append(val)
67                                 new_plugins.append(plugins_by_name[val])
68
69                     po = PluginObject()
70                     po.name = "global"
71                     globalconf = dict()
72                     globalconf['order'] = ','.join(new_names)
73                     po.set_config(globalconf)
74                     po.save_plugin_config(FACILITY)
75
76                     # When all is saved update also live config. The
77                     # live config is a list of the actual plugin
78                     # objects.
79                     self._site[FACILITY]['enabled'] = new_plugins
80
81                     message = "New configuration saved."
82                     message_type = "success"
83
84                 except ValueError, e:
85                     message = str(e)
86                     message_type = "error"
87
88                 except Exception, e:  # pylint: disable=broad-except
89                     message = "Failed to save data!"
90                     message_type = "error"
91
92         opts = [p.name for p in self._site[FACILITY]['enabled']]
93         return self._template('admin/login_order.html',
94                               message=message,
95                               message_type=message_type,
96                               title='login plugins order',
97                               name='admin_login_order_form',
98                               menu=self.menu, action=self.url,
99                               options=opts)
100
101
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)
110
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)
115
116         self.order = LoginPluginsOrder(self._site, self)
117
118     def root_with_msg(self, message=None, message_type=None):
119         login_plugins = self._site[FACILITY]
120         ordered = []
121         for p in login_plugins['enabled']:
122             ordered.append(p.name)
123         return self._template('admin/login.html', title=self.title,
124                               message=message,
125                               message_type=message_type,
126                               available=login_plugins['available'],
127                               enabled=ordered,
128                               menu=self._master.menu)
129
130     def root(self, *args, **kwargs):
131         return self.root_with_msg()
132
133     def enable(self, plugin):
134         msg = None
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
145
146     def disable(self, plugin):
147         msg = None
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