How can I get account's role in "beforeRemote"?
I created a model Account which extend from "User", I also covered the relations with "AccessToken" like this
"accessTokens": {
"type": "hasMany",
"model": "AccessToken",
"foreignKey": "accountId"
}
My question is how can I get the account's role in "beforeRemote"?
the code like this :
Account.beforeRemote('method', function(ctx, next) {
if( role = 'admin' ) { can update something }
else{ only can update one property , for example: name}
next();
});
How can I get the "role" ? Thanks
@Spark-star you should do that using the loopback authorisation
// common/models/account.js
var RoleMapping = app.loopback.RoleMapping;
Account.beforeRemote('method', function(ctx, account, next) {
RoleMapping.findOne({
where: { principalId: account.Id },
include: 'role'
}, function(err, roleMapping) {
if (err || !roleMapping) { return next(err); }
role = roleMapping.role().name
if (role === 'admin') {
\\ Do awesome
else {
\\ Weak sauce!
}
next();
});
})
Thank you so much! It's great.
This document should help as well: https://docs.strongloop.com/display/LB/Remote+hooks
Closing this issue for now. If you still have an issue with the same subject, please feel free to leave the comment and I will re-open the issue. Thanks.
Now you can use this:
ctx.args.options.authorizedRoles
Remember to apply "strictObjectIDCoercion" option with true value to the following models in model-config.json:
-
Role
-
ACL
-
RoleMapping