Commits

"danjac"  committed e23b96c

handle http exceptions

  • Participants
  • Parent commits 2de14ed

Comments (0)

Files changed (2)

File flaskext/principal.py

 from collections import namedtuple, deque
 
 
-from flask import g, session, current_app
+from flask import g, session, current_app, abort
 from flask.signals import Namespace
 
 
     flow is continued (context manager) or the function is executed (decorator).
     """
 
-    def __init__(self, permission):
+    def __init__(self, permission, http_exception=None):
         self.permission = permission
+        self.http_exception = http_exception
         """The permission of this principal
         """
 
     def __enter__(self):
         # check the permission here
         if not self.can():
+            if self.http_exception:
+                abort(self.http_exception)
             raise PermissionDenied(self.permission)
 
     def __exit__(self, *exc):
         access.
         """
 
-    def require(self):
+    def require(self, http_exception=None):
         """Create a principal for this permission.
 
         The principal may be used as a context manager, or a decroator.
         """
-        return IdentityContext(self)
+        return IdentityContext(self, http_exception)
 
     def union(self, other):
         """Create a new permission with the requirements of the union of this

File tests/test_principal.py

         with admin_permission.require():
             with editor_permission.require():
                 pass
+    
+    @app.route('/j')
+    def h():
+        i = Identity('james')
+        identity_changed.send(app, identity=i)
+        with admin_permission.require(403):
+            with editor_permission.require(403):
+                pass
+
 
     return app
 
     client = mkapp().test_client()
     raises(PermissionDenied, client.open, '/g')
 
+def test_and_permissions_view_with_http_exc():
+    client = mkapp().test_client()
+    response = client.open("/j")
+    assert response.status_code == 403
 
+
+
+