Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在hook之前将参数传递给falcon?_Python_Falconframework - Fatal编程技术网

Python 如何在hook之前将参数传递给falcon?

Python 如何在hook之前将参数传递给falcon?,python,falconframework,Python,Falconframework,我需要根据一些ROL授权用户,因此我需要: class Things: @falcon.before(myfunc, 'can_delete_tag') @on_get(req, resp): ... 但这似乎是不可能的。。。有什么想法吗?除非我们修补falcon的功能,否则使用内部falcon挂钩是不可能的。因为猎鹰钩住了所有的参数。但标准的装饰师可以做到这一点: def Authorize(action): def request_checked(f

我需要根据一些ROL授权用户,因此我需要:

class Things:
    @falcon.before(myfunc, 'can_delete_tag')
    @on_get(req, resp):
        ...

但这似乎是不可能的。。。有什么想法吗?

除非我们修补falcon的功能,否则使用内部falcon挂钩是不可能的。因为猎鹰钩住了所有的参数。但标准的装饰师可以做到这一点:

def Authorize(action):
    def request_checked(func):
        def _f(self, req, resp, *args, **kw):
            u = getUserInfoFromSession(req)
            if isAuthorizedTo(u.get('id'), action):
                return func(self, req, resp, *args, **kw)
            else:
                raise falcon.HTTPUnauthorized('Not Authorized', 'Permission Denied')
        return _f
    return request_checked
现在我们可以使用它:

class Things:
    @Authorize('can_delete_tag')
    @on_get(req, resp):
        ...

猎鹰1.3似乎也有同样的限制!这是一份工作

import falcon


class HasPermission(object):
    def __init__(self, role_name):
        self.role_name = role_name

    def validate_has_role(self, req, resp, resource, params):
        raise falcon.HTTPBadRequest('Bad request')
使用它

class Things:
    @Authorize('can_delete_tag')
@on_get(req, resp):
    ...

write_access = HasPermission("WRITE").validate_has_role
read_access = HasPermission("READ").validate_has_role

警告:这是一个老帖子了。Falcon 0.4.0更改了其内部结构,该代码可能无法工作。我在falcon 0.1.0中使用了这段代码。请注意,如果将validate_has_role()重命名为uuu call_uuu(),则可以将对象直接传递到@before。另一个选项是使用functools.partial()。