2014-02-12 05:23:40 -05:00
|
|
|
User = require("../../models/User").User
|
|
|
|
UserLocator = require("./UserLocator")
|
2016-06-13 08:21:44 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require('metrics-sharelatex')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-03-17 10:55:41 -04:00
|
|
|
|
|
|
|
module.exports = UserCreator =
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
getUserOrCreateHoldingAccount: (email, callback = (err, user)->)->
|
|
|
|
self = @
|
|
|
|
UserLocator.findByEmail email, (err, user)->
|
|
|
|
if user?
|
|
|
|
callback(err, user)
|
|
|
|
else
|
|
|
|
self.createNewUser email:email, holdingAccount:true, callback
|
|
|
|
|
|
|
|
createNewUser: (opts, callback)->
|
2016-06-13 08:21:44 -04:00
|
|
|
logger.log opts:opts, "creating new user"
|
2014-02-12 05:23:40 -05:00
|
|
|
user = new User()
|
|
|
|
user.email = opts.email
|
|
|
|
user.holdingAccount = opts.holdingAccount
|
2016-11-01 12:36:26 -04:00
|
|
|
user.ace.syntaxValidation = true
|
2014-04-10 09:43:06 -04:00
|
|
|
|
|
|
|
username = opts.email.match(/^[^@]*/)
|
2016-06-13 08:21:44 -04:00
|
|
|
if opts.first_name? and opts.first_name.length != 0
|
|
|
|
user.first_name = opts.first_name
|
|
|
|
else if username?
|
2014-04-10 09:43:06 -04:00
|
|
|
user.first_name = username[0]
|
|
|
|
else
|
|
|
|
user.first_name = ""
|
2016-06-13 08:21:44 -04:00
|
|
|
|
|
|
|
if opts.last_name?
|
|
|
|
user.last_name = opts.last_name
|
|
|
|
else
|
|
|
|
user.last_name = ""
|
2014-04-10 09:43:06 -04:00
|
|
|
|
2015-01-21 10:26:01 -05:00
|
|
|
user.featureSwitches?.pdfng = true
|
2014-04-10 09:43:06 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
user.save (err)->
|
|
|
|
callback(err, user)
|
2017-03-17 10:55:41 -04:00
|
|
|
|
|
|
|
metrics.timeAsyncMethod(
|
|
|
|
UserCreator, 'createNewUser',
|
|
|
|
'mongo.UserCreator',
|
|
|
|
logger
|
|
|
|
)
|