/
change_password.js
executable file
·83 lines (65 loc) · 2.61 KB
/
change_password.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Copyright (C) 2014 PencilBlue, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Changes the logged in user's password
*/
//dependencies
var BaseController = pb.BaseController;
var FormController = pb.FormController;
function ChangePassword(){}
//inheritance
util.inherits(ChangePassword, FormController);
ChangePassword.prototype.onPostParamsRetrieved = function(post, cb) {
var self = this;
//sanitize
post.current_password = BaseController.sanitize(post.current_password);
post.new_password = BaseController.sanitize(post.new_password);
post.confirm_password = BaseController.sanitize(post.confirm_password);
//validate
var message = this.hasRequiredParams(post, ['current_password', 'new_password', 'confirm_password']);
if(message) {
this.formError(message, '/user/manage_account', cb);
return;
}
var where = {
_id: ObjectID(self.session.authentication.user._id),
password: pb.security.encrypt(post.current_password)
};
delete post.current_password;
if(post.new_password !== post.confirm_password) {
self.formError(self.ls.get('PASSWORD_MISMATCH'), '/user/change_password', cb);
return;
}
post.password = post.new_password;
delete post.new_password;
delete post.confirm_password;
var dao = new pb.DAO();
dao.loadByValues(where, 'user', function(err, user) {
if(util.isError(err) || user === null) {
self.formError(self.ls.get('INVALID_PASSWORD'), '/user/change_password', cb);
return;
}
pb.DocumentCreator.update(post, user);
dao.update(user).then(function(result) {
if(util.isError(result)) {
self.formError(self.ls.get('ERROR_SAVING'), '/user/change_password', cb);
return;
}
self.session.success = self.ls.get('PASSWORD_CHANGED');
cb(pb.RequestHandler.generateRedirect(pb.config.siteRoot + '/user/change_password'));
});
});
};
//exports
module.exports = ChangePassword;