我想知道是否有人可以帮助我,我无法找到为什么我的'admin'角色在Loopback 3.x中不起作用,我正在尝试以下代码:
script.js-为在postgres db中创建管理员角色,这将正确创建角色和映射
module.exports = function (app) {
var User = app.models.User;
var Role = app.models.Role;
var RoleMapping = app.models.RoleMapping;
User.create([
{username: 'Test', email: '[email protected]', password: 'test'},
], function(err, users) {
if (err) throw err;
console.log('Created users:', users);
//create the admin role
Role.create({
name: 'admin'
}, function(err, role) {
if (err) throw err;
console.log('Created role:', role);
role.principals.create({
principalType: RoleMapping.USER,
principalId: users[0].id
}, function(err, principal) {
if (err) throw err;
console.log('Created principal:', principal);
});
});
});
}
然后authentication.js-
server.enableAuth({ datasource: 'ds' });
在testModel.json中,acls如下:
"acls": [
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
}
]
NOTE-我只想将写权限授予ADMIN。
但是该模型的POST api仍然可供所有人使用,请在错误的地方纠正我!
提前感谢!
最后,我通过以下方式应用acls得到了答案:
"acls": [
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
}
],
我只是先限制所有用户,然后才授予Admin权限!
这种方式对我有用,如果他们的方法更好,可以纠正我:)