Reuse the AdminPlugins class for the providers too
[cascardo/ipsilon.git] / ipsilon / login / common.py
index 94284b0..73422ae 100755 (executable)
@@ -24,7 +24,6 @@ from ipsilon.util.plugin import PluginLoader, PluginObject
 from ipsilon.util.plugin import PluginInstaller
 from ipsilon.info.common import Info
 from ipsilon.util.cookies import SecureCookie
-from ipsilon.util.trans import Transaction
 import cherrypy
 
 
@@ -35,6 +34,7 @@ class LoginManagerBase(PluginObject, Log):
 
     def __init__(self):
         super(LoginManagerBase, self).__init__()
+        self._site = None
         self.path = '/'
         self.next_login = None
         self.info = None
@@ -116,7 +116,14 @@ class LoginManagerBase(PluginObject, Log):
     def get_tree(self, site):
         raise NotImplementedError
 
+    @property
+    def is_enabled(self):
+        if self._site:
+            return self in self._site[FACILITY]['enabled']
+        return False
+
     def enable(self, site):
+        self._site = site
         plugins = site[FACILITY]
         if self in plugins['enabled']:
             return
@@ -148,6 +155,7 @@ class LoginManagerBase(PluginObject, Log):
         self.info = root.info
 
     def disable(self, site):
+        self._site = site
         plugins = site[FACILITY]
         if self not in plugins['enabled']:
             return
@@ -193,7 +201,7 @@ class LoginFormBase(LoginPageBase):
         return self._template(self.formtemplate, **context)
 
     def root(self, *args, **kwargs):
-        self.trans = Transaction('login', **kwargs)
+        self.trans = self.get_valid_transaction('login', **kwargs)
         op = getattr(self, cherrypy.request.method, self.GET)
         if callable(op):
             return op(*args, **kwargs)
@@ -227,6 +235,8 @@ class LoginFormBase(LoginPageBase):
             "next_url": next_url,
             "username": username,
             "login_target": target,
+            "cancel_url": '%s/login/cancel?%s' % (self.basepath,
+                                                  self.trans.get_GET_arg()),
         }
         context.update(kwargs)
         if self.trans is not None:
@@ -243,6 +253,7 @@ class Login(Page):
 
     def __init__(self, *args, **kwargs):
         super(Login, self).__init__(*args, **kwargs)
+        self.cancel = Cancel(*args, **kwargs)
         self.first_login = None
         self.info = Info(self._site)
 
@@ -265,7 +276,7 @@ class Login(Page):
 
     def root(self, *args, **kwargs):
         if self.first_login:
-            trans = Transaction('login', **kwargs)
+            trans = self.get_valid_transaction('login', **kwargs)
             redirect = '%s/login/%s?%s' % (self.basepath,
                                            self.first_login.path,
                                            trans.get_GET_arg())
@@ -280,6 +291,25 @@ class Logout(Page):
         return self._template('logout.html', title='Logout')
 
 
+class Cancel(Page):
+
+    def GET(self, *args, **kwargs):
+
+        session = UserSession()
+        session.logout(None)
+
+        # return to the caller if any
+        transdata = self.get_valid_transaction('login', **kwargs).retrieve()
+        if 'login_return' not in transdata:
+            raise cherrypy.HTTPError(401)
+        raise cherrypy.HTTPRedirect(transdata['login_return'])
+
+    def root(self, *args, **kwargs):
+        op = getattr(self, cherrypy.request.method, self.GET)
+        if callable(op):
+            return op(*args, **kwargs)
+
+
 class LoginMgrsInstall(object):
 
     def __init__(self):