UserEmailsController.coffee 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. logger.log req.params
  30. email = EmailHelper.parseEmail(req.params.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. showConfirm: (req, res, next) ->
  43. res.render 'user/confirm_email', {
  44. token: req.query.token,
  45. title: 'confirm_email'
  46. }
  47. confirm: (req, res, next) ->
  48. token = req.body.token
  49. if !token?
  50. return res.sendStatus 422
  51. UserEmailsConfirmationHandler.confirmEmailFromToken token, (error) ->
  52. if error?
  53. if error instanceof Errors.NotFoundError
  54. res.status(404).json({
  55. message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
  56. })
  57. else
  58. next(error)
  59. else
  60. res.sendStatus 200