issue #438 fixed

parent 6ac4f1af
...@@ -221,34 +221,30 @@ module.exports = { ...@@ -221,34 +221,30 @@ module.exports = {
// Updates student information // Updates student information
// //
update: function (req, res) { update: function (req, res) {
if (!req.params.id_stu) { if (!req.params.id_stu) {
return res.json(500, { res.badRequest();
error: 'No student defined'
});
} }
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) { Student.findOne(req.params.id_stu).then(function(stu) {
delete req.body.password; var k;
}
// copy attributes // copy attributes
for (k in req.body) stu[k] = req.body[k]; for (k in req.body) stu[k] = req.body[k];
stu.save(function (stuSaveError, saved) {
if (stuSaveError) { if (!req.body.password) // to avoid change password when no one is provided
return res.json(500, { delete stu.password;
error: 'Error when saving student'
}); stu.save().then(function (saved) {
} res.ok(stu);
return res.json(stu); })
}); .catch(function(err) {
res.severError();
}); });
})
.catch(function (err) {
res.notFound();
});
}, },
......
...@@ -240,7 +240,10 @@ module.exports = { ...@@ -240,7 +240,10 @@ module.exports = {
beforeUpdate: function (attrs, next) { beforeUpdate: function (attrs, next) {
delete attrs.username; delete attrs.username;
attrs.attributes = Student.getValidAttributes(attrs.attributes); 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(); 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