如何使用Loopback ACL修改用户angular色的权限

我想了解回环acl但失败,如果我可以使用回环acl控制angular色授权,我该怎么办?

当我得到请求

GET http://localhost:1337/api/Employees 401 (Unauthorized) { "error": { "name": "Error", "status": 401, "message": "Authorization Required", "statusCode": 401, "stack": "Error: Authorization Required } } 

这是一名雇员。 JSONconfiguration

 { "name": "Employee", "base": "User", "properties": { "nickname": { "type": "string" } }, "validations": [], "relations": {}, "acls": [ { "principalType": "ROLE", "principalId": "admin", "permission": "ALLOW", "accessType": "READ" } ], "methods": [] } 

以下代码是添加一个员工

 { "nickname": "", "realm": "", "username": "", "credentials": "object", "challenges": "object", "email": "", "emailVerified": false, "verificationToken": "", "status": "", "created": "", "lastUpdated": "", "id": 0 } 

我不知道loopback acls的内部。 我如何去改变要实现访问控制效果?

要支持像admin这样的自定义angular色,您需要定义angular色并将您的用户添加到angular色。 以下是我用于内部项目的一些代码:

 // Admin users var adminUsers = require('../config/users.json').admins; app.models.role.findOrCreate({where: {name: 'admin'}}, {name: 'admin'}, function (err, role) { if (err) { return console.error(err); } // role.principals() doesn't work here as the role.id might have a different // type than roleMapping.roleId app.models.roleMapping.find({where: {roleId: role.id.toString()}}, function (err, principals) { if (err) { return console.error(err); } var mapping = {}; principals.forEach(function (p) { if (p.principalType === 'USER') { mapping[p.principalId] = p; } }); app.models.user.find({where: {email: {inq: adminUsers}}}, function (err, users) { if (err) { return console.error(err); } if (users && users.length) { users.forEach(function (user) { if (mapping[user.id.toString()]) { console.log('User %s (%s) found in role %s', user.username, user.email, role.name); return; } role.principals.create({principalType: 'USER', principalId: user.id}, function (err, mapping) { if (err) { return console.error(err); } console.log('User %s (%s) added to role %s', user.username, user.email, role.name); }); }); } }); }; });