Feathers - 仅限当前用户拥有的数据的服务响应

时间:2017-05-20 23:35:38

标签: hook feathersjs

在Feathers中,目标是将特定服务上可访问的数据限制为仅由当前登录用户拥有的数据。

假设我正在使用Feathers身份验证,此服务上可用的数据存储在数据库表中,并且包含用户ID的表列名为user_id,此钩子是否会达到目标?

如果没有那么需要改变什么?

如果能够回答问题很重要,那么我正在使用Sequelize和Postgres。

const { authenticate } = require('feathers-authentication').hooks;

const { queryWithCurrentUser } = require('feathers-authentication-hooks');
const { associateCurrentUser } = require('feathers-authentication-hooks');

const readRestrict = [
  queryWithCurrentUser({
    idField: 'id',
    as: 'user_id'
  })
];

const modRestrict = [
  associateCurrentUser({
    idField: 'id',
    as: 'user_id'
  })
];

module.exports = {
  before: {
    all:    [ authenticate('jwt') ],
    find:   [ ...readRestrict ],
    get:    [ ...readRestrict ],
    create: [ ...modRestrict ],
    update: [ ...modRestrict ],
    patch:  [ ...modRestrict ],
    remove: [ ...modRestrict ]
  },

  after: {
    all:    [],
    find:   [],
    get:    [],
    create: [],
    update: [],
    patch:  [],
    remove: []
  },

  error: {
    all:    [],
    find:   [],
    get:    [],
    create: [],
    update: [],
    patch:  [],
    remove: []
  }
};

它似乎有效,但由于我是羽毛菜鸟,我认为我最好先检查一下,然后确保没有我不知道会导致泄漏的情况。

1 个答案:

答案 0 :(得分:0)

作为羽毛和表达的totoal初学者,我不确定。现在,所有工作如上所述。

旧答案

对于remove,我使用了restrictToOwner。 (我也认为patchupdate因为它们对现有数据进行操作。但我没有测试过。) 否则,我可以通过指定id来交叉删除数据。也许你可以检查一下你是否也是这种情况。

这是测试用例:

  1. 用户1使用创建模型对象
    • 用于检查授权的用户ID
    • 用于标识对象的对象ID
  2. 用户2删除具有对象ID的对象
    • 测试ok:404预期
    • 测试失败:204或200工作
  3. 用户1尝试获取该对象
    • 测试ok:对象在那里,200
    • 测试失败:对象不存在,404
  4. 测试代码:

    非常感谢,你发帖对我很有帮助!