26e96a7d5d18bf6bf4c26c8b83f004ae8ec958a7
[cascardo/ipsilon.git] / ipsilon / admin / providers.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
21 import cherrypy
22 from ipsilon.util.page import Page
23 from ipsilon.providers.common import FACILITY
24 from ipsilon.admin.common import AdminPluginPage
25
26
27 class ProviderPlugins(Page):
28     def __init__(self, site, parent):
29         super(ProviderPlugins, self).__init__(site)
30         self._master = parent
31         self.title = 'Identity Providers'
32         self.url = '%s/providers' % parent.url
33         self.facility = FACILITY
34         parent.add_subtree('providers', self)
35
36         for plugin in self._site[FACILITY]['available']:
37             cherrypy.log.error('Admin provider plugin: %s' % plugin)
38             obj = self._site[FACILITY]['available'][plugin]
39             self.__dict__[plugin] = AdminPluginPage(obj, self)
40
41     def root_with_msg(self, message=None, message_type=None):
42         plugins = self._site[FACILITY]
43         return self._template('admin/providers.html', title=self.title,
44                               baseurl=self.url,
45                               message=message,
46                               message_type=message_type,
47                               available=plugins['available'],
48                               enabled=plugins['enabled'],
49                               menu=self._master.menu)
50
51     def root(self, *args, **kwargs):
52         return self.root_with_msg()
53
54     def enable(self, plugin):
55         msg = None
56         plugins = self._site[FACILITY]
57         if plugin not in plugins['available']:
58             msg = "Unknown plugin %s" % plugin
59             return self.root_with_msg(msg, "error")
60         obj = plugins['available'][plugin]
61         if obj not in plugins['enabled']:
62             obj.enable(self._site)
63             msg = "Plugin %s enabled" % obj.name
64         return self.root_with_msg(msg, "success")
65     enable.exposed = True
66
67     def disable(self, plugin):
68         msg = None
69         plugins = self._site[FACILITY]
70         if plugin not in plugins['available']:
71             msg = "Unknown plugin %s" % plugin
72             return self.root_with_msg(msg, "error")
73         obj = plugins['available'][plugin]
74         if obj in plugins['enabled']:
75             obj.disable(self._site)
76             msg = "Plugin %s disabled" % obj.name
77         return self.root_with_msg(msg, "success")
78     disable.exposed = True