Created
October 4, 2016 19:04
-
-
Save zhenyanghua/d24ea57cd70e69bcb82dc3bc8f14ea74 to your computer and use it in GitHub Desktop.
Loopback query role by user Id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('underscore'); | |
module.exports = function(Customer) { | |
Customer.getRolesById = function (id, cb) { | |
Customer.getApp(function (err, app) { | |
if (err) throw err; | |
var RoleMapping = app.models.RoleMapping; | |
var Role = app.models.Role; | |
RoleMapping.find({ where : { principalId: id }}, function (err, roleMappings) { | |
var roleIds = _.uniq(roleMappings | |
.map(function (roleMapping) { | |
return roleMapping.roleId; | |
})); | |
var conditions = roleIds.map(function (roleId) { | |
return { id: roleId }; | |
}); | |
Role.find({ where: { or: conditions}}, function (err, roles) { | |
if (err) throw err; | |
var roleNames = roles.map(function(role) { | |
return role.name; | |
}); | |
cb(null, {"roles": roleNames}); | |
}); | |
}); | |
}); | |
}; | |
Customer.remoteMethod('getRolesById', { | |
http: { path: '/getRolesById', verb: 'get' }, | |
accepts: {arg: 'id', type: 'number'}, | |
returns: { arg: 'payload', type: 'Object' } | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if model base is RoleMapping , must add " strictObjectIDCoercion" in server/model-config.json, " RoleMapping.find({ where : { principalId: id }} " is not work.
strongloop/loopback#3121