UserEmailsController.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. AuthenticationController = require('../Authentication/AuthenticationController')
  2. UserGetter = require("./UserGetter")
  3. UserUpdater = require("./UserUpdater")
  4. EmailHelper = require("../Helpers/EmailHelper")
  5. UserEmailsConfirmationHandler = require "./UserEmailsConfirmationHandler"
  6. { endorseAffiliation } = require("./UserAffiliationsManager")
  7. logger = require("logger-sharelatex")
  8. Errors = require "../Errors/Errors"
  9. module.exports = UserEmailsController =
  10. list: (req, res, next) ->
  11. userId = AuthenticationController.getLoggedInUserId(req)
  12. UserGetter.getUserFullEmails userId, (error, fullEmails) ->
  13. return next(error) if error?
  14. res.json fullEmails
  15. add: (req, res, next) ->
  16. userId = AuthenticationController.getLoggedInUserId(req)
  17. email = EmailHelper.parseEmail(req.body.email)
  18. return res.sendStatus 422 unless email?
  19. affiliationOptions =
  20. university: req.body.university
  21. role: req.body.role
  22. department: req.body.department
  23. UserUpdater.addEmailAddress userId, email, affiliationOptions, (error)->
  24. return next(error) if error?
  25. UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (err) ->
  26. return next(error) if error?
  27. res.sendStatus 204
  28. remove: (req, res, next) ->
  29. userId = AuthenticationController.getLoggedInUserId(req)
  30. email = EmailHelper.parseEmail(req.body.email)
  31. return res.sendStatus 422 unless email?
  32. UserUpdater.removeEmailAddress userId, email, (error)->
  33. return next(error) if error?
  34. res.sendStatus 200
  35. setDefault: (req, res, next) ->
  36. userId = AuthenticationController.getLoggedInUserId(req)
  37. email = EmailHelper.parseEmail(req.body.email)
  38. return res.sendStatus 422 unless email?
  39. UserUpdater.updateV1AndSetDefaultEmailAddress userId, email, (error)->
  40. if error?
  41. if error instanceof Errors.UnconfirmedEmailError
  42. return res.sendStatus 409
  43. else if error instanceof Errors.EmailExistsError
  44. return res.status(409).json {
  45. error:
  46. message: "The email '#{email}' is already in use by another account"
  47. }
  48. else
  49. return next(error)
  50. else
  51. return res.sendStatus 200
  52. endorse: (req, res, next) ->
  53. userId = AuthenticationController.getLoggedInUserId(req)
  54. email = EmailHelper.parseEmail(req.body.email)
  55. return res.sendStatus 422 unless email?
  56. endorseAffiliation userId, email, req.body.role, req.body.department, (error)->
  57. return next(error) if error?
  58. res.sendStatus 204
  59. resendConfirmation: (req, res, next) ->
  60. userId = AuthenticationController.getLoggedInUserId(req)
  61. email = EmailHelper.parseEmail(req.body.email)
  62. return res.sendStatus 422 unless email?
  63. UserGetter.getUserByAnyEmail email, {_id:1}, (error, user) ->
  64. return next(error) if error?
  65. if !user? or user?._id?.toString() != userId
  66. logger.log {userId, email, foundUserId: user?._id}, "email doesn't match logged in user"
  67. return res.sendStatus 422
  68. logger.log {userId, email}, 'resending email confirmation token'
  69. UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (error) ->
  70. return next(error) if error?
  71. res.sendStatus 200
  72. showConfirm: (req, res, next) ->
  73. res.render 'user/confirm_email', {
  74. token: req.query.token,
  75. title: 'confirm_email'
  76. }
  77. confirm: (req, res, next) ->
  78. token = req.body.token
  79. if !token?
  80. return res.sendStatus 422
  81. UserEmailsConfirmationHandler.confirmEmailFromToken token, (error) ->
  82. if error?
  83. if error instanceof Errors.NotFoundError
  84. res.status(404).json({
  85. message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
  86. })
  87. else
  88. next(error)
  89. else
  90. res.sendStatus 200