Changeset 5743

Show
Ignore:
Timestamp:
11/22/08 08:26:53 (2 months ago)
Author:
Gustavo
Message:
  • Added *tons* of tests to verify the integration of repoze.who and repoze.what.
  • Moved the @require decorator from repoze.what into TG itself.
  • Synchronized the used services of repoze.what according to the latest API changes to make it independent of TG.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tg/controllers.py

    r5634 r5743  
    515515        errors = [] 
    516516        environ = pylons.request.environ 
    517         identity = environ.get('repoze.who.identity') 
    518517        if not hasattr(self, "require") or \ 
    519518            self.require is None or \ 
    520             self.require.eval_with_object(identity, errors): 
     519            self.require.eval_with_environ(environ, errors): 
    521520            return True 
    522521 
  • trunk/tg/decorators.py

    r5544 r5743  
    1010from decorator import decorator 
    1111 
     12from webob.exc import HTTPUnauthorized 
    1213from webob.multidict import MultiDict 
    1314from webhelpers.paginate import Page 
    14 from tg.configuration import Bunch 
    1515# this can't be tg, as we are circular importing then! 
    1616from pylons import config, request 
    1717from pylons import tmpl_context as c 
    1818from util import partial 
     19from repoze.what.authorize import check_authorization, NotAuthorizedError 
     20 
     21from tg.configuration import Bunch 
     22from tg.flash import flash 
    1923 
    2024class Decoration(object): 
     
    208212            self.content_type, self.engine, self.template, self.exclude_names) 
    209213        return func 
     214 
    210215 
    211216def override_template(controller, template): 
     
    344349    s.commit = old_commit 
    345350    return retval 
    346      
    347  
     351 
     352 
     353def require(predicate): 
     354    """ 
     355    Make repoze.what verify that the predicate is met. 
     356     
     357    @param predicate: A repoze.what predicate. 
     358    @return: The decorator that checks authorization. 
     359     
     360    """ 
     361     
     362    @decorator 
     363    def check_auth(func, *args, **kwargs): 
     364        environ = request.environ 
     365        try: 
     366            check_authorization(predicate, environ) 
     367        except NotAuthorizedError, e: 
     368            flash(e.errors, status="status_error") 
     369            raise HTTPUnauthorized() 
     370        return func(*args, **kwargs) 
     371     
     372    return check_auth 
     373 
  • trunk/tg/__init__.py

    r5418 r5743  
    5454from tg.controllers import TGController, redirect, url, use_wsgi_app 
    5555from tg.configuration import config 
    56 from tg.decorators import validate, expose, override_template, paginate, postpone_commits 
     56from tg.decorators import validate, expose, override_template, paginate, \ 
     57                          postpone_commits, require 
    5758from tg.flash import flash, get_flash, get_status 
    5859 
     
    6263    'expose', 'validate', 'TGController', 'tmpl_context', 'app_globals', 
    6364    'overide_template', 'request', 'response', 'session','TurboGearsApplication', 
    64     'use_wsgi_app', 'TGApp', 'app_globals' 
     65    'use_wsgi_app', 'TGApp', 'app_globals', 'require' 
    6566]