2016-05-10 89 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))