Fix storing info plugin status and order
authorSimo Sorce <simo@redhat.com>
Thu, 9 Oct 2014 22:09:54 +0000 (18:09 -0400)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Fri, 24 Oct 2014 16:02:09 +0000 (18:02 +0200)
This is the same issue already resolved for the login plugins in
commit a6ed2bba137df5fb8a9fb2931ccb2d92ca3fa0e0

Signed-off-by: Simo Sorce <simo@redhat.com>
Reviewed-by: Patrick Uiterwijk <puiterwijk@redhat.com>
ipsilon/admin/info.py
ipsilon/info/common.py

index cea6b0e..d3f5284 100755 (executable)
@@ -10,6 +10,15 @@ from ipsilon.admin.common import AdminPage
 from ipsilon.info.common import FACILITY
 
 
 from ipsilon.info.common import FACILITY
 
 
+def save_enabled_plugins(names):
+    po = PluginObject()
+    po.name = "global"
+    globalconf = dict()
+    globalconf['order'] = ','.join(names)
+    po.set_config(globalconf)
+    po.save_plugin_config(FACILITY)
+
+
 class InfoPluginsOrder(AdminPage):
 
     def __init__(self, site, parent):
 class InfoPluginsOrder(AdminPage):
 
     def __init__(self, site, parent):
@@ -51,12 +60,7 @@ class InfoPluginsOrder(AdminPage):
                                 new_names.append(val)
                                 new_plugins.append(plugins_by_name[val])
 
                                 new_names.append(val)
                                 new_plugins.append(plugins_by_name[val])
 
-                    po = PluginObject()
-                    po.name = "global"
-                    globalconf = dict()
-                    globalconf['order'] = ','.join(new_names)
-                    po.set_config(globalconf)
-                    po.save_plugin_config(FACILITY)
+                    save_enabled_plugins(new_names)
 
                     # When all is saved update also live config. The
                     # live config is a list of the actual plugin
 
                     # When all is saved update also live config. The
                     # live config is a list of the actual plugin
@@ -125,6 +129,7 @@ class InfoPlugins(AdminPage):
         obj = plugins['available'][plugin]
         if obj not in plugins['enabled']:
             obj.enable(self._site)
         obj = plugins['available'][plugin]
         if obj not in plugins['enabled']:
             obj.enable(self._site)
+            save_enabled_plugins(list(x.name for x in plugins['enabled']))
             msg = "Plugin %s enabled" % obj.name
         return self.root_with_msg(msg, "success")
     enable.public_function = True
             msg = "Plugin %s enabled" % obj.name
         return self.root_with_msg(msg, "success")
     enable.public_function = True
@@ -139,6 +144,7 @@ class InfoPlugins(AdminPage):
         obj = plugins['available'][plugin]
         if obj in plugins['enabled']:
             obj.disable(self._site)
         obj = plugins['available'][plugin]
         if obj in plugins['enabled']:
             obj.disable(self._site)
+            save_enabled_plugins(list(x.name for x in plugins['enabled']))
             msg = "Plugin %s disabled" % obj.name
         return self.root_with_msg(msg, "success")
     disable.public_function = True
             msg = "Plugin %s disabled" % obj.name
         return self.root_with_msg(msg, "success")
     disable.public_function = True
index 4fbb7ef..c4be8fe 100755 (executable)
@@ -13,11 +13,18 @@ class InfoProviderBase(PluginObject, Log):
 
     def __init__(self):
         super(InfoProviderBase, self).__init__()
 
     def __init__(self):
         super(InfoProviderBase, self).__init__()
+        self.enabled = False
 
     def get_user_attrs(self, user):
         raise NotImplementedError
 
 
     def get_user_attrs(self, user):
         raise NotImplementedError
 
+    @property
+    def is_enabled(self):
+        return self.enabled
+
     def enable(self, site):
     def enable(self, site):
+        self.enabled = True
+
         plugins = site[FACILITY]
         if self in plugins['enabled']:
             return
         plugins = site[FACILITY]
         if self in plugins['enabled']:
             return
@@ -30,6 +37,8 @@ class InfoProviderBase(PluginObject, Log):
         self.debug('Info plugin enabled: %s' % self.name)
 
     def disable(self, site):
         self.debug('Info plugin enabled: %s' % self.name)
 
     def disable(self, site):
+        self.enabled = False
+
         plugins = site[FACILITY]
         if self not in plugins['enabled']:
             return
         plugins = site[FACILITY]
         if self not in plugins['enabled']:
             return
@@ -45,7 +54,6 @@ class Info(Log):
 
     def __init__(self, site):
         self._site = site
 
     def __init__(self, site):
         self._site = site
-        self.providers = []
 
         loader = PluginLoader(Info, FACILITY, 'InfoProvider')
         self._site[FACILITY] = loader.get_plugin_data()
 
         loader = PluginLoader(Info, FACILITY, 'InfoProvider')
         self._site[FACILITY] = loader.get_plugin_data()
@@ -54,26 +62,30 @@ class Info(Log):
         available = plugins['available'].keys()
         self.debug('Available info providers: %s' % str(available))
 
         available = plugins['available'].keys()
         self.debug('Available info providers: %s' % str(available))
 
+        plugins['root'] = self
         for item in plugins['whitelist']:
             self.debug('Login plugin in whitelist: %s' % item)
             if item not in plugins['available']:
                 self.debug('Info Plugin %s not found' % item)
                 continue
         for item in plugins['whitelist']:
             self.debug('Login plugin in whitelist: %s' % item)
             if item not in plugins['available']:
                 self.debug('Info Plugin %s not found' % item)
                 continue
-            self.providers.append((item, plugins['available'][item]))
-            self.debug('Added Info plugin: %s' % item)
-
-    def get_user_attrs(self, user, provider=None):
-        if provider:
-            for p in self.providers:
-                if p[0] == provider:
-                    return p[1].get_user_attrs(user)
-        else:
-            for p in self.providers:
-                ret = p[1].get_user_attrs(user)
-                if ret:
-                    return ret
-
-        return None
+            plugins['available'][item].enable(self._site)
+
+    def get_user_attrs(self, user, requested=None):
+        plugins = self._site[FACILITY]['available']
+        result = dict()
+
+        for _, p in plugins.items():
+            if requested is None:
+                if not p.is_enabled:
+                    continue
+            else:
+                if requested != p.name:
+                    continue
+            result = p.get_user_attrs(user)
+            if result:
+                break
+
+        return result
 
 
 class InfoProviderInstaller(object):
 
 
 class InfoProviderInstaller(object):