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.plugin import PluginLoader, PluginObject
21 from ipsilon.util.plugin import PluginInstaller
22 from ipsilon.util.page import Page
26 class ProviderException(Exception):
28 def __init__(self, message):
29 super(ProviderException, self).__init__(message)
30 self.message = message
33 return repr(self.message)
35 def _debug(self, fact):
36 if cherrypy.config.get('debug', False):
37 cherrypy.log('%s: %s' % (self.__class__.__name__, fact))
40 class ProviderBase(PluginObject):
42 def __init__(self, name, path):
43 super(ProviderBase, self).__init__()
49 def _debug(self, fact):
50 if cherrypy.config.get('debug', False):
53 def get_tree(self, site):
54 raise NotImplementedError
56 def register(self, site):
62 plugins = site[FACILITY]
63 if self.name in plugins['config']:
64 self.set_config(plugins['config'][self.name])
66 # init pages and admin interfaces
67 self.tree = self.get_tree(site)
69 self._debug('IdP Provider registered: %s' % self.name)
71 if self.get_config_value('enabled') == '1':
72 # and add self to the root
73 root = site[FACILITY]['root']
74 root.add_subtree(self.name, self.tree)
75 self._debug('IdP Provider enabled: %s' % self.name)
79 if self.get_config_value('enabled') == '1':
83 def enable(self, site):
87 # and add self to the root
88 root = site[FACILITY]['root']
89 root.add_subtree(self.name, self.tree)
91 self.set_config_value('enabled', '1')
92 self.save_plugin_config(FACILITY)
93 self._debug('IdP Provider enabled: %s' % self.name)
95 def disable(self, site):
96 if not self.is_enabled:
99 # remove self to the root
100 root = site[FACILITY]['root']
101 root.del_subtree(self.name)
103 self.set_config_value('enabled', '0')
104 self.save_plugin_config(FACILITY)
105 self._debug('IdP Provider disabled: %s' % self.name)
108 class ProviderPageBase(Page):
110 def __init__(self, site, config):
111 super(ProviderPageBase, self).__init__(site)
112 self.plugin_name = config.name
115 def GET(self, *args, **kwargs):
116 raise cherrypy.HTTPError(501)
118 def POST(self, *args, **kwargs):
119 raise cherrypy.HTTPError(501)
121 def root(self, *args, **kwargs):
122 op = getattr(self, cherrypy.request.method, self.GET)
124 return op(*args, **kwargs)
126 raise cherrypy.HTTPError(405)
128 def _debug(self, fact):
129 superfact = '%s: %s' % (self.plugin_name, fact)
130 super(ProviderPageBase, self)._debug(superfact)
132 def _audit(self, fact):
133 cherrypy.log('%s: %s' % (self.plugin_name, fact))
136 FACILITY = 'provider_config'
139 class LoadProviders(object):
141 def __init__(self, root, site):
142 loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
143 site[FACILITY] = loader.get_plugin_data()
144 providers = site[FACILITY]
146 available = providers['available'].keys()
147 self._debug('Available providers: %s' % str(available))
149 providers['root'] = root
150 for item in providers['available']:
151 plugin = providers['available'][item]
152 plugin.register(site)
154 def _debug(self, fact):
155 if cherrypy.config.get('debug', False):
159 class ProvidersInstall(object):
162 pi = PluginInstaller(ProvidersInstall)
163 self.plugins = pi.get_plugins()