-
Notifications
You must be signed in to change notification settings - Fork 289
/
password_reset_service.js
271 lines (237 loc) · 9.22 KB
/
password_reset_service.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Copyright (C) 2016 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/>.
*/
'use strict';
//dependencies
var util = require('../../util.js');
var async = require('async');
module.exports = function(pb) {
//pb dependencies
var DAO = pb.DAO;
var BaseObjectService = pb.BaseObjectService;
var ValidationService = pb.ValidationService;
var UrlService = pb.UrlService;
var SiteService = pb.SiteService;
/**
* @private
* @static
* @readonly
* @property TYPE
* @type {String}
*/
var TYPE = 'password_reset';
/**
* Provides interactions with topics
* @class PasswordResetService
* @extends BaseObjectService
* @constructor
* @param {Object} context
* @param {string} context.site
* @param {boolean} context.onlyThisSite
* @param {UserService} context.userService
* @param {SiteService} context.siteService
* @param {EmailService} context.emailService
*/
function PasswordResetService(context) {
if (!util.isObject(context)) {
context = {};
}
/**
* @property userService
* @type {UserService}
*/
this.userService = context.userService;
/**
* @property siteService
* @type {SiteService}
*/
this.siteService = context.siteService;
/**
* @property emailService
* @type {EmailService}
*/
this.emailService = context.emailService;
context.type = TYPE;
PasswordResetService.super_.call(this, context);
}
util.inherits(PasswordResetService, BaseObjectService);
/**
* @method addIfNotExists
* @param {string} usernameOrEmail
* @param {function} cb (Error, {created: boolean, data: {}})
*/
PasswordResetService.prototype.addIfNotExists = function(usernameOrEmail, cb) {
var self = this;
//retrieve the user
this.userService.getByUsernameOrEmail(usernameOrEmail, {}, function(err, userObj) {
if (util.isError(err)) {
return cb(err);
}
if (util.isNullOrUndefined(userObj)) {
return cb(BaseObjectService.notFound());
}
//attempt to retrieve any existing reset
self.getSingle({where: {userId: userObj.id}}, function(err, passwordResetObj) {
if (util.isError(err)) {
return cb(err);
}
//need to know if we should create the DTO or not
var created = !passwordResetObj;
if (created) {
passwordResetObj = {userId: userObj.id};
}
//now persist it back
self.save(passwordResetObj, function(err, persistedPasswordReset) {
if (util.isError(err)) {
return cb(err);
}
//now attempt to send the notification
self.sendPasswordResetEmail(userObj, persistedPasswordReset, function(err) {
cb(err, {
created: created,
data: persistedPasswordReset
});
});
});
});
});
};
/**
* Sends a password reset email to a user
* @method sendPasswordResetEmail
* @param {object} user A user object
* @param {object} passwordReset A password reset object containing the verification code
* @param {function} cb (Error)
*/
PasswordResetService.prototype.sendPasswordResetEmail = function(user, passwordReset, cb) {
var self = this;
var tasks = [
util.wrapTask(this.siteService, this.siteService.getByUid, [this.context.site]),
function(siteInfo, callback) {
var root = SiteService.getHostWithProtocol(siteInfo.hostname);
var verificationUrl = UrlService.urlJoin(root, '/actions/user/reset_password') +
util.format('?email=%s&code=%s', encodeURIComponent(user.email), encodeURIComponent(passwordReset.verificationCode));
var options = {
to: user.email,
subject: siteInfo.displayName + ' Password Reset',
template: 'admin/elements/password_reset_email',
replacements: {
'verification_url': verificationUrl,
'first_name': user.first_name,
'last_name': user.last_name
}
};
self.emailService.sendFromTemplate(options, callback);
}
];
async.waterfall(tasks, cb);
};
/**
* @method validate
* @param context
* @param {object} context.data
* @param {Array} context.validationErrors
* @param {function} cb
* @returns {*}
*/
PasswordResetService.prototype.validate = function(context, cb) {
var tasks = [
util.wrapTask(this, this.validateUserId, [context]),
util.wrapTask(this, this.validateVerificationCode, [context])
];
async.parallel(tasks, cb);
};
PasswordResetService.prototype.validateUserId = function(context, cb) {
var obj = context.data;
var errors = context.validationErrors;
if (!ValidationService.isIdStr(obj.userId, true)) {
errors.push(BaseObjectService.validationFailure('userId', 'The userId must be a valid string'));
return cb();
}
var options = {
where: DAO.getIdWhere(obj.userId)
};
this.userService.count(options, function(err, count) {
if (count !== 1) {
errors.push(BaseObjectService.validationFailure('userId', 'The userId must reference an existing user'));
}
cb(err);
});
};
PasswordResetService.prototype.validateVerificationCode = function(context, cb) {
var obj = context.data;
var errors = context.validationErrors;
if (!ValidationService.isNonEmptyStr(obj.verificationCode, true)) {
errors.push(BaseObjectService.validationFailure('verificationCode', 'The verificationCode must be a non-empty string'));
return cb();
}
var where = {
verificationCode: obj.verificationCode
};
this.dao.unique(TYPE, where, obj[DAO.getIdField()], function(err, isUnique) {
if (!isUnique) {
errors.push(BaseObjectService.validationFailure('verificationCode', 'The verificationCode must be unique'));
}
cb(err);
});
};
/**
*
* @static
* @method
* @param {Object} context
* @param {PasswordResetService} context.service An instance of the service that triggered
* the event that called this handler
* @param {Function} cb A callback that takes a single parameter: an error if occurred
*/
PasswordResetService.onFormat = function(context, cb) {
var dto = context.data;
dto.userId = BaseObjectService.sanitize(dto.userId);
cb();
};
/**
*
* @static
* @method
* @param {Object} context
* @param {PasswordResetService} context.service An instance of the service that triggered
* the event that called this handler
* @param {Function} cb A callback that takes a single parameter: an error if occurred
*/
PasswordResetService.onMerge = function(context, cb) {
context.object.userId = context.data.userId;
//the verification code is overwritten each time the object is saved. This makes it a singleton and prevents
//multiple requests for a password reset, per user, floating around
context.object.verificationCode = util.uniqueId();
cb();
};
/**
*
* @static
* @method validate
* @param {Object} context
* @param {Object} context.data The DTO that was provided for persistence
* @param {PasswordResetService} context.service An instance of the service that triggered
* the event that called this handler
* @param {Function} cb A callback that takes a single parameter: an error if occurred
*/
PasswordResetService.onValidate = function(context, cb) {
context.service.validate(context, cb);
};
//Event Registries
BaseObjectService.on(TYPE + '.' + BaseObjectService.FORMAT, PasswordResetService.onFormat);
BaseObjectService.on(TYPE + '.' + BaseObjectService.MERGE, PasswordResetService.onMerge);
BaseObjectService.on(TYPE + '.' + BaseObjectService.VALIDATE, PasswordResetService.onValidate);
//exports
return PasswordResetService;
};