Handle invalid/expired transactions gracefully
[cascardo/ipsilon.git] / ipsilon / util / page.py
index a99d2f4..213f945 100755 (executable)
@@ -19,6 +19,7 @@
 
 from ipsilon.util.log import Log
 from ipsilon.util.user import UserSession
+from ipsilon.util.trans import Transaction
 from urllib import unquote
 import cherrypy
 
@@ -34,16 +35,6 @@ def admin_protect(fn):
     return check
 
 
-def auth_protect(fn):
-    def check(self, *args, **kwargs):
-        if UserSession().get_user().is_anonymous:
-            raise cherrypy.HTTPRedirect(self.basepath)
-        else:
-            return fn(self, *args, **kwargs)
-
-    return check
-
-
 class Page(Log):
     def __init__(self, site, form=False):
         if 'template_env' not in site:
@@ -52,6 +43,8 @@ class Page(Log):
         self.basepath = cherrypy.config.get('base.mount', "")
         self.user = None
         self._is_form_page = form
+        self.default_headers = dict()
+        self.auth_protect = False
 
     def _compare_urls(self, url1, url2):
         u1 = unquote(url1)
@@ -62,8 +55,13 @@ class Page(Log):
 
     def __call__(self, *args, **kwargs):
         # pylint: disable=star-args
+        cherrypy.response.headers.update(self.default_headers)
+
         self.user = UserSession().get_user()
 
+        if self.auth_protect and self.user.is_anonymous:
+            raise cherrypy.HTTPError(401)
+
         if len(args) > 0:
             op = getattr(self, args[0], None)
             if callable(op) and getattr(op, 'public_function', None):
@@ -116,4 +114,11 @@ class Page(Log):
     def del_subtree(self, name):
         del self.__dict__[name]
 
+    def get_valid_transaction(self, provider, **kwargs):
+        try:
+            return Transaction(provider, **kwargs)
+        except ValueError:
+            msg = 'Transaction expired, or cookies not available'
+            raise cherrypy.HTTPError(401, msg)
+
     exposed = True