UserEmailsController.coffee 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. showConfirm: (req, res, next) ->
  50. res.render 'user/confirm_email', {
  51. token: req.query.token,
  52. title: 'confirm_email'
  53. }
  54. confirm: (req, res, next) ->
  55. token = req.body.token
  56. if !token?
  57. return res.sendStatus 422
  58. UserEmailsConfirmationHandler.confirmEmailFromToken token, (error) ->
  59. if error?
  60. if error instanceof Errors.NotFoundError
  61. res.status(404).json({
  62. message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
  63. })
  64. else
  65. next(error)
  66. else
  67. res.sendStatus 200