UserEmailsController.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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("../Institutions/InstitutionsAPI")
  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. if error?
  25. return UserEmailsController._handleEmailError error, req, res, next
  26. UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (err) ->
  27. return next(error) if error?
  28. res.sendStatus 204
  29. remove: (req, res, next) ->
  30. userId = AuthenticationController.getLoggedInUserId(req)
  31. email = EmailHelper.parseEmail(req.body.email)
  32. return res.sendStatus 422 unless email?
  33. UserUpdater.removeEmailAddress userId, email, (error)->
  34. return next(error) if error?
  35. res.sendStatus 200
  36. setDefault: (req, res, next) ->
  37. userId = AuthenticationController.getLoggedInUserId(req)
  38. email = EmailHelper.parseEmail(req.body.email)
  39. return res.sendStatus 422 unless email?
  40. UserUpdater.updateV1AndSetDefaultEmailAddress userId, email, (error)->
  41. if error?
  42. return UserEmailsController._handleEmailError error, req, res, next
  43. else
  44. return res.sendStatus 200
  45. endorse: (req, res, next) ->
  46. userId = AuthenticationController.getLoggedInUserId(req)
  47. email = EmailHelper.parseEmail(req.body.email)
  48. return res.sendStatus 422 unless email?
  49. endorseAffiliation userId, email, req.body.role, req.body.department, (error)->
  50. return next(error) if error?
  51. res.sendStatus 204
  52. resendConfirmation: (req, res, next) ->
  53. userId = AuthenticationController.getLoggedInUserId(req)
  54. email = EmailHelper.parseEmail(req.body.email)
  55. return res.sendStatus 422 unless email?
  56. UserGetter.getUserByAnyEmail email, {_id:1}, (error, user) ->
  57. return next(error) if error?
  58. if !user? or user?._id?.toString() != userId
  59. logger.log {userId, email, foundUserId: user?._id}, "email doesn't match logged in user"
  60. return res.sendStatus 422
  61. logger.log {userId, email}, 'resending email confirmation token'
  62. UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (error) ->
  63. return next(error) if error?
  64. res.sendStatus 200
  65. showConfirm: (req, res, next) ->
  66. res.render 'user/confirm_email', {
  67. token: req.query.token,
  68. title: 'confirm_email'
  69. }
  70. confirm: (req, res, next) ->
  71. token = req.body.token
  72. if !token?
  73. return res.sendStatus 422
  74. UserEmailsConfirmationHandler.confirmEmailFromToken token, (error) ->
  75. if error?
  76. if error instanceof Errors.NotFoundError
  77. res.status(404).json({
  78. message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
  79. })
  80. else
  81. next(error)
  82. else
  83. res.sendStatus 200
  84. _handleEmailError: (error, req, res, next) ->
  85. if error instanceof Errors.UnconfirmedEmailError
  86. return res.status(409).json {
  87. message: 'email must be confirmed'
  88. }
  89. else if error instanceof Errors.EmailExistsError
  90. return res.status(409).json {
  91. message: req.i18n.translate("email_already_registered")
  92. }
  93. else
  94. return next(error)