Refactor plugin configuration
[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.log import Log
21 from ipsilon.util.plugin import PluginInstaller, PluginLoader
22 from ipsilon.util.plugin import PluginObject, PluginConfig
23 from ipsilon.util.page import Page
24 import cherrypy
25
26
27 class ProviderException(Exception, Log):
28
29     def __init__(self, message):
30         super(ProviderException, self).__init__(message)
31         self.message = message
32
33     def __str__(self):
34         return repr(self.message)
35
36
37 class AuthenticationError(ProviderException):
38
39     def __init__(self, message, code):
40         super(AuthenticationError, self).__init__(message)
41         self.code = code
42         self._debug('%s [%s]' % (message, code))
43
44
45 class InvalidRequest(ProviderException):
46
47     def __init__(self, message):
48         super(InvalidRequest, self).__init__(message)
49         self._debug(message)
50
51
52 class ProviderBase(PluginConfig, PluginObject):
53
54     def __init__(self, name, path):
55         PluginConfig.__init__(self)
56         PluginObject.__init__(self)
57         self.name = name
58         self.path = path
59         self.tree = None
60         self.is_enabled = False
61
62     def on_enable(self):
63         # this one does nothing
64         # derived classes can override with custom behavior
65         return
66
67     def get_tree(self, site):
68         raise NotImplementedError
69
70     def register(self, site):
71         if self.tree:
72             # already registered
73             return
74
75         # configure self
76         plugins = site[FACILITY]
77         if self.name in plugins['config']:
78             self.import_config(plugins['config'][self.name])
79
80         # init pages and admin interfaces
81         self.tree = self.get_tree(site)
82
83         self._debug('IdP Provider registered: %s' % self.name)
84
85         if self.get_config_value('enabled') is True:
86             # and enable self
87             self._enable(site)
88
89     def _enable(self, site):
90         root = site[FACILITY]['root']
91         root.add_subtree(self.name, self.tree)
92         self._debug('IdP Provider enabled: %s' % self.name)
93         self.is_enabled = True
94         self.on_enable()
95
96     def enable(self, site):
97         if self.is_enabled:
98             return
99
100         self._enable(site)
101         self.set_config_value('enabled', True)
102         self.save_plugin_config(FACILITY)
103
104     def disable(self, site):
105         if not self.is_enabled:
106             return
107
108         # remove self to the root
109         root = site[FACILITY]['root']
110         root.del_subtree(self.name)
111
112         self.is_enabled = False
113         self.set_config_value('enabled', False)
114         self.save_plugin_config(FACILITY)
115         self._debug('IdP Provider disabled: %s' % self.name)
116
117
118 class ProviderPageBase(Page):
119
120     def __init__(self, site, config):
121         super(ProviderPageBase, self).__init__(site)
122         self.plugin_name = config.name
123         self.cfg = config
124
125     def GET(self, *args, **kwargs):
126         raise cherrypy.HTTPError(501)
127
128     def POST(self, *args, **kwargs):
129         raise cherrypy.HTTPError(501)
130
131     def root(self, *args, **kwargs):
132         method = cherrypy.request.method
133
134         preop = getattr(self, 'pre_%s' % method, None)
135         if preop and callable(preop):
136             preop(*args, **kwargs)
137
138         op = getattr(self, method, self.GET)
139         if callable(op):
140             return op(*args, **kwargs)
141         else:
142             raise cherrypy.HTTPError(405)
143
144     def _debug(self, fact):
145         superfact = '%s: %s' % (self.plugin_name, fact)
146         super(ProviderPageBase, self)._debug(superfact)
147
148     def _audit(self, fact):
149         cherrypy.log('%s: %s' % (self.plugin_name, fact))
150
151
152 FACILITY = 'provider_config'
153
154
155 class LoadProviders(Log):
156
157     def __init__(self, root, site):
158         loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
159         site[FACILITY] = loader.get_plugin_data()
160         providers = site[FACILITY]
161
162         available = providers['available'].keys()
163         self._debug('Available providers: %s' % str(available))
164
165         providers['root'] = root
166         for item in providers['available']:
167             plugin = providers['available'][item]
168             plugin.register(site)
169
170
171 class ProvidersInstall(object):
172
173     def __init__(self):
174         pi = PluginInstaller(ProvidersInstall)
175         self.plugins = pi.get_plugins()