UserEmailsController.coffee 3.3 KB

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