强制执行 Feathers.js 资源的访问权限
Enforce access rights on Feathers.js resources
如何限制对通过 Feathers.js 公开的资源子集的访问权限?
例如,您如何将 CRUD 操作限制为用户的 tenantId 或 groupId?
到目前为止,我已经在某种程度上使用了服务挂钩。下面的代码适用于 "get/find" 但不适用于 "put/update"。跟踪生成的 mongo 查询,在 "update" 场景中仅查询“_id”。我的查询似乎被忽略或覆盖了。
// my-resource.hooks.js
module.exports = {
before: {
all: [
function(hook) {
const userGroupId = hook.params.userGroupId;
// NOTE: userGroupId gets extracted via an Express js hook from auth/header
hook.params.query = {
...hook.params.query,
userGroupId
}
return hook;
}
]
}
}
更新
到目前为止,我已经扩展了 feathers-mongoose 服务实现并更新了这段代码。似乎可以解决问题,但我仍然想知道我是否遗漏了什么。
_get(id, params = {}) {
params.query = params.query || {};
const discriminator = (params.query || {})[this.discriminatorKey] || this.discriminatorKey;
const model = this.discriminators[discriminator] || this.Model;
let modelQuery = model
.findOne({
[this.id]: id,
...params.query
});
为了知道是否允许用户修改单个资源,最好的方法是先检索它,检查权限并抛出 Feathers error 如果不允许:
const errors = require('feathers-errors');
// my-resource.hooks.js
module.exports = {
before: {
all: [
function(hook) {
// NOTE: userGroupId gets extracted via an Express js hook from auth/header
const userGroupId = hook.params.userGroupId;
// If there is an id, get the entry first to check the permission
if(hook.id !== undefined && hook.id !== null) {
return hook.service.get(hook.id).then(entry => {
if(entry.userGroupId !== userGroupId) {
throw new errors.Forbidden('You are not allowed to access this');
}
return hook;
});
}
// Otherwise just restrict the query
hook.params.query = {
...hook.params.query,
userGroupId
}
return Promise.resolve(hook);
}
]
}
}
如何限制对通过 Feathers.js 公开的资源子集的访问权限?
例如,您如何将 CRUD 操作限制为用户的 tenantId 或 groupId?
到目前为止,我已经在某种程度上使用了服务挂钩。下面的代码适用于 "get/find" 但不适用于 "put/update"。跟踪生成的 mongo 查询,在 "update" 场景中仅查询“_id”。我的查询似乎被忽略或覆盖了。
// my-resource.hooks.js
module.exports = {
before: {
all: [
function(hook) {
const userGroupId = hook.params.userGroupId;
// NOTE: userGroupId gets extracted via an Express js hook from auth/header
hook.params.query = {
...hook.params.query,
userGroupId
}
return hook;
}
]
}
}
更新
到目前为止,我已经扩展了 feathers-mongoose 服务实现并更新了这段代码。似乎可以解决问题,但我仍然想知道我是否遗漏了什么。
_get(id, params = {}) {
params.query = params.query || {};
const discriminator = (params.query || {})[this.discriminatorKey] || this.discriminatorKey;
const model = this.discriminators[discriminator] || this.Model;
let modelQuery = model
.findOne({
[this.id]: id,
...params.query
});
为了知道是否允许用户修改单个资源,最好的方法是先检索它,检查权限并抛出 Feathers error 如果不允许:
const errors = require('feathers-errors');
// my-resource.hooks.js
module.exports = {
before: {
all: [
function(hook) {
// NOTE: userGroupId gets extracted via an Express js hook from auth/header
const userGroupId = hook.params.userGroupId;
// If there is an id, get the entry first to check the permission
if(hook.id !== undefined && hook.id !== null) {
return hook.service.get(hook.id).then(entry => {
if(entry.userGroupId !== userGroupId) {
throw new errors.Forbidden('You are not allowed to access this');
}
return hook;
});
}
// Otherwise just restrict the query
hook.params.query = {
...hook.params.query,
userGroupId
}
return Promise.resolve(hook);
}
]
}
}