【问题标题】:How flexible is Pyramids auth system?Pyramids 身份验证系统有多灵活?
【发布时间】:2012-02-25 21:27:30
【问题描述】:

我开始使用 Pyramid 框架(伟大的框架),我已经到了用户授权的地步。我想利用 ACL 来阻止已经登录的用户访问注册页面。显然,我可以通过其他方式做到这一点,但我想知道是否有任何方法可以使用金字塔中的工具来做到这一点。

我知道,通过向视图添加权限,不符合条件的用户将被显示为禁止视图。就我而言,我只是想重新路由已经是会员的用户远离不适用于他们的视图(注册、登录等)。

我尝试__acl__ = [(Deny, Authenticated, 'guest')] 无济于事,因为它阻止了所有用户的登录页面。

另外,在某种程度上,有没有办法动态地改变路线。我希望登录用户的主页与访客的主页不同。

【问题讨论】:

  • Pyramid 的身份验证系统非常灵活,这就是您难以理解的原因。我无法从 ACL 的单个 sn-p 中真正告诉您您做错了什么,但我可以告诉您,如果您打开 pyramid.debug_authentication,它将有助于了解您的身份验证策略返回了哪些主体。
  • 谢谢迈克尔。所以我假设我确实用我输入的 sn-p 朝着正确的方向前进?
  • 另外,当禁止视图被调用时,有没有办法获取被调用的初始请求,以便我可以采取相应的行动并重定向到正确的页面?
  • 我添加了一个可以解决您的问题的答案。

标签: python authorization acl pyramid


【解决方案1】:

您需要调查您的身份验证策略返回的主体,以了解发生了什么。很容易判断您是否在 INI 文件中打开了pyramid.debug_authorization。授权策略会将找到的 ACL 与通过 pyramid.security.effective_principals(request) 返回的主体进行比较。如果这些不匹配,应该清楚发生了什么。

实现基于表单的登录的方法是(假设 Pyramid 1.3a9+):

from pyramid.httpexceptions import HTTPSeeOther
from pyramid.security import authenticated_userid
from pyramid.view import forbidden_view_config

@forbidden_view_config()
def forbidden_view(request):
    if authenticated_userid(request):
        # user is already logged in, they are really forbidden
        return request.context # the forbidden 403 response

    url = request.route_url('login', _query={'came_from': request.path})
    return HTTPSeeOther(url)

这会将came_from 参数作为request.GET['came_from'] 添加到您的登录视图中的URL。当然,如果不存在,您可以在登录后将它们重定向到主屏幕。

【讨论】:

  • 对不起Michael,我设置的acl还是不行,不知道为什么。调试信息说:debug_authorization of url http://localhost:6543/register (view name u'' against context <occupeyed.security.RootFactory object at 0x3a62d50>): ACLDenied permission 'guest' via ACE '<default deny>' in ACL [('Allow', 'system.Authenticated', 'member'), ('Deny', 'system.Authenticated', 'guest')] on context <occupeyed.security.RootFactory object at 0x3a62d50> for principals ['system.Everyone']
  • 代码为:__acl__ = [(Allow, Authenticated, 'member'),(Deny, Authenticated, 'guest')]
  • 当我使用访客权限@view_config(route_name='register', renderer='register.mako', permission='guest') 时,我得到误报(阻止未经身份验证的用户注册)。
  • 您在哪里允许未经身份验证的用户“访客”权限?我没有看到任何地方之后 Deny 那里你(Allow, Everyone, 'guest')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-09
  • 1970-01-01
  • 2012-04-12
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
相关资源
最近更新 更多