我正在使用Loopback3。我遇到了ACL角色的问题,我不确定我做错了什么。我希望特定角色的用户能够将数据写入端点,并且出于某种原因,我设置的用户(位于其中一个角色中)无法写入。我得到一个需要授权的错误。环回ACL“需要授权”
我有4个角色:
- 管理
- 内部
- 外部
- 机器人
对于这个端点,所有授权用户可以读取数据,但只有管理员,内部和bot用户可以写入数据,并且只有管理员用户可以删除数据。
下面是我有我的ACL定义:
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "internal",
"permission": "ALLOW"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "bot",
"permission": "ALLOW"
},
{
"accessType": "DELETE",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
}
],
我有两个用户设置,一个是机器人,而另一个是管理员。当我为任何用户对API执行POST请求时,即使从浏览器界面进行操作,我也会得到'需要授权'错误。我可以做一个没有问题的GET,但是POST会失败。
如果我删除所有的“WRITE”ACL,并用它替换它们,做一个POST的作品。
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
所以,我可以做到这一点,但我不知道为什么我的自定义角色是失败。
编辑:这是我创建用户的方式,因为我实际上没有任何类型的界面。
module.exports = function (app) {
let today = new Date();
let admin = {
name: 'admin',
description: 'admin users',
created: today.toJSON(),
modified: today.toJSON()
};
let internal = {
name: 'internal',
description: 'Internal users',
created: today.toJSON(),
modified: today.toJSON()
};
let external = {
name: 'external',
description: 'external users',
created: today.toJSON(),
modified: today.toJSON()
};
let bot = {
name: 'bot',
description: 'robots',
created: today.toJSON(),
modified: today.toJSON()
};
let model = app.models.user;
model.create([
{username: 'bot', email: '[email protected]', password: 'test123'},
{username: 'admin', email: '[email protected]', password: 'test123'},
{username: 'iAdmin', email: '[email protected]', password: 'test123'},
{username: 'eUser', email: '[email protected]', password: 'test123'},
], function(err, users) {
if (err) throw err;
app.models.Role.create(bot, function (err, botRole) {
if (err) throw err;
botRole.principals.create({principalType: app.models.RoleMapping.user, principalID: users[0].id}, function(err, principal) {
if (err) throw err;
});
});
app.models.Role.create(admin, function (err, adminRole) {
if (err) throw err;
adminRole.principals.create({principalType: app.models.RoleMapping.user, PrincipalID: users[1].id}, function(err, principal) {
if (err) throw err;
});
});
app.models.Role.create(admin, function (err, internalRole) {
if (err) throw err;
internalRole.principals.create({principalType: app.models.RoleMapping.user, PrincipalID: users[2].id}, function(err, principal) {
if (err) throw err;
});
});
app.models.Role.create(external, function (err, externalRole) {
if (err) throw err;
externalRole.principals.create({principalType: app.models.RoleMapping.user, PrincipalID: users[3].id}, function(err, principal) {
if (err) throw err;
});
});
});
};
问题可能出在您创建角色并将其关联到用户的位置。 –
可能地,我已经添加了用于首先创建用户的脚本。 – Lisa
我相信他们应该是'app.models.RoleMapping.USER'而不是'app.models.RoleMapping.user' –