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