3 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
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.
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.
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/>.
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
27 class ProviderException(Exception, Log):
29 def __init__(self, message):
30 super(ProviderException, self).__init__(message)
31 self.message = message
34 return repr(self.message)
37 class ProviderBase(PluginObject):
39 def __init__(self, name, path):
40 super(ProviderBase, self).__init__()
46 # this one does nothing
47 # derived classes can override with custom behavior
50 def get_tree(self, site):
51 raise NotImplementedError
53 def register(self, site):
59 plugins = site[FACILITY]
60 if self.name in plugins['config']:
61 self.set_config(plugins['config'][self.name])
63 # init pages and admin interfaces
64 self.tree = self.get_tree(site)
66 self._debug('IdP Provider registered: %s' % self.name)
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)
76 if self.get_config_value('enabled') == '1':
80 def enable(self, site):
84 # and add self to the root
85 root = site[FACILITY]['root']
86 root.add_subtree(self.name, self.tree)
88 self.set_config_value('enabled', '1')
89 self.save_plugin_config(FACILITY)
92 self._debug('IdP Provider enabled: %s' % self.name)
94 def disable(self, site):
95 if not self.is_enabled:
98 # remove self to the root
99 root = site[FACILITY]['root']
100 root.del_subtree(self.name)
102 self.set_config_value('enabled', '0')
103 self.save_plugin_config(FACILITY)
104 self._debug('IdP Provider disabled: %s' % self.name)
107 class ProviderPageBase(Page):
109 def __init__(self, site, config):
110 super(ProviderPageBase, self).__init__(site)
111 self.plugin_name = config.name
114 def GET(self, *args, **kwargs):
115 raise cherrypy.HTTPError(501)
117 def POST(self, *args, **kwargs):
118 raise cherrypy.HTTPError(501)
120 def root(self, *args, **kwargs):
121 op = getattr(self, cherrypy.request.method, self.GET)
123 return op(*args, **kwargs)
125 raise cherrypy.HTTPError(405)
127 def _debug(self, fact):
128 superfact = '%s: %s' % (self.plugin_name, fact)
129 super(ProviderPageBase, self)._debug(superfact)
131 def _audit(self, fact):
132 cherrypy.log('%s: %s' % (self.plugin_name, fact))
135 FACILITY = 'provider_config'
138 class LoadProviders(Log):
140 def __init__(self, root, site):
141 loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
142 site[FACILITY] = loader.get_plugin_data()
143 providers = site[FACILITY]
145 available = providers['available'].keys()
146 self._debug('Available providers: %s' % str(available))
148 providers['root'] = root
149 for item in providers['available']:
150 plugin = providers['available'][item]
151 plugin.register(site)
154 class ProvidersInstall(object):
157 pi = PluginInstaller(ProvidersInstall)
158 self.plugins = pi.get_plugins()