Changeset 122

Show
Ignore:
Timestamp:
09/23/07 17:45:21
Author:
thejimmyg
Message:

Restructured the authorization code

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • AuthKit/branches/0.4/authkit/pylons_adaptors.py

    r96 r122  
    1 """Pylons specific code to facilitate using AuthKit with Pylons 
    2  
    3 There is a full Pylons manual in addition to the AuthKit manual which  
    4 you should read if you plan to use AuthKit with Pylons 
    5  
    6 .. Note :: 
    7  
    8     In addition to the authorize methods described here, you can also use the 
    9     default ``authkit.authorize.middleware`` function to add WSGI middleware 
    10     authorization checks to your Pylons application since Pylons has a full 
    11     WSGI middleware stack. Just add the middleware to your project's 
    12     ``config/middleware.py`` file. 
    13  
     1"""\ 
     2This module is deprecated. Please update your code to use  
     3``authkit.authorize.pylons`` instead of this module. 
    144""" 
    15  
    16 from decorator import decorator 
    17 from pylons import request 
    18  
    19 from authkit.authorize import PermissionSetupError 
    20 from authkit.authorize import NotAuthenticatedError, NotAuthorizedError 
    21 from authkit.authorize import authorize_request as authkit_authorize_request 
    22  
    23 def authorize(permission): 
    24     """ 
    25     This is a decorator which can be used to decorate a Pylons controller action. 
    26     It takes the permission to check as the only argument and can be used with 
    27     all types of permission objects. 
    28     """ 
    29     def validate(func, self, *args, **kwargs): 
    30         def app(environ, start_response): 
    31             return func(self, *args, **kwargs) 
    32         return permission.check(app, request.environ, self.start_response) 
    33     return decorator(validate) 
    34  
    35 def authorize_request(permission): 
    36     """ 
    37     This function can be used within a controller action to ensure that no code  
    38     after the function call is executed if the user doesn't pass the permission 
    39     check specified by ``permission``. 
    40  
    41     .. Note :: 
    42  
    43         Unlike the ``authorize()`` decorator or 
    44         ``authkit.authorize.middleware`` middleware, this function has no 
    45         access to the WSGI response so cannot be used to check response-based 
    46         permissions.  Since almost all AuthKit permissions are request-based 
    47         this shouldn't be a big problem unless you are defining your own  
    48         advanced permission checks. 
    49     """ 
    50     authkit_authorize_request(request.environ, permission) 
    51  
    52 def authorized(permission): 
    53     """ 
    54     Similar to the ``authorize_request()`` function with no access to the 
    55     request but rather than raising an exception to stop the request if a 
    56     permission check fails, this function simply returns ``False`` so that you 
    57     can test permissions in your code without triggering a sign in. It can 
    58     therefore be used in a controller action or template. 
    59  
    60     Use like this:: 
    61  
    62         if authorized(permission): 
    63             return Response('You are authorized') 
    64         else: 
    65             return Response('Access denied') 
    66   
    67     """ 
    68     try: 
    69         authorize_request(permission) 
    70     except (NotAuthorizedError, NotAuthenticatedError): 
    71         return False 
    72     else: 
    73         return True 
    74  
     5import warnings 
     6warnings.warn( 
     7    """This module is deprecated. Please update your code to use ``authkit.authorize.pylons`` instead of this module.""",  
     8    DeprecationWarning,  
     9    2 
     10
     11from authkit.authorize.pylons import *