In configure we do not need to set_config()
[cascardo/ipsilon.git] / ipsilon / login / common.py
index cb45fd6..028b754 100755 (executable)
@@ -17,7 +17,6 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from ipsilon.util.log import Log
 from ipsilon.util.page import Page
 from ipsilon.util.user import UserSession
 from ipsilon.util.plugin import PluginLoader, PluginObject
 from ipsilon.util.page import Page
 from ipsilon.util.user import UserSession
 from ipsilon.util.plugin import PluginLoader, PluginObject
@@ -30,13 +29,15 @@ import cherrypy
 USERNAME_COOKIE = 'ipsilon_default_username'
 
 
 USERNAME_COOKIE = 'ipsilon_default_username'
 
 
-class LoginManagerBase(PluginObject, Log):
+class LoginManagerBase(PluginObject):
 
     def __init__(self):
         super(LoginManagerBase, self).__init__()
 
     def __init__(self):
         super(LoginManagerBase, self).__init__()
+        self._site = None
         self.path = '/'
         self.next_login = None
         self.info = None
         self.path = '/'
         self.next_login = None
         self.info = None
+        self.is_enabled = False
 
     def redirect_to_path(self, path):
         base = cherrypy.config.get('base.mount', "")
 
     def redirect_to_path(self, path):
         base = cherrypy.config.get('base.mount', "")
@@ -116,10 +117,13 @@ class LoginManagerBase(PluginObject, Log):
         raise NotImplementedError
 
     def enable(self, site):
         raise NotImplementedError
 
     def enable(self, site):
-        plugins = site[FACILITY]
-        if self in plugins['enabled']:
+        if self.is_enabled:
             return
 
             return
 
+        if not self._site:
+            self._site = site
+        plugins = self._site[FACILITY]
+
         # configure self
         if self.name in plugins['config']:
             self.set_config(plugins['config'][self.name])
         # configure self
         if self.name in plugins['config']:
             self.set_config(plugins['config'][self.name])
@@ -141,16 +145,18 @@ class LoginManagerBase(PluginObject, Log):
             root.first_login = self
 
         plugins['enabled'].append(self)
             root.first_login = self
 
         plugins['enabled'].append(self)
+        self.is_enabled = True
         self._debug('Login plugin enabled: %s' % self.name)
 
         # Get handle of the info plugin
         self.info = root.info
 
     def disable(self, site):
         self._debug('Login plugin enabled: %s' % self.name)
 
         # Get handle of the info plugin
         self.info = root.info
 
     def disable(self, site):
-        plugins = site[FACILITY]
-        if self not in plugins['enabled']:
+        if not self.is_enabled:
             return
 
             return
 
+        plugins = self._site[FACILITY]
+
         # remove self from chain
         root = plugins['root']
         if root.first_login == self:
         # remove self from chain
         root = plugins['root']
         if root.first_login == self:
@@ -164,6 +170,7 @@ class LoginManagerBase(PluginObject, Log):
         self.next_login = None
 
         plugins['enabled'].remove(self)
         self.next_login = None
 
         plugins['enabled'].remove(self)
+        self.is_enabled = False
         self._debug('Login plugin disabled: %s' % self.name)
 
 
         self._debug('Login plugin disabled: %s' % self.name)
 
 
@@ -226,6 +233,8 @@ class LoginFormBase(LoginPageBase):
             "next_url": next_url,
             "username": username,
             "login_target": target,
             "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:
         }
         context.update(kwargs)
         if self.trans is not None:
@@ -242,6 +251,7 @@ class Login(Page):
 
     def __init__(self, *args, **kwargs):
         super(Login, self).__init__(*args, **kwargs)
 
     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)
 
         self.first_login = None
         self.info = Info(self._site)
 
@@ -279,6 +289,25 @@ class Logout(Page):
         return self._template('logout.html', title='Logout')
 
 
         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):
 class LoginMgrsInstall(object):
 
     def __init__(self):