在环回中创建超级用户管理员

问题描述 投票:1回答:1

我仍然深入挖掘环回,请帮忙。我有以下模型:admin =>扩展内置的User模型,student =>从内置的User模型和其他模型ofcoz扩展。

简要:

我不希望经过身份验证的实例student能够访问端点Student GET /students。我不希望Student能够访问有关所有Student的信息。所以,我建议使用admin,它应该能够通过实现角色来访问端点GET /学生。我希望管理员能够访问所有端点

常设:

在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: '[email protected]', 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);
      });
    });
  });

    }
});

}

student.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"
    }
  ],

admin.json

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

问题

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

目标

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

loopbackjs acl
1个回答
0
投票

在大多数情况下,一个好的设计是有一个单一的成员(或客户或任何你喜欢的)扩展内置的用户模型。

基本上,您注册两个成员,创建角色管理员并提升管理员两个成员中的一个。这里有一个例子:Multiple users roles loopback

student.json中的acls是正确的并且会起作用。

如果您真的想要扩展内置用户的不同模型,请在官方文档中详细说明具有多个用户模型的访问控制:

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

© www.soinside.com 2019 - 2024. All rights reserved.