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

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

问题描述:

我想了解loopback acl但失败,如果我可以使用loopback acl控制角色授权,我该怎么办?如何使用Loopback ACL修改用户角色的权限

当我请求

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

下面是雇员。该JSON配置

{ 
    "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 
} 

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

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

// 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); 
        }); 
       }); 
      } 
      }); 
     }; 
    }); 
+0

对不起。所以迟到了回复你。我错过了与你最好的会议时间。你在那里? – dblose 2014-09-01 01:27:35

+0

我需要更新这个脚本吗?在我的情况下,我已经添加了一个扩展的用户模型,该模型与我需要访问这些方法的另一个持久模型的关系 – Elankeeran 2016-03-25 11:29:50