【问题标题】:Strongloop loopback how to assign static roles to new usersStrongloop loopback如何为新用户分配静态角色
【发布时间】:2016-04-06 02:53:48
【问题描述】:

我克隆了一个https://github.com/beeman/loopback-angular-admin 我已经使用环回资源管理器创建了几个新角色,但是如何将角色分配给我创建的用户

我有一个用户模型,它从环回中的用户模型扩展而来 而模型文件是这样的——

{
  "name": "user",
  "plural": "users",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "accessTokens": {
      "type": "hasMany",
      "model": "accessToken",
      "foreignKey": "userId"
    },
    "identities": {
      "type": "hasMany",
      "model": "userIdentity",
      "foreignKey": "userId"
    },
    "credentials": {
      "type": "hasMany",
      "model": "userCredential",
      "foreignKey": "userId"
    },
    "roles": {
      "type": "hasMany",
      "model": "Role",
      "foreignKey": "principalId",
      "through": "RoleMapping"
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "admin",
      "permission": "ALLOW"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$unauthenticated",
      "permission": "DENY"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}

我的 user.js 就像 -

module.exports = function (user) {

  // Set the username to the users email address by default.
  user.observe('before save', function setDefaultUsername(ctx, next) {
    if (ctx.instance) {
      if(ctx.isNewInstance) {
        ctx.instance.username = ctx.instance.email;
      }
      ctx.instance.status = 'created';
      ctx.instance.created = Date.now();
    }
    next();
  });

};

现在,我想根据我从客户端传递的属性 ctx.instance.type 为用户分配角色和主体

【问题讨论】:

    标签: angularjs authorization roles loopbackjs strongloop


    【解决方案1】:

    假设您已经在 Role 表中创建了一组有限的角色,请使用 after save 挂钩为刚刚创建的用户分配特定的角色:

    User.observe('after save', function setRoleMapping(ctx, next) {
      if (ctx.instance) {
        if(ctx.isNewInstance) {
    
          var RoleMapping = User.app.models.RoleMapping;
          // var roleId = based on type lookup or static?
    
          RoleMapping.create({
            principalType: "USER",
            principalId: ctx.instance.id,
            roleId: roleId
          }, function(err, roleMapping) {
            if (err) {return console.log(err);}
    
            // success stuff
    
          }):
    
        }
      }
      next();
    });
    

    代码未经测试,只是一个大概的想法。您不能使用 before save 挂钩,因为您不知道用于 RoleMapping 表中 principalId 的用户 ID。

    更新:版本包括按传入的类型查找角色:

    user.observe('after save', function setRoleMapping(ctx, next) {
      if (ctx.instance) {
        if(ctx.isNewInstance) {
    
          // look up role based on type
          //
          Role.find({where: {name: ctx.instance.type}}, function(err, role) {
            if (err) {return console.log(err);}
    
            RoleMapping.create({
              principalType: "USER",
              principalId: ctx.instance.id,
              roleId: role.id
            }, function(err, roleMapping) {
    
              if (err) {return console.log(err);}
    
              console.log('User assigned RoleID ' + role.id + ' (' + ctx.instance.type + ')');
    
            }):
    
          });
    
        }
      }
      next();
    });
    

    查询文档在这里:https://docs.strongloop.com/display/public/LB/Querying+data

    【讨论】:

    • 那么如何根据ctx.instance.type值查询数据库模型来获取roleId
    • 这取决于您如何根据类型值确定角色。有多少个角色?角色名称只是类型名称吗?
    • 是的,它只是类型名称,除了管理员之外还有大约 6 个角色,但只是为了学习如何查询角色或任何模型我想知道如何通过查询数据库来做到这一点首先,然后从找到的角色附加 roleId
    • 在我的答案中添加了一个带有角色查找的版本。但这里要小心,如果传入的类型不在列表中,您将没有 role.id 来创建 RoleMapping 条目,因此您需要添加逻辑来处理错误的类型值。使用观察者还会增加需要继续使用next() 的复杂性,并且您还可以传递错误next(err) 以便它们冒泡(但您必须在每个回调上下文中放入多个next(err)s,或者使用用单个 catch() 承诺。
    猜你喜欢
    • 2016-04-07
    • 1970-01-01
    • 2015-04-25
    • 2014-04-07
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多