【问题标题】:How to use Loopback ACL modify permissions on the user role如何使用 Loopback ACL 修改用户角色的权限
【发布时间】:2014-10-16 18:39:02
【问题描述】:

我想了解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 的内部。怎么去改变才能达到访问控制的效果?

【问题讨论】:

    标签: node.js strongloop loopbackjs


    【解决方案1】:

    要支持像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);
                      });
                  });
                }
              });
          };
      });
    

    【讨论】:

    • 对不起。这么晚才回复你。我错过了与你的最佳会话时间。你在吗?
    • 我需要在哪里更新这个脚本?在我的情况下,我添加了一个扩展用户模型,该模型与我需要访问这些方法的其他持久模型有关系
    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 2018-02-09
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 2018-11-18
    • 2016-09-13
    相关资源
    最近更新 更多