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