Add generic support for IdP plugin admin pages
[cascardo/ipsilon.git] / ipsilon / providers / common.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 from ipsilon.util.plugin import PluginLoader, PluginObject
21 from ipsilon.util.plugin import PluginInstaller
22 from ipsilon.util.page import Page
23 import cherrypy
24
25
26 class ProviderException(Exception):
27
28     def __init__(self, message):
29         super(ProviderException, self).__init__(message)
30         self.message = message
31
32     def __str__(self):
33         return repr(self.message)
34
35     def _debug(self, fact):
36         if cherrypy.config.get('debug', False):
37             cherrypy.log('%s: %s' % (self.__class__.__name__, fact))
38
39
40 class ProviderBase(PluginObject):
41
42     def __init__(self, name, path):
43         super(ProviderBase, self).__init__()
44         self.name = name
45         self.path = path
46         self.admin = None
47
48     def _debug(self, fact):
49         if cherrypy.config.get('debug', False):
50             cherrypy.log(fact)
51
52     def get_tree(self, site):
53         raise NotImplementedError
54
55     def enable(self, site):
56         plugins = site[FACILITY]
57         if self in plugins['enabled']:
58             return
59
60         # configure self
61         if self.name in plugins['config']:
62             self.set_config(plugins['config'][self.name])
63
64         # and add self to the root
65         root = plugins['root']
66         root.add_subtree(self.name, self.get_tree(site))
67
68         plugins['enabled'].append(self)
69         self._debug('IdP Provider enabled: %s' % self.name)
70
71     def disable(self, site):
72         plugins = site[FACILITY]
73         if self not in plugins['enabled']:
74             return
75
76         # remove self to the root
77         root = plugins['root']
78         root.del_subtree(self.name)
79
80         plugins['enabled'].remove(self)
81         self._debug('IdP Provider disabled: %s' % self.name)
82
83
84 class ProviderPageBase(Page):
85
86     def __init__(self, site, config):
87         super(ProviderPageBase, self).__init__(site)
88         self.plugin_name = config.name
89         self.cfg = config
90
91     def GET(self, *args, **kwargs):
92         raise cherrypy.HTTPError(501)
93
94     def POST(self, *args, **kwargs):
95         raise cherrypy.HTTPError(501)
96
97     def root(self, *args, **kwargs):
98         op = getattr(self, cherrypy.request.method, self.GET)
99         if callable(op):
100             return op(*args, **kwargs)
101         else:
102             raise cherrypy.HTTPError(405)
103
104     def _debug(self, fact):
105         superfact = '%s: %s' % (self.plugin_name, fact)
106         super(ProviderPageBase, self)._debug(superfact)
107
108     def _audit(self, fact):
109         cherrypy.log('%s: %s' % (self.plugin_name, fact))
110
111
112 FACILITY = 'provider_config'
113
114
115 class LoadProviders(object):
116
117     def __init__(self, root, site):
118         loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
119         site[FACILITY] = loader.get_plugin_data()
120         providers = site[FACILITY]
121
122         available = providers['available'].keys()
123         self._debug('Available providers: %s' % str(available))
124
125         providers['root'] = root
126         for item in providers['whitelist']:
127             self._debug('IdP Provider in whitelist: %s' % item)
128             if item not in providers['available']:
129                 continue
130             providers['available'][item].enable(site)
131
132     def _debug(self, fact):
133         if cherrypy.config.get('debug', False):
134             cherrypy.log(fact)
135
136
137 class ProvidersInstall(object):
138
139     def __init__(self):
140         pi = PluginInstaller(ProvidersInstall)
141         self.plugins = pi.get_plugins()