2016-05-10 66 views
1

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

+0

如果你不考虑做使用细化的权限,我认为你可以做的是将超级管理员和经理添加到你的组中。然后,例如在你的评论方法中,你可以设置(权限=“Moderator,SuperModerator”)和你的删除方法设置(权限=“SuperModerator”) – webjunkie

回答

0

Websauna框架有一个例子来做到这一点:

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

的复制粘贴的答案,以满足#1过激版主的深层欲望:

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))