Pyramid 中基于动态用户的授权

发布于 2024-11-18 16:50:57 字数 763 浏览 1 评论 0原文

我遵循在 Pyramid 文档上找到的安全准则以及维基教程 添加授权

现在我需要添加基于单个用户而不是组的限制。

举例来说,如果任何博客编辑都有权查看所有评论,则只有帖子作者可以编辑帖子本身

对于第一个任务,我的根 ACL 中将有这样的内容:

__acl__ = [ (Allow, Everyone, 'view'),
            (Allow, Authenticated, 'view_profile'),
            (Allow, 'groups:editor', 'edit_comment')
]

但是 edit_post 呢?

我已阅读这个答案,但对我来说似乎对我的需求来说太过分了,因为我不'不需要构建资源树。

I'm following security guidelines found on Pyramid docs along with wiki tutorial Adding Authorization

Now I need to add restrictions based un single user rather than groups.

Let's say for example that, if any blog editor can have permission to review all comments, only post author can edit the post itself.

For the first task I will have in my Root ACL like this:

__acl__ = [ (Allow, Everyone, 'view'),
            (Allow, Authenticated, 'view_profile'),
            (Allow, 'groups:editor', 'edit_comment')
]

but whay about for edit_post?

I've read this answer but seems overkill to me for my needs since I don't need to build a resource tree.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-11-25 16:50:57

通过在项目中创建 Root 资源,您已经拥有了一个“资源树”。您只需在其上为 posts 添加一个节点,该节点将返回一个带有特定 __acl__Post 对象,该对象仅包含授权用户 ID。然后,您可以让 edit_posts 路由使用 traverse='/posts/{post_id}' 来遍历资源树到 Post 对象,其中__acl__ 就可以了。

这并不困难,并且是让 Pyramid 为您做这些事情的方法。

如果您不想使用 permission 参数,您可以在视图本身内部进行授权,就像柯克建议的那样。

另外,如果您不喜欢这种添加 __acl__ 属性和遍历授权的方法,您可以实现自己的 AuthorizationPolicy 来执行您想要的操作给定的主体列表和权限。

Pyramid 的身份验证系统的要点在于它的存在,这很棒。 Pyramid 绝不要求您使用它,对于不使用它的视图,处理它不会对性能产生影响。

You already have a "Resource Tree" by creating the Root resource in your project. You just need to add a node on it for posts that will return a Post object with a particular __acl__ that contains only the authorized user id. You can then have your edit_posts route use traverse='/posts/{post_id}' to traverse your resource tree to the Post object with the __acl__ on it.

This isn't difficult, and is the way to have Pyramid do this stuff for you.

If you don't want to use the permission argument you can do the authorization inside of the view itself, like Kirk suggested.

Also, if you don't like this method of adding __acl__ properties and traversal for authorization, you can implement your own AuthorizationPolicy to do what you'd like it to do with a given list of principals and a permission.

The point of Pyramid's auth system is that it's there, which is great. Pyramid by no means requires you to use it and for views that don't use it, there is no performance impact of dealing with it.

蓬勃野心 2024-11-25 16:50:57

你可能把这件事搞得太复杂了。首先,如果访问者是帖子的作者,则仅显示指向 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.")

You might be making this too complicated. First, only show a link to the edit_post view if the visitor is the post's author. That will handle 99% of the problem by making that view invisible to people who shouldn't see it. For the other 1% - clever users hand-editing the URL to directly access the editing view - add something like this:

def edit_post(request):
    ...
    if authenticated_userid(request) != author:
        raise pyramid.httpexceptions.HTTPForbidden("You are not this post's author.")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文