我有表在数据库中与相同的界面查看和编辑他们与金字塔应用程序。例如:
report表view记录路由示例:/birdreport/report/871;
report表编辑记录路由示例:/birdreport/report/871/edit;
report表的每条记录都有包含user_id的字段-该值与authenticated_userid函数返回的值相同。我很清楚如何通过添加查看权限来禁用对edit的访问。但是,我如何才能使访问edit视图只为那些用户I显示在相应的记录中?
发布于 2012-10-08 15:18:02
您可以通过在Report模型中定义__acl__()来使用Pyramid authorization policy。例如:
from sqlalchemy.orm import relationship, backref
from pyramid.security import Everyone, Allow
class Report(Base):
# ...
user_id = Column(Integer, ForeignKey('user.id'))
# ...
@property
def __acl__(self):
return [
(Allow, Everyone, 'view'),
(Allow, self.user_id, 'edit'),
]
# this also works:
#__acl__ = [
# (Allow, Everyone, 'view'),
# (Allow, self.user_id, 'edit'),
#]
class User(Base):
# ...
reports = relationship('Report', backref='user')上面的__acl__()将允许每个人调用您的视图view,但只有与Report相关的用户才能edit它。
很可能您尚未启用身份验证策略或授权策略,引用documentation
使用配置器的set_authorization_policy()方法启用授权策略。
您还必须启用身份验证策略才能启用授权策略。这是因为授权通常依赖于身份验证。在应用程序设置期间使用set_authentication_policy()和方法指定身份验证策略。
from pyramid.config import Configurator
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
authentication_policy = AuthTktAuthenticationPolicy('seekrit')
authorization_policy = ACLAuthorizationPolicy()
config = Configurator()
config.set_authentication_policy(authentication_policy)
config.set_authorization_policy(authorization_policy)以上配置启用了一种策略,该策略将在请求环境中传递的包含对单个主体的引用的“身份验证单”cookie的值与试图调用某个视图时在资源树中找到的任何ACL中存在的主体进行比较。
虽然可以混合和匹配不同的身份验证和授权策略,但使用身份验证策略配置一个金字塔应用程序是错误的,而没有授权策略,反之亦然。如果这样做,您将在应用程序启动时收到一个错误。
https://stackoverflow.com/questions/12776532
复制相似问题