You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to get all users belong to a specific role? #332
Actually I am working on a project created via slc lb project myapp, so there is a new roleMapping in the models.json file which extends the RoleMapping model.
Still for your own code I cannot get anything back for the users. and here is the code I tracked down in the node-inspector
// Set up the connection to users/applications/roles once the modelRole.once('dataSourceAttached',function(){varroleMappingModel=this.RoleMapping||loopback.getModelByType(RoleMapping);Role.prototype.users=function(callback){roleMappingModel.find({where: {roleId: this.id,principalType: RoleMapping.USER}},function(err,mappings){if(err){callback&&callback(err);return;}returnmappings.map(function(m){returnm.principalId;});});};
@mneil Thanks, your solution is working fine. However in RoleMapping.app.models.Role.byName, the function byName is saying undefined. Hence, I have modified the above portion as follows RoleMapping.app.models.Role.findOne({where: {name:role}}, function(err, role){ // ... });
Ah yes, sorry @hilarudeensa I didn't define that function in my example.
In ./server/boot/role.js
module.exports = function(app) {
var Role = app.models.Role;
/**
* Get a role by name
* @param name
*/
Role.byName = function(name, callback){
Role.findOne({where: {name: name}}, callback);
}
}
I also added slightly better error checking to the above examples as I found that it was throwing an exception if I searched for a role that didn't exist. In my case the role should always exist but I have tests for if it doesn't and they were failing without better error checking.
I think you should close this issue. It's related to #1441. I was just suggesting a workaround that I used. A couple of the suggestions about coercing the id to a mongo ObjectId in the issue I just linked to really is a better solution though.
Activity
raymondfeng commentedon Jun 16, 2014
haohello commentedon Jun 16, 2014
Well, the following code doesn't work
I changed the code to the following and then get it working correctly.
raymondfeng commentedon Jun 16, 2014
See https://github.com/strongloop/loopback/blob/master/test/role.test.js#L68
haohello commentedon Jun 17, 2014
Actually I am working on a project created via
slc lb project myapp, so there is a new roleMapping in the models.json file which extends the RoleMapping model.Still for your own code I cannot get anything back for the users. and here is the code I tracked down in the node-inspector
haohello commentedon Jun 17, 2014
I guess this issue (#274) might be able to answer why your code doesn't work for me, by the way, I am also using MongoDB as my data source.
haohello commentedon Jun 18, 2014
Any update on this issue?
mneil commentedon Jul 7, 2015
Seems there are several open issues with roles not correctly being included with users. The way I ended up making this work was:
server/boot/role-mapping.jsThen in my users model
common/models/user.jshilarudeensa commentedon Aug 19, 2015
@mneil Thanks, your solution is working fine. However in
RoleMapping.app.models.Role.byName, the functionbyNameis saying undefined. Hence, I have modified the above portion as followsRoleMapping.app.models.Role.findOne({where: {name:role}}, function(err, role){ // ... });mneil commentedon Aug 23, 2015
Ah yes, sorry @hilarudeensa I didn't define that function in my example.
In
./server/boot/role.jsI also added slightly better error checking to the above examples as I found that it was throwing an exception if I searched for a role that didn't exist. In my case the role should always exist but I have tests for if it doesn't and they were failing without better error checking.
superkhau commentedon Feb 4, 2016
Are you guys still running into issues? Can I close this?
mneil commentedon Feb 4, 2016
I think you should close this issue. It's related to #1441. I was just suggesting a workaround that I used. A couple of the suggestions about coercing the id to a mongo ObjectId in the issue I just linked to really is a better solution though.
superkhau commentedon Feb 4, 2016
@mneil Thanks for the response. Closing.
Please open a new issue if you guys are still running into problems.
codyolsen commentedon Aug 29, 2016
Please note that the
adminRole.usersmethod used in @raymondfeng's solution is currently broken. I've submitted a simple fix in PR #2742.