deadman deadman - 3 months ago 101
AngularJS Question

Strongloop loopback how to assign static roles to new users

I have cloned a

https://github.com/beeman/loopback-angular-admin

and I have created a couple of new roles using the loopback explorer but how do I assign roles to users that I create

I have a user model which extends from User model in loopback
and the model file is like this -

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


and my user.js is like -

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();
});

};


Now, I want to assign roles and principals to users based on a property
ctx.instance.type
that I am passing from client

Answer

Assuming you already have a finite set of Roles created in your Role table, use an after save hook to assign the just-created User a specific Role:

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();
});

Code not tested, just a general idea. You can't use a before save hook since you won't know the ID of the User to use for the principalId in the RoleMapping table.

UPDATE: Version including looking up the Role by type passed in:

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();
});

Query docs are here: https://docs.strongloop.com/display/public/LB/Querying+data

Comments