在羽毛中,目标是限制在某个服务上访问的数据仅限于当前登录用户所拥有的数据。
假设我使用的是羽毛身份验证,此服务上可用的数据存储在数据库表中,并且包含用户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: []
}
};这似乎是可行的,但由于我是一个羽毛菜鸟,我想我最好先检查一下,然后才把它放进野外,以确保没有我不知道的情况会导致泄漏。
发布于 2017-09-19 09:01:10
作为一个玩具新手,对羽毛和表达,我不确定。现在,所有的工作如上所述。
旧答案
对于remove,我使用了restrictToOwner。(我也认为patch和update是因为它们对现有数据进行操作。不过,我没有对此进行测试。否则,我就可以通过指定id来交叉删除数据。也许你也可以检查一下是否也是这样。
这是测试用例:
测试代码:
非常感谢,你的帖子对我真的很有帮助!
https://stackoverflow.com/questions/44091808
复制相似问题