首页 > 解决方案 > 在环回中创建超级用户管理员

问题描述

我仍在深入研究环回,请提供帮助。我有以下模型:admin=> 扩展内置User模型,student=> 扩展内置User模型和 coz 的其他模型。

简短的:

我不希望经过身份验证的实例student能够访问端点Student GET /students。我不希望Student能够访问有关所有Student(s) 的信息。所以,我提出了 admin,它应该能够通过 Role 的实现来访问端点 GET /students。我希望管理员能够访问所有端点

常设:

在 script.js 中

module.exports = function(app) {
 const User = app.models.admin;
 const Role = app.models.Role;
 const RoleMapping = app.models.RoleMapping;


 Role.find({ name: 'admin' }, function(err, results) {
    if (err) { 
        throw err;
     }

    if (results.length < 1) {

        // now  Role creation...
   User.create([{name:'Felix Olonde',username:"felix",email: 'felix@gmail.com', password: 'felix123',phone_number:3127287656,dob:1988-03-04,state: 'LA'
    }
  ], function(err, users) {
    if (err) throw err;

    console.log('Created user:', users);

    //create the admin role
    Role.create({
      name: 'admin'
    }, function(err, role) {
      if (err) throw err;

      console.log('Created role:', role);

      role.principals.create({
        principalType: RoleMapping.USER,
        principalId: users[0].id
      }, function(err, principal) {
        if (err) throw err;

        console.log('Created principal:', principal);
      });
    });
  });

    }
});

}

学生.json

"acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$owner",//student to access their own information
      "permission": "ALLOW"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "admin",
      "permission": "ALLOW",
      "property": "find"
    }
  ],

管理员.json

  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    }
  ],

问题

登录在数据库中成功创建的管理员后(我使用托管在 mLab 中的 mongo),当我尝试让所有学生使用资源管理器时,我不断收到 401 ..“需要授权”

目标

我只希望管理员能够真正完全控制 API 端点。即管理员应该能够获得所有学生等。

标签: loopbackjsacl

解决方案


在大多数情况下,一个好的设计是让一个类 Member(或 Customer 或任何你喜欢的)扩展内置的 User 模型。

基本上,您注册两个成员,创建角色 admin 并提升两个成员之一的 admin。此处的示例: 多用户角色环回

输入的 aclsstudent.json是正确的,并且可以正常工作。

如果你真的想有不同的模型来扩展内置的用户,在官方文档中有很好的解释多用户模型的访问控制:

https://loopback.io/doc/en/lb3/Authentication-authorization-and-permissions.html#access-control-with-multiple-user-models


推荐阅读