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')
|
|
|
|
const { User } = require('../../models/User')
|
2019-05-29 05:21:06 -04:00
|
|
|
const { addAffiliation } = require('../Institutions/InstitutionsAPI')
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
async function createNewUser(attributes, options = {}) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
const reversedHostname = user.email
|
|
|
|
.split('@')[1]
|
|
|
|
.split('')
|
|
|
|
.reverse()
|
|
|
|
.join('')
|
|
|
|
|
2019-10-07 11:23:45 -04:00
|
|
|
const emailData = {
|
|
|
|
email: user.email,
|
|
|
|
createdAt: new Date(),
|
|
|
|
reversedHostname
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
if (!options.skip_affiliation) {
|
|
|
|
// There is no guarantee this will complete so we must not rely on it
|
|
|
|
addAffiliation(user._id, user.email, err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(
|
|
|
|
{ userId: user._id, email: user.email },
|
|
|
|
"couldn't add affiliation for user on create"
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-09-18 11:22:27 -04:00
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
const UserCreator = {
|
|
|
|
createNewUser: util.callbackify(createNewUser),
|
|
|
|
promises: {
|
|
|
|
createNewUser: createNewUser
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
module.exports = UserCreator
|