issue #438 fixed

parent 6ac4f1af
......@@ -221,34 +221,30 @@ module.exports = {
// Updates student information
//
update: function (req, res) {
if (!req.params.id_stu) {
return res.json(500, {
error: 'No student defined'
});
res.badRequest();
}
Student.findOne(req.params.id_stu)
.exec(function (err, stu) {
var k;
if (err || !stu)
return res.json(500, {
error: "No student found"
});
if (!req.body.password) {
delete req.body.password;
}
Student.findOne(req.params.id_stu).then(function(stu) {
var k;
// copy attributes
for (k in req.body) stu[k] = req.body[k];
stu.save(function (stuSaveError, saved) {
if (stuSaveError) {
return res.json(500, {
error: 'Error when saving student'
});
}
return res.json(stu);
});
// copy attributes
for (k in req.body) stu[k] = req.body[k];
if (!req.body.password) // to avoid change password when no one is provided
delete stu.password;
stu.save().then(function (saved) {
res.ok(stu);
})
.catch(function(err) {
res.severError();
});
})
.catch(function (err) {
res.notFound();
});
},
......
......@@ -240,7 +240,10 @@ module.exports = {
beforeUpdate: function (attrs, next) {
delete attrs.username;
attrs.attributes = Student.getValidAttributes(attrs.attributes);
attrs.password = bcrypt.hashSync(attrs.password, bcrypt.genSaltSync());
if (attrs.password) {
sails.log.debug('password changed');
attrs.password = bcrypt.hashSync(attrs.password, bcrypt.genSaltSync());
}
next();
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment