gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:21:50 26 4
gpt4 key购买 nike

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

假设我使用的是 Feathers 认证,这个服务上可用的数据存储在一个数据库表中,包含用户 ID 的表列称为 user_id ,这个钩子(Hook)能达到目的吗?

如果不是,那么需要改变什么?

如果能够回答这个问题很重要,那么我将使用 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: []
}
};

它似乎有效,但由于我是 Feathers 菜鸟,我想我最好在将其投入野外之前检查一下,以确保没有我不知道会导致泄漏的情况。

最佳答案

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

旧答案

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

这是测试用例:

  • 用户 1 创建一个模型对象
  • 用于检查授权的用户 ID
  • 标识对象的对象 ID
  • 用户 2 删除具有对象 id 的对象
  • 测试正常:预期 404
  • 测试失败:204 或 200 工作
  • 用户 1 尝试获取对象
  • 测试正常:对象在那里,200
  • 测试失败:对象不存在,404

  • 测试代码:
  • test_cannot_cross_delete

  • 非常感谢,您的帖子对我很有帮助!

    关于hook - Feathers - 限制服务响应仅限当前用户拥有的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44091808/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com