【问题标题】:HTTPForbidden context route not firingHTTPForbidden 上下文路由未触发
【发布时间】:2013-01-20 05:10:03
【问题描述】:

我在 Pyramid 中的安全性正常工作时遇到了一些麻烦。我的安全本身似乎在起作用:如果用户尝试访问他们无权查看的资源,Pyramid 会抛出 HTTPForbidden 异常。问题在于,在这种情况下,它应该退回到登录视图,而这并没有发生。我只是通过堆栈跟踪获得默认的 Pyramid 异常屏幕。

我的登录视图:

from pyramid.httpexceptions import HTTPForbidden

@view_config(context = HTTPForbidden, renderer="login.mak")
@view_config(route_name = 'login', renderer='login.mak')
class Login(ViewBase):
    def __init__(self, request):
        super(Login, self).__init__(request)
        self.data['title'] = "Login"

        if request.method == 'POST':
            name = request.params['username']
            passwd = request.params['password']
            validuser = User.check(name, passwd)
            if validuser is None:
                self.data['requested_path'] = request.params['requestpath']
                self.__call__()
            else:
                headers = remember(request, str(validuser.id))
                raise HTTPFound(
                    location = request.params['requestpath'],
                    headers = headers
                    )
        else:
            self.data['requested_path'] = request.url

    def __call__(self):
        return self.data

所有视图的默认权限设置为“视图”,我的acl 类如下所示:

from pyramid.security import Allow
from pyramid.security import Everyone
from pyramid.security import Authenticated
from pyramid.security import ALL_PERMISSIONS

# Handles ACL auth for the entire application

class RootFactory(object):

    __acl__ = [
        (Allow, Everyone, 'view'),
        (Allow, 'Editor', 'edit'),
        (Allow, 'Admin', ALL_PERMISSIONS)
    ]

    def __init__(self, request):
        pass

def groupfinder(userid, request):

    from ctic.models import User

    user = User.get(userid)
    member_groups = []
    if user != None:
        member_groups.append(user.group.groupname)
        return member_groups
    else:
        return None

正如我所说,ACL 方面似乎正在发挥作用。

有趣的是,如果我从 init.py 中删除 default_permission,一切都会正常进行。

任何关于我哪里出错的指针将不胜感激。

【问题讨论】:

    标签: python authentication pyramid


    【解决方案1】:

    这可能与您的问题无关,但基于类的视图在 Pyramid 中的工作方式是一个两步过程。 1) Pyramid 使用request 对象实例化您的类 2) Pyramid 调用__call__ 方法或attrview_config 中指定的方法。因此,您的__init__ 中对self.__call__() 的调用不正确。

    您使用基于类的视图的方式有点不合常规,但我看不出您在此处粘贴的内容中有任何实际错误。

    【讨论】:

      【解决方案2】:

      您的groupfinder() 在所有情况下都应该返回一个列表。如果用户不在任何组中,则返回 [] 而不是 None

      我不确定这是否是您的问题。但是当我返回 None 而不是 [] 时,我遇到了类似的行为。

      【讨论】:

      • “在所有情况下”都不正确。 groupfinder 有 2 个用途:1) 确定用户是否经过身份验证 - 如果用户未登录,则返回 None,如果登录则返回列表 2) 确定用户拥有哪些主体 - 这意味着填充返回的列表与系统中的 ACL 匹配的主体。他的示例中的 groupfinder 正确地做到了这一点。
      【解决方案3】:

      您可能希望将“permission=NO_PERMISSION_REQUIRED”添加到您的 view_config 中

      from pyramid.security import NO_PERMISSION_REQUIRED
      @view_config(context = HTTPForbidden, renderer="login.mak", permission=NO_PERMISSION_REQUIRED)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 2018-07-07
        • 1970-01-01
        • 2017-03-01
        相关资源
        最近更新 更多