decorators.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:lxc-rest 作者: lxc-webpanel 项目源码 文件源码
def user_has(ability, get_user=import_user):
    """
    Takes an ability (a string name of either a role or an ability) and returns the function if the user has that ability
    """
    def wrapper(func):
        @wraps(func)
        def inner(*args, **kwargs):
            desired_ability = Ability.query.filter_by(
                name=ability).first()
            user_abilities = []
            current_identity = get_user()
            for group in current_identity._groups:
                user_abilities += group.abilities
            if desired_ability.id in user_abilities or current_identity.admin:
                return func(*args, **kwargs)
            else:
                raise Forbidden("You do not have access")
        return inner
    return wrapper
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号