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