LoopBack 的默认 ACL 比您定义的更具体,因此您的默认 ACL 最终不会生效。 @authenticated 和 @unauthenticated ALLOW 规则不优先于 DENY 所有规则。但是自定义角色可以,并且使用自定义ADMINISTRATOR 角色是框架中的正确方式。
- 您需要为特定用户创建角色。
- 使用 RoleMapping 模型将该角色映射到用户。
步骤 1 和 2 可以使用此引导脚本完成(例如:App/server/boot/create-admin-user.js):
module.exports = function(app) {
var User = app.models.ExtendedUser;
var Role = app.models.Role;
var RoleMapping = app.models.RoleMapping;
User.findOrCreate({ where: { username: 'admin', email: 'admin@admin.com' } },
{
username: 'admin',
email: 'admin@admin.com',
password: 'admin123'
},
function(err, user) {
if (err) return console.log(err);
// Create the admin role
Role.findOrCreate({where: { name: 'ADMINISTRATOR' }},
{ name: 'ADMINISTRATOR' },
function(err, role) {
if (err) return debug(err);
console.log("Role Created: " + role.name);
// Assign admin role
RoleMapping.findOrCreate({where: { roleId: role.id, principalId: user.id }},
{ roleId: role.id, principalId: user.id, principalType: RoleMapping.USER },
function(err, roleMapping) {
if (err) return console.log(err);
console.log("ADMINISTRATOR Role assigned to " + user.username);
});
});
});
};
- 在您的
ExtendedUser 模型中创建一个 ACL 条目以允许角色 ADMINISTRATOR 写入:
```
{
"name": "ExtendedUser",
"base": "User",
/* ... */
"acls": [
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "ADMINISTRATOR",
"permission": "ALLOW"
}
]
}