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