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__()
45 def get_tree(self, site):
46 raise NotImplementedError
48 def register(self, site):
54 plugins = site[FACILITY]
55 if self.name in plugins['config']:
56 self.set_config(plugins['config'][self.name])
58 # init pages and admin interfaces
59 self.tree = self.get_tree(site)
61 self._debug('IdP Provider registered: %s' % self.name)
63 if self.get_config_value('enabled') == '1':
64 # and add self to the root
65 root = site[FACILITY]['root']
66 root.add_subtree(self.name, self.tree)
67 self._debug('IdP Provider enabled: %s' % self.name)
71 if self.get_config_value('enabled') == '1':
75 def enable(self, site):
79 # and add self to the root
80 root = site[FACILITY]['root']
81 root.add_subtree(self.name, self.tree)
83 self.set_config_value('enabled', '1')
84 self.save_plugin_config(FACILITY)
85 self._debug('IdP Provider enabled: %s' % self.name)
87 def disable(self, site):
88 if not self.is_enabled:
91 # remove self to the root
92 root = site[FACILITY]['root']
93 root.del_subtree(self.name)
95 self.set_config_value('enabled', '0')
96 self.save_plugin_config(FACILITY)
97 self._debug('IdP Provider disabled: %s' % self.name)
100 class ProviderPageBase(Page):
102 def __init__(self, site, config):
103 super(ProviderPageBase, self).__init__(site)
104 self.plugin_name = config.name
107 def GET(self, *args, **kwargs):
108 raise cherrypy.HTTPError(501)
110 def POST(self, *args, **kwargs):
111 raise cherrypy.HTTPError(501)
113 def root(self, *args, **kwargs):
114 op = getattr(self, cherrypy.request.method, self.GET)
116 return op(*args, **kwargs)
118 raise cherrypy.HTTPError(405)
120 def _debug(self, fact):
121 superfact = '%s: %s' % (self.plugin_name, fact)
122 super(ProviderPageBase, self)._debug(superfact)
124 def _audit(self, fact):
125 cherrypy.log('%s: %s' % (self.plugin_name, fact))
128 FACILITY = 'provider_config'
131 class LoadProviders(Log):
133 def __init__(self, root, site):
134 loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
135 site[FACILITY] = loader.get_plugin_data()
136 providers = site[FACILITY]
138 available = providers['available'].keys()
139 self._debug('Available providers: %s' % str(available))
141 providers['root'] = root
142 for item in providers['available']:
143 plugin = providers['available'][item]
144 plugin.register(site)
147 class ProvidersInstall(object):
150 pi = PluginInstaller(ProvidersInstall)
151 self.plugins = pi.get_plugins()