Add server-install plugin configuration support
[cascardo/ipsilon.git] / ipsilon / login / common.py
index 4888060..d290521 100755 (executable)
@@ -20,6 +20,7 @@
 from ipsilon.util.page import Page
 from ipsilon.util.user import UserSession
 from ipsilon.util.plugin import PluginLoader, PluginObject
+from ipsilon.util.plugin import PluginInstaller
 import cherrypy
 
 
@@ -34,24 +35,37 @@ class LoginManagerBase(PluginObject):
         base = cherrypy.config.get('base.mount', "")
         raise cherrypy.HTTPRedirect('%s/login/%s' % (base, path))
 
-    def auth_successful(self, username):
+    def auth_successful(self, username, userdata=None):
         # save ref before calling UserSession login() as it
         # may regenerate the session
-        ref = cherrypy.config.get('base.mount', "") + '/'
-        if 'referral' in cherrypy.session:
-            ref = cherrypy.session['referral']
+        session = UserSession()
+        ref = session.get_data('login', 'Return')
+        if not ref:
+            ref = cherrypy.config.get('base.mount', "") + '/'
+
+        session.login(username)
+
+        # Save additional data provided by the login manager
+        if userdata:
+            for key in userdata:
+                session.save_data('user', key, userdata[key])
 
-        UserSession().login(username)
         raise cherrypy.HTTPRedirect(ref)
 
     def auth_failed(self):
-        # Just make sure we destroy the session
-        UserSession().logout(None)
-
+        # try with next module
         if self.next_login:
             return self.redirect_to_path(self.next_login.path)
 
-        ref = cherrypy.config.get('base.mount', "") + '/unauthorized'
+        # return to the caller if any
+        session = UserSession()
+        ref = session.get_data('login', 'Return')
+
+        # otherwise destroy session and return error
+        if not ref:
+            session.logout(None)
+            raise cherrypy.HTTPError(401)
+
         raise cherrypy.HTTPRedirect(ref)
 
 
@@ -111,3 +125,10 @@ class Logout(Page):
     def root(self, *args, **kwargs):
         UserSession().logout(self.user)
         return self._template('logout.html', title='Logout')
+
+
+class LoginMgrsInstall(object):
+
+    def __init__(self):
+        pi = PluginInstaller(LoginMgrsInstall)
+        self.plugins = pi.get_plugins()