Assign role to new user object
I'm trying to assign a role to a user newly created in user.js When creating the model object, the role isn't assigned to it. Here's the code in user.js
User.observe ('after save', function (ctx,user, next) {
Role.find({where: {name: ctx.instance.usertype}}, function(err, role) {
if (err) {return console.log(err);}
RoleMapping.create({
principalType: "USER",
principalId: ctx.instance.id,
roleId: role.id
}, function(err, roleMapping) {
if (err) {return console.log(err);}
console.log('User assigned RoleID ' + role.id + ' (' + ctx.instance.type + ')');
})
});
next();
});
Can you provide a link to a test project with steps to reproduce the issue? See https://github.com/strongloop/loopback/wiki/Reporting-issues#bug-report.
Also, have you tried https://github.com/strongloop/loopback-example-user-management?
Hi @superkhau I'm not trying to assign a role in the boot, I'm trying to assign a role in user.js file so that at every user created I can assign a specific role to him depending on the usertype preperty.
var config = require('../../server/config.json');
var path = require('path');
var models = require('../../server/model-config.json');
var Role =models.Role;
var RoleMapping =models.RoleMapping;
module.exports = function(User) {
User.createAdmin = function(cb){
User.create([
{email: '[email protected]', password: 'Vancouver', username : 'ach', usertype : 'Admin'}
], function(err, users) {
if (err) throw err;
console.log('Models created: \n', users);
});
var response = "Bravo";
cb(null, response);
};
User.remoteMethod('createAdmin',
{
http: {path: '/createAdmin', verb: 'post'},
returns: {arg: 'createAdmin', type: 'string'}
}
);
User.createUserR = function(cb){
User.create([
{email: '[email protected]', password: 'Vancouvfgfer', username : 'acvbbh', usertype : 'UserR'}
], function(err, users) {
if (err) throw err;
console.log('Models created: \n', users);
});
var response = "Bravo";
cb(null, response);
};
User.remoteMethod('createUserRh',
{
http: {path: '/createUserRh', verb: 'post'},
returns: {arg: 'createUserR', type: 'string'}
}
);
User.createEmp = function(cb){
User.create([
{email: '[email protected]', password: 'ncobvbuvfgfer', username : 'acvbbhcvbc', usertype : 'Emp'}
], function(err, users) {
if (err) throw err;
console.log('Models created: \n', users);
User.observe ('after save', function (ctx, next) {
Role.find({where: {name: ctx.instance.usertype}}, function(err, role) {
if (err) {return console.log(err);}
RoleMapping.create({
principalType: "USER",
principalId: ctx.instance.id,
roleId: role.id
}, function(err, roleMapping) {
if (err) {return console.log(err);}
console.log('User assigned RoleID ' + role.id + ' (' + ctx.instance.usertype + ')');
})
});
next();
});
});
var response = "Bravo";
cb(null, response);
};
User.remoteMethod('createEmp',
{
http: {path: '/createEmp', verb: 'post'},
returns: {arg: 'createEmp', type: 'string'}
}
);
};
this is the user.js file
@achref-benahmed have you come up solution ?
This should work although I would like to know if there is a way by just setting relations but I have not had any luck with that.
Use the attached method to init Role and RoleMapping
let app = require('../../server/server.js');
module.exports = function (user) {
let Role;
let RoleMapping;
user.on('attached', function (a) {
Role = app.models.Role;
RoleMapping = app.models.RoleMapping;
// perform any setup that requires the app object
})
Alternatively you can just define Role,RoleMapping inside each method that you need.
User.createAdmin = function(cb){
let Role = app.models.Role;
let RoleMapping = app.models.RoleMapping;
}
I am having the same Issue , I can't acces the Role model inside user.js to assign Roles inside remote method
'use strict';
module.exports = function(Account) {
// end point for creating a ressource Account.createRessource = function(username, email, password, cb) { Account.create([ {username: username , email: email , password: password } ],function (err, user) { if( err) throw err ; console.log("ressource created :",user) // How do I assign role in this section or what is the best aproach to do IT // thank you :D
cb("Ressource Created: "+user, true);
});
}; Account.remoteMethod('createRessource', { accepts: [{ arg: 'username', type: 'string'}, { arg: 'email', type: 'string'}, { arg: 'password', type: 'password'} ], http: {path:'/create-ressource', verb: 'get'} });
};
See my comment, this worked for me. https://github.com/strongloop/loopback/issues/2023#issuecomment-322659243
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.