Harshit Laddha
Harshit Laddha

Reputation: 2124

strongloop loopback find users role using loaded operation hook

I have cloned a https://github.com/beeman/loopback-angular-admin and I have created a couple of new roles using the loopback explorer and I am trying to get all the roles of a particular user when he logs in by loaded operation hook defined here -

https://docs.strongloop.com/display/public/LB/Operation+hooks#Operationhooks-loaded

like this -

user.observe('loaded', function appendRole(ctx, next){
    if(ctx.instance){
      user.findOne({
        where: {
          id: ctx.instance.id
        },
        include: {
          "relation":"roles"
        }
      })
    }
    next();
  })

so how do I return roles of a particular user using operation hooks. I am able to get all the roles of a user using loopback explorer using this api call

http://localhost:80/api/users/567ce48d6503f9404b56bb3e/roles?access_token=gyPzW3rpr3uzve2bUHtZQWv8iV5PfZYW7QLicCs4GwIKTdNA33SeRAlgPIQef7AE

UPDATE :

So, I tried adding the following code to the user.js -

user.observe('loaded', function appendRole(ctx, next){
    if(ctx.instance){
      console.log(ctx.instance.roles);
    }
    next();
  })

and I am getting the following output in the console -

{ [Function]
  _receiver: 
   { username: '[email protected]',
     password: '$2a$10$Bubhaq1LXFyCUn.W1/pEOewLSqspcP2GQlONwGH98V4HqCOAc9522',
     email: '[email protected]',
     status: 'created',
     created: Mon Jan 04 2016 22:53:53 GMT+0530 (IST),
     firstName: 'Harshit',
     lastName: 'Laddha',
     gender: 'male',
     birthday: '1993-07-30T18:30:00.000Z',
     qualification: 'sa;',
     experience: 'askjdl',
     achievements: 'sakldj',
     street: 'has',
     locality: 'alskjd',
     area: 'lkjd',
     city: 'bangalore',
     id: 568aaaa997ace4670b5d9ac2 },
  _scope: 
   { where: { principalId: 568aaaa997ace4670b5d9ac2 },
     collect: 'role',
     include: 'role' },
  _targetClass: 'Role',
  getAsync: [Function],
  build: [Function: build],
  create: [Function],
  updateAll: [Function: updateAll],
  destroyAll: [Function: destroyAll],
  findById: [Function],
  findOne: [Function: findOne],
  count: [Function: count],
  destroy: [Function],
  updateById: [Function],
  exists: [Function],
  add: [Function],
  remove: [Function] }

so how do I get the roles of user using loaded operation hook

Upvotes: 0

Views: 830

Answers (1)

notbrain
notbrain

Reputation: 3396

Did you try ctx.instance.roles?

UPDATE: Ah crap, that's right, I guess you'll need to do a lookup, but you can't use a user.find() since it will trigger an infinite loop on the loaded observe. Try something like this:

user.observe('loaded', function getRoleMappings(ctx, next) {

  var roleMapFilter = {
    where: {principalId: ctx.instance.id},
    include: ['role']
  };

  user.app.models.RoleMapping.find(roleMapFilter, function(err, roleMaps) {
    if (err) {
      console.log('roleMap', err);
      next(err);
    }

    console.log("roleMaps ", roleMaps);
    next();

  });


});

console.log("roleMaps ", roleMaps); should output something like:

roleMaps  [ 
  {
    id: 1,
    principalType: 'USER',
    principalId: '1',
    roleId: 1,
    role: { 
      id: 1,
      name: 'admin',
      description: null,
      created: Thu Jun 25 2015 10:24:40 GMT-0700 (PDT),
      modified: Thu Jun 25 2015 10:24:40 GMT-0700 (PDT)
    } 
  },
  { 
    id: 2,
    principalType: 'USER',
    principalId: '1',
    roleId: 6,
    role: {
      id: 6,
      name: 'sales',
      description: null,
      created: Thu Jun 25 2015 10:24:40 GMT-0700 (PDT),
      modified: Thu Jun 25 2015 10:24:40 GMT-0700 (PDT)
    } 
  }
]

Upvotes: 1

Related Questions