【发布时间】:2014-02-12 01:45:34
【问题描述】:
如果我先添加路由,然后添加视图,如何根据 Pyramid 中的请求 matchdict 设置查看权限?我的意思是:
config.add_route('full_reg', '{base}/reg/{id}/full', factory=RegContextFactory)
config.add_view(view=RegCustomView, attr='full_reg', route_name='full_reg', request_method='GET',
permission=request.matchdict["base"])
当然,我那里没有“请求”对象,但我怎么能这样做呢?
编辑:添加代码。 以下是课程:
class RegContextFactory():
@property
def __acl__(self):
return [
(Allow, 'g:users', 'x'),
(Allow, 'g:users2', 'y'),
]
def __init__(self, request):
self.request = request
class RegCustomView():
def __init__(self, context, request):
self.context = context
self.request = request
def full_reg(self):
# if the user is not from users group, or the base param is not 'x',
#then this view should be forbidden
base = self.request.matchdict.get('base')
return Response('ok')
【问题讨论】:
-
请出示您的
RegCustomView班级。您误用了config.add_view的view参数。此外,通常不需要attr参数。查看以下资源:1) docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/… 2) michael.merickel.org/projects/pyramid_auth_demo/index.html 3) stackoverflow.com/questions/10266652/… 4) stackoverflow.com/questions/16169590/… -
我编辑了问题,放置了课程代码(出于目的而编辑)。就我而言,
attr参数是必需的,view参数不会被误用,因为代码工作正常。但这里真正的问题是:如果我是 'users' 组的用户,我应该拥有 'x' 权限,但这个 'x' 应该来自 matchdict。 -
抱歉,将您的视图参数误读为
RegContextFactory而不是RegCustomView -
这部分文档展示了如何使用 matchdict 返回适当的
Effective Principal: michael.merickel.org/projects/pyramid_auth_demo/… -
这个要点有帮助吗? gist.github.com/ianjosephwilson/8870333