限制金字塔默认需要登录

Restrict Pyramid to require login by default

我怎样才能使用金字塔,它需要用户登录,除非我明确表示它不需要特定视图?

目前我需要为我正在创建的每个 view_config 添加一些必需的权限。如果我忘记了一个,我可能会有安全风险。我希望反过来,在登录时添加一些免费访问标签等。如果我忘记了其中一个,我将有一个无法访问的页面,这比缺少保护更有可能被发现。

现在我需要一些权限参数并在我的 AuthPolicy 中进行一些检查

@view_config(route_name='my_route', renderer='my_templ.html', permission='view')
def view_foo(request):
    # ...

我想要一些东西,比如必须添加 permission=None 或者可能是第二个装饰器,比如 @public_access 之类的。

我目前实际上只对让登录用户可以访问我的所有视图感兴趣(而不是请求更具体的许可)。但是使用权限似乎是正确的方法。任何实现 "logged in only unless explicitly specified" 情况的建议将不胜感激。

使用 config.set_default_permission 为明确设置了 none 的视图设置权​​限。

这样你就可以

config.set_default_permission('private_view')

并限制 'private_view' 权限给经过身份验证的用户;然后明确允许未经身份验证的用户使用某些视图,例如登录。

另请注意:

If a default permission is in effect, view configurations meant to create a truly anonymously accessible view (even exception view views) must use the value of the permission importable as pyramid.security.NO_PERMISSION_REQUIRED. When this string is used as the permission for a view configuration, the default permission is ignored, and the view is registered, making it available to all callers regardless of their credentials.

否则访问您的 404、403 视图将被拒绝...