DrBeza
DrBeza

Reputation: 2251

Is it possible to get the current User’s roles accessible in a remote method in Loopback?

Is it possible to get the current User’s roles accessible in a remote method in Loopback?

I’m trying to allow a remote method to either return a subset of data based on a find using a where filter & current userId but in the case of an admin user, I simply want to return a full set of data.

So I have been trying to obtain the list of roles for the current user. But I am struggling to make Role.getRoles() return anything other than:

[ '$unauthenticated', '$everyone' ]

I have tried the context from loopback.getCurrentContext() and the context passed into the beforeRemote method and I have tried ACL.checkAccessForContext().

Any help would be appreciated.

Upvotes: 2

Views: 5599

Answers (1)

Jordan Kasper
Jordan Kasper

Reputation: 13273

This can happen for many reasons, one is that the Role model is not related to your User model (see here for brief explanation). Can you check that?

Other than that, it's probably that first argument (context) which we need to have better documentation on ( sorry :( ). If you look at the tests for the role class you can see how it might be used.

var RoleMapping = loopback.RoleMapping;
RoleMapping.attachTo(ds); // `ds` is your data source

// ...

Role.getRoles({principalType: RoleMapping.USER, principalId: user.id}, function(err, roles) {
    console.log(roles);  // everyone, authenticated, etc (hopefully)
});

Upvotes: 7

Related Questions