如何给权限特定的用户python金字塔?

2024-04-26 09:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

在python金字塔框架中,我可以在对象级别授予权限,但如何在对象级别授予权限。我有组管理员和版主,但在版主组中,一个用户将额外授予删除权限,而在管理组中,一个用户将获得比管理员更少的角色。在


Tags: 对象用户框架权限角色管理员级别版主
1条回答
网友
1楼 · 发布于 2024-04-26 09:23:17

WebSUNA框架有一个示例来执行以下操作:

https://websauna.org/docs/narrative/crud/standalone.html#creating-crud-resources

复制粘贴答案以满足Stackoverflow过度热心主持人的深层愿望:

class ContractResource(Resource):
    """Map one TokenContract SQLAlchemy model instance to editable CRUD resource."""

    # __acl__ can be callable or property.
    # @reify caches the results after the first call
    @reify
    def __acl__(self) -> List[tuple]:
        # Give the user principal delete access as the owner
        # The returned list overrides __acl__ from the parent level
        # (ContractCRUD in our case)
        # See websauna.system.auth.principals for details
        contract = self.get_object()  # type: TokenContract
        if contract.owner:
            owner_principal = "user:{}".format(contract.owner.id)
            return [(Allow, owner_principal, "delete")]
        else:
            return []

    def get_title(self):
        token_contract = self.get_object()
        return "Smart contract {}".format(bin_to_eth_address(token_contract.contract_address))

相关问题 更多 >