金字塔:URL Dispatch(混合)应用程序中的资源树

sascha_p

我正在尝试在 URL Dispatch 应用程序中实现动态 ACL(包括行级安全性)。仅定义 Root factory 似乎还不够,因为我需要对每个安全端点执行单独的 authz 检查。我的设置如下(我使用金字塔文档mmerickel 的教程作为指导):

config.py

...
settings = config.registry.settings
config = Configurator(settings=settings, root_factory=RootPermissionFactory)
config.set_authentication_policy(CustomAuthenticationPolicy(settings))
config.set_authorization_policy(ACLAuthorizationPolicy())
...

views.py

#import ...

@view_defaults(renderer='json', permission='secured')
class RecordsView(object):
    ...

   @view_config(request_method='GET', route_name='records_by_id')
   def get(self):
        record = self.request.context.data
        if not record:
            return HTTPNotFound()
        return record

    @view_config(request_method='GET', route_name='records')
    def get_by_owners(self):
        owner_uids = self.request.params.mixed()['owner_uids']
        return records_service.get_records(owner_uids=owner_uids)

def includeme(config):
    config.add_route('records', '/records', factory=RecordsResource)
    config.add_route('records_by_id', 'records/{record_id}', factory=RecordFactory, traverse='{record_id}')

authorization.py

class RootPermissionFactory(object):
    __name__ = None
    __parent__ = None

    def __acl__(self):
        return [
            (Allow, 'authenticated_principal', 'secured'),
        ]

    def __init__(self, request):
        self.request = request


class RecordFactory(object):
    def __init__(self, request):
        self.request = request

    def __getitem__(self, key):
        record_data = records_service.get_record(key)
        owner = record_data.get('owner_uid')
        return RecordContext(self.request, owner, record_data)


class RecordContext(object):
    def __acl__(self):    
        owner_principal = 'u:{owner}'.format(owner=self.owner)
        return [
            (Allow, owner_principal, 'secured'),
        ]

    def __init__(self, request, owner, record_data):
        self.request = request
        self.owner = owner
        self.data = record_data


class RecordsResource(object):
    def __acl__(self):
        request_params = self.request.params.mixed()
        request_owner_uids = request_params['owner_uids']
        authorized_owner_uids = owners_api_service.get_authorized_owners(self.request.user['auth_data'])
        return [(Allow, 'authenticated_principal', 'secured')]\
            if set(authorized_owner_uids) == set(request_owner_uids) else []

    def __init__(self, request):
        self.request = request

我的问题如下:

  • 在没有模型层的情况下使用行级安全检查是否可以接受?即没有为 Records 数据设置 ORM,也没有将持久数据放入的普通模型,所以我必须使用“假”RecordContext类来附加__acl__规则并将数据传递给视图
  • /records尽管从遍历的角度来看它不是资源并且依赖于查询参数而不是路径部分,但端点视为资源是否可以接受
迈克尔·梅里克尔

我认为你的两个问题的答案都是:如果它对你有用,那么它是完全可以接受的。我发现很多成功地将 URL 视为资源作为通用模式,因为我有一些工具可以避免使用route_name. 例如:

config.add_route('records', '/records', factory=RecordsResource, use_global_views=True)
config.add_route('records_by_id', 'records/{record_id}', factory=RecordFactory, traverse='{record_id}', use_global_views=True)

@view_config(context=RecordsResource, renderer='json')
def records_view(request):
    return {}

@view_config(context=RecordContext, renderer='json')
def record_view(request):
    return {}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

引导程序中的金字塔网格

来自分类Dev

掌握金字塔中的WSGI应用程序

来自分类Dev

无法投放金字塔应用

来自分类Dev

简单的金字塔Java程序

来自分类Dev

在C中编写倒金字塔

来自分类Dev

金字塔中的多种认证策略

来自分类Dev

Java中的金字塔模式

来自分类Dev

R中的金字塔图

来自分类Dev

打印*的金字塔在Java中

来自分类Dev

C中的星号金字塔

来自分类Dev

在HTML中绑定JS金字塔

来自分类Dev

C中的简单金字塔

来自分类Dev

金字塔与数组中的每个项目

来自分类Dev

在Java中打印金字塔图案

来自分类Dev

在PHP中创建金字塔

来自分类Dev

金字塔+ SQLAlchemy中的db问题

来自分类Dev

在金字塔中显示Shibboleth属性

来自分类Dev

Perl 中的倒金字塔设计

来自分类Dev

在URL中使用查询字符串对金字塔/玉米资源进行单元测试

来自分类Dev

如何在金字塔中的URL中添加语言代码?

来自分类Dev

在Elastic Beanstalk上部署金字塔应用程序

来自分类Dev

金字塔开源Web应用程序

来自分类Dev

在Elastic Beanstalk上部署金字塔应用程序

来自分类Dev

在Highcharts中,如何为多金字塔图表中的每个金字塔设置标题

来自分类Dev

金字塔-静态应用程序无法在apache wsgi模块下找到静态资源

来自分类Dev

金字塔-静态应用程序无法在apswsgi模块下找到静态资源

来自分类Dev

将基础应用于此CSS金字塔

来自分类Dev

Java金字塔程序打印数字?

来自分类Dev

想要在星号中包含金字塔中的空间