Refactor plugin configuration
[cascardo/ipsilon.git] / ipsilon / providers / common.py
index b1968f4..ead50e2 100755 (executable)
@@ -18,8 +18,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from ipsilon.util.log import Log
-from ipsilon.util.plugin import PluginLoader, PluginObject
-from ipsilon.util.plugin import PluginInstaller
+from ipsilon.util.plugin import PluginInstaller, PluginLoader
+from ipsilon.util.plugin import PluginObject, PluginConfig
 from ipsilon.util.page import Page
 import cherrypy
 
@@ -34,13 +34,30 @@ class ProviderException(Exception, Log):
         return repr(self.message)
 
 
-class ProviderBase(PluginObject):
+class AuthenticationError(ProviderException):
+
+    def __init__(self, message, code):
+        super(AuthenticationError, self).__init__(message)
+        self.code = code
+        self._debug('%s [%s]' % (message, code))
+
+
+class InvalidRequest(ProviderException):
+
+    def __init__(self, message):
+        super(InvalidRequest, self).__init__(message)
+        self._debug(message)
+
+
+class ProviderBase(PluginConfig, PluginObject):
 
     def __init__(self, name, path):
-        super(ProviderBase, self).__init__()
+        PluginConfig.__init__(self)
+        PluginObject.__init__(self)
         self.name = name
         self.path = path
         self.tree = None
+        self.is_enabled = False
 
     def on_enable(self):
         # this one does nothing
@@ -58,39 +75,32 @@ class ProviderBase(PluginObject):
         # configure self
         plugins = site[FACILITY]
         if self.name in plugins['config']:
-            self.set_config(plugins['config'][self.name])
+            self.import_config(plugins['config'][self.name])
 
         # init pages and admin interfaces
         self.tree = self.get_tree(site)
 
         self._debug('IdP Provider registered: %s' % self.name)
 
-        if self.get_config_value('enabled') == '1':
-            # and add self to the root
-            root = site[FACILITY]['root']
-            root.add_subtree(self.name, self.tree)
-            self._debug('IdP Provider enabled: %s' % self.name)
+        if self.get_config_value('enabled') is True:
+            # and enable self
+            self._enable(site)
 
-    @property
-    def is_enabled(self):
-        if self.get_config_value('enabled') == '1':
-            return True
-        return False
+    def _enable(self, site):
+        root = site[FACILITY]['root']
+        root.add_subtree(self.name, self.tree)
+        self._debug('IdP Provider enabled: %s' % self.name)
+        self.is_enabled = True
+        self.on_enable()
 
     def enable(self, site):
         if self.is_enabled:
             return
 
-        # and add self to the root
-        root = site[FACILITY]['root']
-        root.add_subtree(self.name, self.tree)
-
-        self.set_config_value('enabled', '1')
+        self._enable(site)
+        self.set_config_value('enabled', True)
         self.save_plugin_config(FACILITY)
 
-        self.on_enable()
-        self._debug('IdP Provider enabled: %s' % self.name)
-
     def disable(self, site):
         if not self.is_enabled:
             return
@@ -99,7 +109,8 @@ class ProviderBase(PluginObject):
         root = site[FACILITY]['root']
         root.del_subtree(self.name)
 
-        self.set_config_value('enabled', '0')
+        self.is_enabled = False
+        self.set_config_value('enabled', False)
         self.save_plugin_config(FACILITY)
         self._debug('IdP Provider disabled: %s' % self.name)