我完成了创建新的自定义角色的成功,被称为softOwner
。万一有人需要它,这里是代码:
module.exports = function(app) {
var Role = app.models.Role;
Role.registerResolver('softOwner', function(role, context, cb) {
function reject(err) {
if(err) {
return cb(err);
}
cb(null, false);
}
var userId = context.accessToken.userId;
if (!userId) {
return reject(); // do not allow anonymous users
}
context.model.findById(context.modelId, function(err, model) {
if(err || !model) {
reject(err);
}
if(model.userId) {
// if there is an owner, check that it is the right owner
(model.userId == context.accessToken.userId) ? cb(null, true) : reject();
} else {
// if there is no owner, authorize
cb(null, true);
}
});
});
};
然后只需添加它的ACL:
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "softOwner",
"permission": "ALLOW"
},
如何设置model.userId?您是否在模型中创建了属性,然后在创建时将其分配给中间件?或者是使用$ owner属性为你做这个事情吗?请参阅http://stackoverflow.com/questions/39970066/loopback-io-model-acl-principalid-owner – steve76
你只需要设置一个属于与用户模型的关系,并且Loopback处理它! – Ghislaindj
是的,但功能有限,就像你需要在请求中指定帖子ID一样。 GET所有记录都不适用于权限,无论是批量更新。 – steve76