python - 将 RoleNeed 与 Flask Principal 相结合

标签 python permissions flask acl flask-principal

我正在尝试创建一个权限,要求用户具有角色 A 或角色 B。

根据 Python Flask Principal文档中,以下行创建了一个权限,要求用户具有角色 A 和角色 B。

combined_permission = Permission(RoleNeed('roleA'), RoleNeed('roleB'))

您知道如何使用 OR 而不是 AND 创建权限吗?

最佳答案

就目前而言,需要使用 OR 检查组合权限的需求。引自 the documentation :

class flask_principal.Permission(*needs)
Represents needs, any of which must be present to access a resource

如果您想要一个 AND,只需将 Permission 子类化并覆盖 the allows method首先检查集合的交集是否与权限的需求相同:

class RequiresAll(Permission):
    def allows(self, identity):
        if not has_all(self.needs, identity.provides):
            return False

        if self.excludes and self.excludes.intersection(identity.provides):
            return False

        return True

def has_all(needed=None, provided=None):
    if needed is None:
        return True

    if provided is None:
        provided = set()

    shared = needed.intersection(provided)
    return shared == needed

关于python - 将 RoleNeed 与 Flask Principal 相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29500333/

相关文章:

WordPress,允许评论者编辑自己的评论吗?

mysql根据字段值自定义用户权限

debugging - VBS 找不到目录 - 'Path Not Found'

python - 为我的 python 脚本创建 WEB UI

python - SQLAlchemy 插入数据与关联表的多对多关系

python - 从正则表达式字符集生成随机字符串

python - 未选中时未提交 Django 复选框

python - 在 pip 中为特定包指定 repo

python - 潜在语义索引如何用于特征选择?

python - flask 应用程序 : The requested URL was not found on the server