2019-05-29 05:21:06 -04:00
|
|
|
const { User } = require('../../models/User')
|
|
|
|
const UserCreator = require('./UserCreator')
|
|
|
|
const UserGetter = require('./UserGetter')
|
|
|
|
const AuthenticationManager = require('../Authentication/AuthenticationManager')
|
2019-08-28 08:59:41 -04:00
|
|
|
const NewsletterManager = require('../Newsletter/NewsletterManager')
|
2019-05-29 05:21:06 -04:00
|
|
|
const async = require('async')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const EmailHandler = require('../Email/EmailHandler')
|
|
|
|
const OneTimeTokenHandler = require('../Security/OneTimeTokenHandler')
|
|
|
|
const Analytics = require('../Analytics/AnalyticsManager')
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const EmailHelper = require('../Helpers/EmailHelper')
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
const UserRegistrationHandler = {
|
2019-05-29 05:21:06 -04:00
|
|
|
_registrationRequestIsValid(body, callback) {
|
|
|
|
const invalidEmail = AuthenticationManager.validateEmail(body.email || '')
|
|
|
|
const invalidPassword = AuthenticationManager.validatePassword(
|
2020-10-22 04:11:43 -04:00
|
|
|
body.password || '',
|
|
|
|
body.email
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
if (invalidEmail != null || invalidPassword != null) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_createNewUserIfRequired(user, userDetails, callback) {
|
|
|
|
if (user == null) {
|
|
|
|
userDetails.holdingAccount = false
|
2019-08-28 08:59:41 -04:00
|
|
|
UserCreator.createNewUser(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
holdingAccount: false,
|
|
|
|
email: userDetails.email,
|
|
|
|
first_name: userDetails.first_name,
|
|
|
|
last_name: userDetails.last_name
|
|
|
|
},
|
2020-09-01 08:37:09 -04:00
|
|
|
{},
|
2019-05-29 05:21:06 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
} else {
|
2019-08-28 08:59:41 -04:00
|
|
|
callback(null, user)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
registerNewUser(userDetails, callback) {
|
|
|
|
const self = this
|
|
|
|
const requestIsValid = this._registrationRequestIsValid(userDetails)
|
|
|
|
if (!requestIsValid) {
|
|
|
|
return callback(new Error('request is not valid'))
|
|
|
|
}
|
|
|
|
userDetails.email = EmailHelper.parseEmail(userDetails.email)
|
2019-08-28 08:59:41 -04:00
|
|
|
UserGetter.getUserByAnyEmail(userDetails.email, (err, user) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if ((user != null ? user.holdingAccount : undefined) === false) {
|
|
|
|
return callback(new Error('EmailAlreadyRegistered'), user)
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
self._createNewUserIfRequired(user, userDetails, (err, user) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
async.series(
|
2019-05-29 05:21:06 -04:00
|
|
|
[
|
|
|
|
cb =>
|
2020-10-12 08:35:50 -04:00
|
|
|
User.update(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ _id: user._id },
|
|
|
|
{ $set: { holdingAccount: false } },
|
|
|
|
cb
|
|
|
|
),
|
|
|
|
cb =>
|
|
|
|
AuthenticationManager.setUserPassword(
|
2020-10-22 04:11:43 -04:00
|
|
|
user,
|
2019-05-29 05:21:06 -04:00
|
|
|
userDetails.password,
|
|
|
|
cb
|
|
|
|
),
|
2019-08-28 08:59:41 -04:00
|
|
|
cb => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (userDetails.subscribeToNewsletter === 'true') {
|
2019-08-28 08:59:41 -04:00
|
|
|
NewsletterManager.subscribe(user, err => {
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn(
|
|
|
|
{ err, user },
|
|
|
|
'Failed to subscribe user to newsletter'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
cb()
|
2019-05-29 05:21:06 -04:00
|
|
|
} // this can be slow, just fire it off
|
|
|
|
],
|
2019-08-28 08:59:41 -04:00
|
|
|
err => {
|
2019-05-29 05:21:06 -04:00
|
|
|
Analytics.recordEvent(user._id, 'user-registered')
|
2019-08-28 08:59:41 -04:00
|
|
|
callback(err, user)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
registerNewUserAndSendActivationEmail(email, callback) {
|
2019-08-28 08:59:41 -04:00
|
|
|
UserRegistrationHandler.registerNewUser(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
email,
|
|
|
|
password: crypto.randomBytes(32).toString('hex')
|
|
|
|
},
|
2019-08-28 08:59:41 -04:00
|
|
|
(err, user) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (
|
|
|
|
err != null &&
|
|
|
|
(err != null ? err.message : undefined) !== 'EmailAlreadyRegistered'
|
|
|
|
) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
(err != null ? err.message : undefined) === 'EmailAlreadyRegistered'
|
|
|
|
) {
|
|
|
|
logger.log({ email }, 'user already exists, resending welcome email')
|
|
|
|
}
|
|
|
|
|
|
|
|
const ONE_WEEK = 7 * 24 * 60 * 60 // seconds
|
2019-08-28 08:59:41 -04:00
|
|
|
OneTimeTokenHandler.getNewToken(
|
2019-05-29 05:21:06 -04:00
|
|
|
'password',
|
2019-09-04 06:57:48 -04:00
|
|
|
{ user_id: user._id.toString(), email },
|
2019-05-29 05:21:06 -04:00
|
|
|
{ expiresIn: ONE_WEEK },
|
2019-08-28 08:59:41 -04:00
|
|
|
(err, token) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const setNewPasswordUrl = `${
|
|
|
|
settings.siteUrl
|
|
|
|
}/user/activate?token=${token}&user_id=${user._id}`
|
|
|
|
|
|
|
|
EmailHandler.sendEmail(
|
|
|
|
'registered',
|
|
|
|
{
|
|
|
|
to: user.email,
|
|
|
|
setNewPasswordUrl
|
|
|
|
},
|
2019-10-15 09:12:11 -04:00
|
|
|
err => {
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn({ err }, 'failed to send activation email')
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
callback(null, user, setNewPasswordUrl)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
|
|
|
|
module.exports = UserRegistrationHandler
|