2019-05-29 05:21:06 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2019-09-18 11:22:27 -04:00
|
|
|
const util = require('util')
|
2020-09-01 08:37:09 -04:00
|
|
|
const { AffiliationError } = require('../Errors/Errors')
|
2020-02-20 11:08:40 -05:00
|
|
|
const Features = require('../../infrastructure/Features')
|
2020-09-01 08:37:09 -04:00
|
|
|
const { User } = require('../../models/User')
|
|
|
|
const UserDeleter = require('./UserDeleter')
|
2020-02-20 11:08:40 -05:00
|
|
|
const UserGetter = require('./UserGetter')
|
2020-09-01 08:37:09 -04:00
|
|
|
const UserUpdater = require('./UserUpdater')
|
2021-03-10 04:20:55 -05:00
|
|
|
const Analytics = require('../Analytics/AnalyticsManager')
|
2021-03-31 05:24:39 -04:00
|
|
|
const UserOnboardingEmailQueueManager = require('./UserOnboardingEmailManager')
|
2020-02-20 11:08:40 -05:00
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
async function _addAffiliation(user, affiliationOptions) {
|
2020-02-20 11:08:40 -05:00
|
|
|
try {
|
2020-09-01 08:37:09 -04:00
|
|
|
await UserUpdater.promises.addAffiliationForNewUser(
|
|
|
|
user._id,
|
|
|
|
user.email,
|
|
|
|
affiliationOptions
|
|
|
|
)
|
2020-02-20 11:08:40 -05:00
|
|
|
} catch (error) {
|
2020-09-01 08:37:09 -04:00
|
|
|
throw new AffiliationError('add affiliation failed').withCause(error)
|
2020-02-20 11:08:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
user = await UserGetter.promises.getUser(user._id)
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
{ userId: user._id, email: user.email },
|
|
|
|
'could not get fresh user data'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
async function createNewUser(attributes, options = {}) {
|
2019-09-18 11:22:27 -04:00
|
|
|
let user = new User()
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
if (attributes.first_name == null || attributes.first_name === '') {
|
|
|
|
attributes.first_name = attributes.email.split('@')[0]
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
Object.assign(user, attributes)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
user.ace.syntaxValidation = true
|
|
|
|
if (user.featureSwitches != null) {
|
|
|
|
user.featureSwitches.pdfng = true
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const reversedHostname = user.email.split('@')[1].split('').reverse().join('')
|
2019-09-18 11:22:27 -04:00
|
|
|
|
2019-10-07 11:23:45 -04:00
|
|
|
const emailData = {
|
|
|
|
email: user.email,
|
|
|
|
createdAt: new Date(),
|
2021-04-27 03:52:58 -04:00
|
|
|
reversedHostname,
|
2019-10-07 11:23:45 -04:00
|
|
|
}
|
2020-02-20 11:08:40 -05:00
|
|
|
if (Features.hasFeature('affiliations')) {
|
|
|
|
emailData.affiliationUnchecked = true
|
|
|
|
}
|
2019-10-07 11:23:45 -04:00
|
|
|
if (
|
|
|
|
attributes.samlIdentifiers &&
|
|
|
|
attributes.samlIdentifiers[0] &&
|
|
|
|
attributes.samlIdentifiers[0].providerId
|
|
|
|
) {
|
|
|
|
emailData.samlProviderId = attributes.samlIdentifiers[0].providerId
|
|
|
|
}
|
|
|
|
|
|
|
|
user.emails = [emailData]
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
user = await user.save()
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
if (Features.hasFeature('affiliations')) {
|
2020-09-01 08:37:09 -04:00
|
|
|
try {
|
|
|
|
user = await _addAffiliation(user, options.affiliationOptions || {})
|
|
|
|
} catch (error) {
|
|
|
|
if (options.requireAffiliation) {
|
|
|
|
await UserDeleter.promises.deleteMongoUser(user._id)
|
|
|
|
throw error
|
|
|
|
} else {
|
|
|
|
logger.error(error)
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 11:08:40 -05:00
|
|
|
}
|
2019-09-18 11:22:27 -04:00
|
|
|
|
2021-03-10 04:20:55 -05:00
|
|
|
Analytics.recordEvent(user._id, 'user-registered')
|
2021-03-31 05:24:39 -04:00
|
|
|
try {
|
|
|
|
await UserOnboardingEmailQueueManager.scheduleOnboardingEmail(user)
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
`Failed to schedule sending of onboarding email for user '${user._id}'`,
|
|
|
|
error
|
|
|
|
)
|
|
|
|
}
|
2021-03-10 04:20:55 -05:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
const UserCreator = {
|
|
|
|
createNewUser: util.callbackify(createNewUser),
|
|
|
|
promises: {
|
2021-04-27 03:52:58 -04:00
|
|
|
createNewUser: createNewUser,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
module.exports = UserCreator
|