Portal_membership.getAuthenticatedUser、portal_state.member 等.. 始终在 View 对象的 __init__ 和 publicTraversal 上返回匿名
我正在制作一个带有自定义遍历的视图。我想根据当前登录的用户动态遍历到下一个对象。但我知道的所有方法(portal_membership.getAuthenticatedUser、portal_state.member 等)始终在 View 对象的 __init__
和 publicTraversal
上返回匿名。它们正确返回的唯一情况是在对象的 __call__ 中。
我应该怎么做才能使 getAuthenticatedUser 对我的案例起作用(在视图的 __init__
或 publicTraversal
调用上)?
I'm making a View with customized traversal. I want to traverse to next object dynamically base on current logged in user. But all method I know (portal_membership.getAuthenticatedUser, portal_state.member etc..) always return anonymous on View object's __init__
and publicTraversal
. The only case they return right is in object's __call__
.
What should I do to make getAuthenticatedUser work on my case (either on view's __init__
or publicTraversal
call)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。由于 Zope 的安全模型,用户是在遍历之后确定的,因为此信息取决于已发布对象的上下文。
您只能在发布对象时依赖
getAuthenticatedUser
;遍历之后进行认证授权,然后发布,这在视图中意味着调用__call__
方法。由于
publicTraversal
是作为遍历阶段的一部分调用的,因此您也无法知道该方法中经过身份验证的用户。您必须重新考虑您的观点,不要依赖于该阶段已知的经过身份验证的用户。You can't. Due to Zope's security model, the user is determined after traversal, because this information depends on the context of the published object.
You can only rely on
getAuthenticatedUser
when the object is being published; after traversal, authentication and authorization take place, and then publication, which in views means invoking the__call__
method.Because
publicTraversal
is called as part of the traversal stage, you cannot know the authenticated user in that method either. You'll have to re-think your view to not rely on the authenticated user being known during that stage.您不应该在 browser:view 的
__init__
中执行任何操作,主要是因为您无法确定所有环境都已设置。值得注意的是,在您的情况下,您可以通过使用 __call__ 方法中的相同方法而不是 __init__ 中的相同方法来解决您的问题You shouldn't do anything in the
__init__
of your browser:view mainly because you cannot be sure that all the environment has been set up. Notably in your case, you can solve your issue by using the same methods in the__call__
method instead that in the__init__