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

时间:2016-05-10 13:59:47

标签: python pyramid

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

1 个答案:

答案 0 :(得分:0)

Websauna框架有一个例子:

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))
相关问题