Google appengine数据存储区查询的行级访问权限

时间:2015-01-22 16:48:23

标签: google-app-engine google-cloud-datastore acl endpoints-proto-datastore

我正在尝试在Google appengine数据存储区表上开发行级访问权限。到目前为止,我确实有一个使用_hooks的常规ndb put(),get()和delete()操作的工作示例。

所有其他表格都应使用类Acl。它被用作结构化财产。

class Acl(EndpointsModel):
    UNAUTHORIZED_ERROR = 'Invalid token.'
    FORBIDDEN_ERROR = 'Permission denied.'

    public = ndb.BooleanProperty()
    readers = ndb.UserProperty(repeated=True)
    writers = ndb.UserProperty(repeated=True)
    owners = ndb.UserProperty(repeated=True)

    @classmethod
    def require_user(cls):
        current_user = endpoints.get_current_user()
        if current_user is None:
            raise endpoints.UnauthorizedException(cls.UNAUTHORIZED_ERROR)
        return current_user

    @classmethod
    def require_reader(cls, record):
        if not record:
            raise endpoints.NotFoundException(record.NOT_FOUND_ERROR)
        current_user = cls.require_user()
        if record.acl.public is not True or current_user not in record.acl.readers:
            raise endpoints.ForbiddenException(cls.FORBIDDEN_ERROR)

我确实希望保护对Location类的访问。所以我确实在课堂上添加了三个钩子(_post_get_hook,_pre_put_hook和_pre_delete_hook)。

class Location(EndpointsModel):
    QUERY_FIELDS = ('state', 'limit', 'order', 'pageToken')
    NOT_FOUND_ERROR = 'Location not found.'

    description = ndb.TextProperty()
    address = ndb.StringProperty()
    acl = ndb.StructuredProperty(Acl)

    @classmethod
    def _post_get_hook(cls, key, future):
        location = future.get_result()
        Acl.require_reader(location)

    def _pre_put_hook(self):
        if self.key.id() is None:
            current_user = Acl.require_user()
            self.acl = Acl()
            self.acl.readers.append(current_user)
            self.acl.writers.append(current_user)
            self.acl.owners.append(current_user)
        else:
            location = self.key.get()
            Acl.require_writer(location)

这适用于所有创建,读取,更新和删除操作,但它不适用于查询。

@Location.query_method(user_required=True,
                       path='location', http_method='GET', name='location.query')
def location_query(self, query):
    """
    Queries locations
    """
    current_user = Acl.require_user()
    query = query.filter(ndb.OR(Location.acl.readers == current_user, Location.acl.public == True))
    return query

当我针对所有位置运行查询时,收到以下错误消息:

BadArgumentError: _MultiQuery with cursors requires __key__ order

现在我有一些问题:

  • 如何解决_MultiQuery问题?
  • 修复后:这个Acl实现有意义吗?有开箱即用的替代品吗? (我希望将Acl存储在记录本身上,以便能够运行直接查询,而无需先获取密钥。)

1 个答案:

答案 0 :(得分:2)

数据存储本身不支持OR过滤器。相反,NDB在幕后做的是运行两个查询:

 query.filter(Location.acl.readers == current_user)
 query.filter(Location.acl.public == True)

然后将这两个查询的结果合并为一个结果集。为了正确合并结果(特别是在重复属性时消除重复),在从任意位置继续查询时(使用游标),需要通过键对查询进行排序。

为了成功运行查询,您需要在运行之前将密钥顺序附加到查询中:

def location_query(self, query):
"""
Queries locations
"""
current_user = Acl.require_user()
query = query.filter(ndb.OR(Location.acl.readers == current_user,
                            Location.acl.public == True)
                    ).order(Location.key)
return query

不幸的是,您的ACL实现不适用于查询。特别是,不会为查询结果调用_post_get_hook。有bug filed on the issue tracker about this

相关问题