angularjs - loopback angular sdk get all users with a certain role -
i'm stuck on getting users role, example admin users, in 1 angular sdk controller.
according docs of strongloop. did was:
user.find({ filter: { include: [{'relation':'roles', 'scope': { where:{ name:'admin', }} }], }, }, function(list) { console.log(list); });
but list got users, non-admin users included too. on server side default codes, didn't change them.
{ "name": "user", "plural": "users", "base": "user", "properties": { }, "relations": { "roles": { "type": "belongsto", "model": "rolemapping", "foreignkey": "principalid" } }, "acls": [], "methods": [] }
could tell me made wrong? don't want loop through "list" query , filter admin users, because huge list of users, admin 2 or 3 persons.
here solution of did, common/models/user.js, created remotemethod, called "getusersbyrole", , accept "role", name of role:
user.remotemethod('getusersbyrole', { accepts: [ { arg: 'role', type: 'string', required: true }, ], returns: {arg: 'users', type: 'string'}, http: { verb: 'get', path: '/byrole/:role' } });
then here function of it:
user.getusersbyrole = function(role, cb) { var loopback = require('loopback'); var role = loopback.getmodel('role'); var useridlist = []; role.findone({include:'principals', where: {name:role}}, function(err, role) { role.principals(function(err, principals) { (var = 0; < principals.length; i++) { useridlist.push(parseint(principals[i].principalid)); } if (useridlist.length > 0) { user.find({where: {id: {inq: useridlist}}}, function(err, users) { cb(err, users); }); } else { cb(err, false); } }); }); }
then run lb-ng command generate service angular client side, run:
user.getusersbyrole({role:rolename}, function(list) { });
in controller.
Comments
Post a Comment