Python eve - 项目 ID_FIELD 为 AUTH_FIELD 的用户限制资源访问功能
Python eve - User-Restricted Resource Access feature with item's ID_FIELD as AUTH_FIELD
我有一组用户,我在没有身份验证的情况下将其打开 POST 这样用户就可以创建帐户,现在我想限制对测试集合的访问,用户只能创建一个测试文档,我将auth_field添加到user_id,我想将user_id的文档添加为field_id,同时将其用作[=32] =],用于 read/write 限制。
这是我的测试模型,我添加了 PUT 因为用户有自己的 ID,它应该用作 test_item id_field.
当我尝试 运行 前夕,我有一个例外,有没有办法正确地做到这一点,所以每个用户请求都经过正确的身份验证并且 auth_field 设置为user_id 将透明地工作 ?
谢谢你的帮助。
tests = {
'resource_methods': ['GET'],
'upsert_on_put': True,
'id_field': 'test_id'
'item_title': 'test',
'auth_field': 'test_id',
'item_methods': ['GET', 'PATCH', 'PUT'],
'schema': {
'test_field': {
'type': 'string',
'required': True
}
}
}
异常:
eve.exceptions.ConfigException: "tests": auth_field cannot be set to id_field (test_id)
TL;DR
从用户和测试集合做一对一的关系,每个用户有一个测试,通过auth_field认证后透明工作。
如果您使用的是您提到的用户受限资源访问权限,则可以使用 before insert event hook 执行此 1:1 关系。因为那样的话,您将在文件上有一个 auth_field。在我的示例中,授权字段是 user_id
.
你的 on_insert_tests
钩子会像这样
from flask import current_app, abort
def check_inserted(documents):
# get ID for current user
user_id = current_app.auth.get_request_auth_value()
# find tests for the current user
collection = current_app.data.driver.db['tests']
tests = collection.find({'user_id': user_id})
if tests.count() > 0:
abort(409, 'Test already present for this account. Only one allowed.')
所以当为当前用户插入第二个测试时,它将中止。
顺便说一下,我不明白为什么要将测试中的 ID 字段更改为 test_id
,而不是使用默认值 _id
。
我有一组用户,我在没有身份验证的情况下将其打开 POST 这样用户就可以创建帐户,现在我想限制对测试集合的访问,用户只能创建一个测试文档,我将auth_field添加到user_id,我想将user_id的文档添加为field_id,同时将其用作[=32] =],用于 read/write 限制。
这是我的测试模型,我添加了 PUT 因为用户有自己的 ID,它应该用作 test_item id_field.
当我尝试 运行 前夕,我有一个例外,有没有办法正确地做到这一点,所以每个用户请求都经过正确的身份验证并且 auth_field 设置为user_id 将透明地工作 ?
谢谢你的帮助。
tests = {
'resource_methods': ['GET'],
'upsert_on_put': True,
'id_field': 'test_id'
'item_title': 'test',
'auth_field': 'test_id',
'item_methods': ['GET', 'PATCH', 'PUT'],
'schema': {
'test_field': {
'type': 'string',
'required': True
}
}
}
异常:
eve.exceptions.ConfigException: "tests": auth_field cannot be set to id_field (test_id)
TL;DR
从用户和测试集合做一对一的关系,每个用户有一个测试,通过auth_field认证后透明工作。
如果您使用的是您提到的用户受限资源访问权限,则可以使用 before insert event hook 执行此 1:1 关系。因为那样的话,您将在文件上有一个 auth_field。在我的示例中,授权字段是 user_id
.
你的 on_insert_tests
钩子会像这样
from flask import current_app, abort
def check_inserted(documents):
# get ID for current user
user_id = current_app.auth.get_request_auth_value()
# find tests for the current user
collection = current_app.data.driver.db['tests']
tests = collection.find({'user_id': user_id})
if tests.count() > 0:
abort(409, 'Test already present for this account. Only one allowed.')
所以当为当前用户插入第二个测试时,它将中止。
顺便说一下,我不明白为什么要将测试中的 ID 字段更改为 test_id
,而不是使用默认值 _id
。