我跟随
security guidelines found on Pyramid docs以及wiki教程
Adding Authorization
现在我需要添加基于非单个用户而不是组的限制.
例如,假设任何博客编辑者都有权审阅所有评论,那么只有帖子作者可以编辑帖子本身.
对于我将在根ACL中执行的第一项任务,如下所示:
__acl__ = [ (Allow,Everyone,'view'),(Allow,Authenticated,'view_profile'),'groups:editor','edit_comment') ]
但是对于edit_post来说呢?
我已经阅读了this answer,但由于我不需要构建资源树,因此对我的需求似乎有些过分.
解决方法
你可能会让这太复杂了.首先,如果访问者是帖子的作者,则仅显示指向edit_post视图的链接.这将解决99%的问题,使该视图对于不应该看到它的人来说是不可见的.对于其他1% – 聪明的用户手动编辑URL以直接访问编辑视图 – 添加如下内容:
def edit_post(request): ... if authenticated_userid(request) != author: raise pyramid.httpexceptions.HTTPForbidden("You are not this post's author.")