2019-05-29 05:21:06 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const mongojs = require('../../infrastructure/mongojs')
|
|
|
|
const metrics = require('metrics-sharelatex')
|
|
|
|
const { db } = mongojs
|
|
|
|
const async = require('async')
|
|
|
|
const { ObjectId } = mongojs
|
2019-09-30 09:21:31 -04:00
|
|
|
const { promisify } = require('util')
|
2019-05-29 05:21:06 -04:00
|
|
|
const UserGetter = require('./UserGetter')
|
|
|
|
const {
|
|
|
|
addAffiliation,
|
|
|
|
removeAffiliation
|
|
|
|
} = require('../Institutions/InstitutionsAPI')
|
2020-02-20 11:08:40 -05:00
|
|
|
const Features = require('../../infrastructure/Features')
|
2019-05-29 05:21:06 -04:00
|
|
|
const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
|
|
|
|
const EmailHelper = require('../Helpers/EmailHelper')
|
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
const NewsletterManager = require('../Newsletter/NewsletterManager')
|
2020-02-27 07:46:07 -05:00
|
|
|
const RecurlyWrapper = require('../Subscription/RecurlyWrapper')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
const UserUpdater = {
|
2020-02-20 11:08:40 -05:00
|
|
|
addAffiliationForNewUser(userId, email, callback) {
|
|
|
|
addAffiliation(userId, email, error => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
UserUpdater.updateUser(
|
|
|
|
{ _id: userId, 'emails.email': email },
|
|
|
|
{ $unset: { 'emails.$.affiliationUnchecked': 1 } },
|
|
|
|
(error, updated) => {
|
|
|
|
if (error || (updated && updated.nModified === 0)) {
|
|
|
|
logger.error(
|
|
|
|
{ userId, email },
|
|
|
|
'could not remove affiliationUnchecked flag for user on create'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(error, updated)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
updateUser(query, update, callback) {
|
|
|
|
if (callback == null) {
|
2019-08-28 08:59:41 -04:00
|
|
|
callback = () => {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (typeof query === 'string') {
|
|
|
|
query = { _id: ObjectId(query) }
|
|
|
|
} else if (query instanceof ObjectId) {
|
|
|
|
query = { _id: query }
|
|
|
|
} else if (typeof query._id === 'string') {
|
|
|
|
query._id = ObjectId(query._id)
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
db.users.update(query, update, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// DEPRECATED
|
|
|
|
//
|
|
|
|
// Change the user's main email address by adding a new email, switching the
|
|
|
|
// default email and removing the old email. Prefer manipulating multiple
|
|
|
|
// emails and the default rather than calling this method directly
|
|
|
|
//
|
|
|
|
changeEmailAddress(userId, newEmail, callback) {
|
|
|
|
newEmail = EmailHelper.parseEmail(newEmail)
|
|
|
|
if (newEmail == null) {
|
|
|
|
return callback(new Error('invalid email'))
|
|
|
|
}
|
|
|
|
|
|
|
|
let oldEmail = null
|
2019-08-28 08:59:41 -04:00
|
|
|
async.series(
|
2019-05-29 05:21:06 -04:00
|
|
|
[
|
|
|
|
cb =>
|
2019-08-28 08:59:41 -04:00
|
|
|
UserGetter.getUserEmail(userId, (error, email) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
oldEmail = email
|
2019-08-28 08:59:41 -04:00
|
|
|
cb(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}),
|
|
|
|
cb => UserUpdater.addEmailAddress(userId, newEmail, cb),
|
2019-07-22 12:55:35 -04:00
|
|
|
cb => UserUpdater.setDefaultEmailAddress(userId, newEmail, true, cb),
|
2019-05-29 05:21:06 -04:00
|
|
|
cb => UserUpdater.removeEmailAddress(userId, oldEmail, cb)
|
|
|
|
],
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
// Add a new email address for the user. Email cannot be already used by this
|
|
|
|
// or any other user
|
|
|
|
addEmailAddress(userId, newEmail, affiliationOptions, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
// affiliationOptions is optional
|
|
|
|
callback = affiliationOptions
|
|
|
|
affiliationOptions = {}
|
|
|
|
}
|
|
|
|
newEmail = EmailHelper.parseEmail(newEmail)
|
|
|
|
if (newEmail == null) {
|
|
|
|
return callback(new Error('invalid email'))
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
UserGetter.ensureUniqueEmailAddress(newEmail, error => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
addAffiliation(userId, newEmail, affiliationOptions, error => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
|
|
|
{ error },
|
|
|
|
'problem adding affiliation while adding email'
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
const reversedHostname = newEmail
|
|
|
|
.split('@')[1]
|
|
|
|
.split('')
|
|
|
|
.reverse()
|
|
|
|
.join('')
|
|
|
|
const update = {
|
|
|
|
$push: {
|
|
|
|
emails: { email: newEmail, createdAt: new Date(), reversedHostname }
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
UserUpdater.updateUser(userId, update, error => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ error }, 'problem updating users emails')
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// remove one of the user's email addresses. The email cannot be the user's
|
|
|
|
// default email address
|
|
|
|
removeEmailAddress(userId, email, callback) {
|
|
|
|
email = EmailHelper.parseEmail(email)
|
|
|
|
if (email == null) {
|
|
|
|
return callback(new Error('invalid email'))
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
removeAffiliation(userId, email, error => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ error }, 'problem removing affiliation')
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = { _id: userId, email: { $ne: email } }
|
|
|
|
const update = { $pull: { emails: { email } } }
|
2019-08-28 08:59:41 -04:00
|
|
|
UserUpdater.updateUser(query, update, (error, res) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ error }, 'problem removing users email')
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.n === 0) {
|
|
|
|
return callback(new Error('Cannot remove email'))
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// set the default email address by setting the `email` attribute. The email
|
|
|
|
// must be one of the user's multiple emails (`emails` attribute)
|
2019-07-22 12:55:35 -04:00
|
|
|
setDefaultEmailAddress(userId, email, allowUnconfirmed, callback) {
|
|
|
|
if (typeof allowUnconfirmed === 'function') {
|
|
|
|
callback = allowUnconfirmed
|
|
|
|
allowUnconfirmed = false
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
email = EmailHelper.parseEmail(email)
|
|
|
|
if (email == null) {
|
|
|
|
return callback(new Error('invalid email'))
|
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
UserGetter.getUser(userId, { email: 1, emails: 1 }, (err, user) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
if (!user) {
|
|
|
|
return callback(new Error('invalid userId'))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
const oldEmail = user.email
|
|
|
|
const userEmail = user.emails.find(e => e.email === email)
|
|
|
|
if (!userEmail) {
|
|
|
|
return callback(new Error('Default email does not belong to user'))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
if (!userEmail.confirmedAt && !allowUnconfirmed) {
|
|
|
|
return callback(new Errors.UnconfirmedEmailError())
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
const query = { _id: userId, 'emails.email': email }
|
|
|
|
const update = { $set: { email } }
|
|
|
|
UserUpdater.updateUser(query, update, (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
// this should not happen
|
|
|
|
if (res.n === 0) {
|
|
|
|
return callback(new Error('email update error'))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-25 10:27:38 -04:00
|
|
|
NewsletterManager.changeEmail(user, email, err => {
|
2019-08-28 08:59:41 -04:00
|
|
|
if (err != null) {
|
|
|
|
logger.warn(
|
|
|
|
{ err, oldEmail, newEmail: email },
|
|
|
|
'Failed to change email in newsletter subscription'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2020-02-27 07:46:07 -05:00
|
|
|
RecurlyWrapper.updateAccountEmailAddress(user._id, email, _error => {
|
|
|
|
// errors are ignored
|
|
|
|
})
|
2019-07-22 12:55:35 -04:00
|
|
|
callback()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
confirmEmail(userId, email, confirmedAt, callback) {
|
|
|
|
if (arguments.length === 3) {
|
|
|
|
callback = confirmedAt
|
|
|
|
confirmedAt = new Date()
|
|
|
|
}
|
|
|
|
email = EmailHelper.parseEmail(email)
|
|
|
|
if (email == null) {
|
|
|
|
return callback(new Error('invalid email'))
|
|
|
|
}
|
|
|
|
logger.log({ userId, email }, 'confirming user email')
|
2019-08-28 08:59:41 -04:00
|
|
|
addAffiliation(userId, email, { confirmedAt }, error => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ error },
|
|
|
|
'problem adding affiliation while confirming email'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
_id: userId,
|
|
|
|
'emails.email': email
|
|
|
|
}
|
|
|
|
const update = {
|
|
|
|
$set: {
|
|
|
|
'emails.$.confirmedAt': confirmedAt
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 11:08:40 -05:00
|
|
|
if (Features.hasFeature('affiliations')) {
|
|
|
|
update['$unset'] = {
|
|
|
|
'emails.$.affiliationUnchecked': 1
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
UserUpdater.updateUser(query, update, (error, res) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.n === 0) {
|
|
|
|
return callback(
|
|
|
|
new Errors.NotFoundError('user id and email do no match')
|
|
|
|
)
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
FeaturesUpdater.refreshFeatures(userId, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
removeReconfirmFlag(userId, callback) {
|
|
|
|
UserUpdater.updateUser(
|
|
|
|
userId.toString(),
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
$set: { must_reconfirm: false }
|
|
|
|
},
|
|
|
|
error => callback(error)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;[
|
|
|
|
'updateUser',
|
|
|
|
'changeEmailAddress',
|
|
|
|
'setDefaultEmailAddress',
|
|
|
|
'addEmailAddress',
|
|
|
|
'removeEmailAddress',
|
|
|
|
'removeReconfirmFlag'
|
|
|
|
].map(method =>
|
|
|
|
metrics.timeAsyncMethod(UserUpdater, method, 'mongo.UserUpdater', logger)
|
|
|
|
)
|
2019-08-28 08:59:41 -04:00
|
|
|
|
2019-09-30 09:21:31 -04:00
|
|
|
const promises = {
|
2020-02-20 11:08:40 -05:00
|
|
|
addAffiliationForNewUser: promisify(UserUpdater.addAffiliationForNewUser),
|
2019-09-30 09:21:31 -04:00
|
|
|
addEmailAddress: promisify(UserUpdater.addEmailAddress),
|
|
|
|
confirmEmail: promisify(UserUpdater.confirmEmail),
|
|
|
|
updateUser: promisify(UserUpdater.updateUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
UserUpdater.promises = promises
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
module.exports = UserUpdater
|