UserEmailsController.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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
  44. return next(error)
  45. else
  46. return res.sendStatus 200
  47. endorse: (req, res, next) ->
  48. userId = AuthenticationController.getLoggedInUserId(req)
  49. email = EmailHelper.parseEmail(req.body.email)
  50. return res.sendStatus 422 unless email?
  51. endorseAffiliation userId, email, req.body.role, req.body.department, (error)->
  52. return next(error) if error?
  53. res.sendStatus 204
  54. resendConfirmation: (req, res, next) ->
  55. userId = AuthenticationController.getLoggedInUserId(req)
  56. email = EmailHelper.parseEmail(req.body.email)
  57. return res.sendStatus 422 unless email?
  58. UserGetter.getUserByAnyEmail email, {_id:1}, (error, user) ->
  59. return next(error) if error?
  60. if !user? or user?._id?.toString() != userId
  61. logger.log {userId, email, foundUserId: user?._id}, "email doesn't match logged in user"
  62. return res.sendStatus 422
  63. logger.log {userId, email}, 'resending email confirmation token'
  64. UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (error) ->
  65. return next(error) if error?
  66. res.sendStatus 200
  67. showConfirm: (req, res, next) ->
  68. res.render 'user/confirm_email', {
  69. token: req.query.token,
  70. title: 'confirm_email'
  71. }
  72. confirm: (req, res, next) ->
  73. token = req.body.token
  74. if !token?
  75. return res.sendStatus 422
  76. UserEmailsConfirmationHandler.confirmEmailFromToken token, (error) ->
  77. if error?
  78. if error instanceof Errors.NotFoundError
  79. res.status(404).json({
  80. message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
  81. })
  82. else
  83. next(error)
  84. else
  85. res.sendStatus 200