UserUpdater.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. logger = require("logger-sharelatex")
  2. mongojs = require("../../infrastructure/mongojs")
  3. metrics = require("metrics-sharelatex")
  4. db = mongojs.db
  5. async = require("async")
  6. ObjectId = mongojs.ObjectId
  7. UserGetter = require("./UserGetter")
  8. { addAffiliation, removeAffiliation } = require("./UserAffiliationsManager")
  9. EmailHelper = require "../Helpers/EmailHelper"
  10. Errors = require "../Errors/Errors"
  11. module.exports = UserUpdater =
  12. updateUser: (query, update, callback = (error) ->) ->
  13. if typeof query == "string"
  14. query = _id: ObjectId(query)
  15. else if query instanceof ObjectId
  16. query = _id: query
  17. else if typeof query._id == "string"
  18. query._id = ObjectId(query._id)
  19. db.users.update query, update, callback
  20. #
  21. # DEPRECATED
  22. #
  23. # Change the user's main email address by adding a new email, switching the
  24. # default email and removing the old email. Prefer manipulating multiple
  25. # emails and the default rather than calling this method directly
  26. #
  27. changeEmailAddress: (userId, newEmail, callback)->
  28. logger.log userId: userId, newEmail: newEmail, "updaing email address of user"
  29. oldEmail = null
  30. async.series [
  31. (cb) ->
  32. UserGetter.getUserEmail userId, (error, email) ->
  33. oldEmail = email
  34. cb(error)
  35. (cb) -> UserUpdater.addEmailAddress userId, newEmail, cb
  36. (cb) -> UserUpdater.setDefaultEmailAddress userId, newEmail, cb
  37. (cb) -> UserUpdater.removeEmailAddress userId, oldEmail, cb
  38. ], callback
  39. # Add a new email address for the user. Email cannot be already used by this
  40. # or any other user
  41. addEmailAddress: (userId, newEmail, affiliationOptions, callback) ->
  42. unless callback? # affiliationOptions is optional
  43. callback = affiliationOptions
  44. affiliationOptions = {}
  45. UserGetter.ensureUniqueEmailAddress newEmail, (error) =>
  46. return callback(error) if error?
  47. addAffiliation userId, newEmail, affiliationOptions, (error) =>
  48. if error?
  49. logger.err error: error, 'problem adding affiliation'
  50. return callback(error)
  51. update = $push: emails: email: newEmail, createdAt: new Date()
  52. @updateUser userId, update, (error) ->
  53. if error?
  54. logger.err error: error, 'problem updating users emails'
  55. return callback(error)
  56. callback()
  57. # remove one of the user's email addresses. The email cannot be the user's
  58. # default email address
  59. removeEmailAddress: (userId, email, callback) ->
  60. removeAffiliation userId, email, (error) =>
  61. if error?
  62. logger.err error: error, 'problem removing affiliation'
  63. return callback(error)
  64. query = _id: userId, email: $ne: email
  65. update = $pull: emails: email: email
  66. @updateUser query, update, (error, res) ->
  67. if error?
  68. logger.err error:error, 'problem removing users email'
  69. return callback(error)
  70. if res.n == 0
  71. return callback(new Error('Cannot remove email'))
  72. callback()
  73. # set the default email address by setting the `email` attribute. The email
  74. # must be one of the user's multiple emails (`emails` attribute)
  75. setDefaultEmailAddress: (userId, email, callback) ->
  76. query = _id: userId, 'emails.email': email
  77. update = $set: email: email
  78. @updateUser query, update, (error, res) ->
  79. if error?
  80. logger.err error:error, 'problem setting default emails'
  81. return callback(error)
  82. if res.n == 0 # TODO: Check n or nMatched?
  83. return callback(new Error('Default email does not belong to user'))
  84. callback()
  85. confirmEmail: (userId, email, callback) ->
  86. email = EmailHelper.parseEmail(email)
  87. return callback(new Error('invalid email')) if !email?
  88. logger.log {userId, email}, 'confirming user email'
  89. query =
  90. _id: userId
  91. 'emails.email': email
  92. update =
  93. $set:
  94. 'emails.$.confirmedAt': new Date()
  95. @updateUser query, update, (error, res) ->
  96. return callback(error) if error?
  97. logger.log {res, userId, email}, "tried to confirm email"
  98. if res.n == 0
  99. return callback(new Errors.NotFoundError('user id and email do no match'))
  100. callback()
  101. [
  102. 'updateUser'
  103. 'changeEmailAddress'
  104. 'setDefaultEmailAddress'
  105. 'addEmailAddress'
  106. 'removeEmailAddress'
  107. ].map (method) ->
  108. metrics.timeAsyncMethod(UserUpdater, method, 'mongo.UserUpdater', logger)