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 def get_tree(self, site):
47 raise NotImplementedError
49 def register(self, site):
55 plugins = site[FACILITY]
56 if self.name in plugins['config']:
57 self.set_config(plugins['config'][self.name])
59 # init pages and admin interfaces
60 self.tree = self.get_tree(site)
62 self._debug('IdP Provider registered: %s' % self.name)
64 if self.get_config_value('enabled') == '1':
65 # and add self to the root
66 root = site[FACILITY]['root']
67 root.add_subtree(self.name, self.tree)
68 self._debug('IdP Provider enabled: %s' % self.name)
72 if self.get_config_value('enabled') == '1':
76 def enable(self, site):
80 # and add self to the root
81 root = site[FACILITY]['root']
82 root.add_subtree(self.name, self.tree)
84 self.set_config_value('enabled', '1')
85 self.save_plugin_config(FACILITY)
86 self._debug('IdP Provider enabled: %s' % self.name)
88 def disable(self, site):
89 if not self.is_enabled:
92 # remove self to the root
93 root = site[FACILITY]['root']
94 root.del_subtree(self.name)
96 self.set_config_value('enabled', '0')
97 self.save_plugin_config(FACILITY)
98 self._debug('IdP Provider disabled: %s' % self.name)
101 class ProviderPageBase(Page):
103 def __init__(self, site, config):
104 super(ProviderPageBase, self).__init__(site)
105 self.plugin_name = config.name
108 def GET(self, *args, **kwargs):
109 raise cherrypy.HTTPError(501)
111 def POST(self, *args, **kwargs):
112 raise cherrypy.HTTPError(501)
114 def root(self, *args, **kwargs):
115 op = getattr(self, cherrypy.request.method, self.GET)
117 return op(*args, **kwargs)
119 raise cherrypy.HTTPError(405)
121 def _debug(self, fact):
122 superfact = '%s: %s' % (self.plugin_name, fact)
123 super(ProviderPageBase, self)._debug(superfact)
125 def _audit(self, fact):
126 cherrypy.log('%s: %s' % (self.plugin_name, fact))
129 FACILITY = 'provider_config'
132 class LoadProviders(Log):
134 def __init__(self, root, site):
135 loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
136 site[FACILITY] = loader.get_plugin_data()
137 providers = site[FACILITY]
139 available = providers['available'].keys()
140 self._debug('Available providers: %s' % str(available))
142 providers['root'] = root
143 for item in providers['available']:
144 plugin = providers['available'][item]
145 plugin.register(site)
148 class ProvidersInstall(object):
151 pi = PluginInstaller(ProvidersInstall)
152 self.plugins = pi.get_plugins()