2014-05-19 06:50:32 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2014-02-12 05:23:40 -05:00
|
|
|
mongojs = require("../../infrastructure/mongojs")
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require("metrics-sharelatex")
|
2014-02-12 05:23:40 -05:00
|
|
|
db = mongojs.db
|
2018-05-25 07:04:09 -04:00
|
|
|
async = require("async")
|
2014-02-12 05:23:40 -05:00
|
|
|
ObjectId = mongojs.ObjectId
|
2018-05-23 10:12:23 -04:00
|
|
|
UserGetter = require("./UserGetter")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = UserUpdater =
|
|
|
|
updateUser: (query, update, callback = (error) ->) ->
|
|
|
|
if typeof query == "string"
|
|
|
|
query = _id: ObjectId(query)
|
|
|
|
else if query instanceof ObjectId
|
|
|
|
query = _id: query
|
2018-05-25 07:04:09 -04:00
|
|
|
else if typeof query._id == "string"
|
|
|
|
query._id = ObjectId(query._id)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
db.users.update query, update, callback
|
2014-05-16 12:29:54 -04:00
|
|
|
|
|
|
|
|
2018-05-25 07:04:09 -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)->
|
|
|
|
logger.log userId: userId, newEmail: newEmail, "updaing email address of user"
|
|
|
|
|
|
|
|
oldEmail = null
|
|
|
|
async.series [
|
|
|
|
(cb) ->
|
|
|
|
UserGetter.getUserEmail userId, (error, email) ->
|
|
|
|
oldEmail = email
|
|
|
|
cb(error)
|
|
|
|
(cb) -> UserUpdater.addEmailAddress userId, newEmail, cb
|
|
|
|
(cb) -> UserUpdater.setDefaultEmailAddress userId, newEmail, cb
|
|
|
|
(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, callback) ->
|
|
|
|
@_ensureUniqueEmailAddress newEmail, (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
|
|
|
|
update = $push: emails: email: newEmail, createdAt: new Date()
|
|
|
|
@updateUser userId, update, (error) ->
|
|
|
|
if error?
|
|
|
|
logger.err error: error, 'problem updating users emails'
|
|
|
|
return callback(error)
|
2014-05-16 12:29:54 -04:00
|
|
|
callback()
|
|
|
|
|
2017-03-16 06:59:18 -04:00
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
# remove one of the user's email addresses. The email cannot be the user's
|
|
|
|
# default email address
|
|
|
|
removeEmailAddress: (userId, email, callback) ->
|
|
|
|
query = _id: userId, email: $ne: email
|
|
|
|
update = $pull: emails: email: email
|
|
|
|
@updateUser query, update, (error, res) ->
|
|
|
|
if error?
|
|
|
|
logger.err error:error, 'problem removing users email'
|
|
|
|
return callback(error)
|
|
|
|
if res.nMatched == 0
|
|
|
|
return callback(new Error('Cannot remove default email'))
|
|
|
|
callback()
|
|
|
|
|
|
|
|
|
|
|
|
# set the default email address by setting the `email` attribute. The email
|
|
|
|
# must be one of the user's multiple emails (`emails` attribute)
|
|
|
|
setDefaultEmailAddress: (userId, email, callback) ->
|
|
|
|
query = _id: userId, 'emails.email': email
|
|
|
|
update = $set: email: email
|
|
|
|
@updateUser query, update, (error, res) ->
|
|
|
|
if error?
|
|
|
|
logger.err error:error, 'problem setting default emails'
|
|
|
|
return callback(error)
|
|
|
|
if res.nMatched == 0
|
|
|
|
return callback(new Error('Default email does not belong to user'))
|
|
|
|
callback()
|
|
|
|
|
|
|
|
|
|
|
|
# check for duplicate email address. This is also enforced at the DB level
|
|
|
|
_ensureUniqueEmailAddress: (newEmail, callback) ->
|
|
|
|
UserGetter.getUserByAnyEmail newEmail, (error, user) ->
|
|
|
|
return callback(message: 'alread_exists') if user?
|
|
|
|
callback()
|
|
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
'updateUser'
|
|
|
|
'changeEmailAddress'
|
|
|
|
'setDefaultEmailAddress'
|
|
|
|
'addEmailAddress'
|
|
|
|
'removeEmailAddress'
|
|
|
|
'_ensureUniqueEmailAddress'
|
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod(UserUpdater, method, 'mongo.UserUpdater', logger)
|