Python前夕 - 用户限制的资源访问功能项目的ID_FIELD作为AUTH_FIELD

问题描述:

我有一个用户的集合,我没有验证POST,所以用户可以创建帐户,现在我想限制访问说测试集合,用户只能创建一个测试文档,我将auth_field添加到user_id,并且想要将user_id作为field_id添加到文档中,同时将其用作auth_field,以用于读/写限制。Python前夕 - 用户限制的资源访问功能项目的ID_FIELD作为AUTH_FIELD

这是我的测试模型,我添加了PUT,因为用户有自己的ID,它应该用作test_item的id_field。

当我试着用这个运行Eve时,我有一个异常,有没有一种正确的方法,所以每个用户请求被正确认证并且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

+0

这就是我最终做的,谢谢。 –