Writing a CherryPy Decorator for Authorization

后端 未结 2 1150
清歌不尽
清歌不尽 2021-02-09 15:30

I have a cherrypy application and on some of the views I want to start only allowing certain users to view them, and sending anyone else to an authorization required page.

2条回答
  •  情话喂你
    2021-02-09 15:58

    Ok, in that case your decorator would look something like this:

    # without any parameters
    def authentication_required(f):
        @functools.wraps(f)
        def _authentication_required(*args, **kwargs):
            # Do you login stuff here
            return f(*args, **kwargs)
        return _authentication_required
    
    # With parameters
    def authentication_required(*allowed_groups):
        def _authentication_required(f):
            @functools.wraps(f)
            def __authentication_required(*args, **kwargs):
                # Do you login stuff here
                return f(*args, **kwargs)
            return __authentication_required
        return _authentication_required
    

提交回复
热议问题