UserEmailsController.coffee 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. AuthenticationController = require('../Authentication/AuthenticationController')
  2. UserGetter = require("./UserGetter")
  3. UserUpdater = require("./UserUpdater")
  4. EmailHelper = require("../Helpers/EmailHelper")
  5. UserEmailsConfirmationHandler = require "./UserEmailsConfirmationHandler"
  6. logger = require("logger-sharelatex")
  7. Errors = require "../Errors/Errors"
  8. module.exports = UserEmailsController =
  9. list: (req, res, next) ->
  10. userId = AuthenticationController.getLoggedInUserId(req)
  11. UserGetter.getUserFullEmails userId, (error, fullEmails) ->
  12. return next(error) if error?
  13. res.json fullEmails
  14. add: (req, res, next) ->
  15. userId = AuthenticationController.getLoggedInUserId(req)
  16. email = EmailHelper.parseEmail(req.body.email)
  17. return res.sendStatus 422 unless email?
  18. affiliationOptions =
  19. university: req.body.university
  20. role: req.body.role
  21. department: req.body.department
  22. UserUpdater.addEmailAddress userId, email, affiliationOptions, (error)->
  23. return next(error) if error?
  24. UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (err) ->
  25. return next(error) if error?
  26. res.sendStatus 204
  27. remove: (req, res, next) ->
  28. userId = AuthenticationController.getLoggedInUserId(req)
  29. email = EmailHelper.parseEmail(req.body.email)
  30. return res.sendStatus 422 unless email?
  31. UserUpdater.removeEmailAddress userId, email, (error)->
  32. return next(error) if error?
  33. res.sendStatus 200
  34. setDefault: (req, res, next) ->
  35. userId = AuthenticationController.getLoggedInUserId(req)
  36. email = EmailHelper.parseEmail(req.body.email)
  37. return res.sendStatus 422 unless email?
  38. UserUpdater.setDefaultEmailAddress userId, email, (error)->
  39. return next(error) if error?
  40. res.sendStatus 200
  41. showConfirm: (req, res, next) ->
  42. res.render 'user/confirm_email', {
  43. token: req.query.token,
  44. title: 'confirm_email'
  45. }
  46. confirm: (req, res, next) ->
  47. token = req.body.token
  48. if !token?
  49. return res.sendStatus 422
  50. UserEmailsConfirmationHandler.confirmEmailFromToken token, (error) ->
  51. if error?
  52. if error instanceof Errors.NotFoundError
  53. res.status(404).json({
  54. message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
  55. })
  56. else
  57. next(error)
  58. else
  59. res.sendStatus 200