UserEmailsController.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. let UserEmailsController
  2. const AuthenticationController = require('../Authentication/AuthenticationController')
  3. const UserGetter = require('./UserGetter')
  4. const UserUpdater = require('./UserUpdater')
  5. const EmailHelper = require('../Helpers/EmailHelper')
  6. const UserEmailsConfirmationHandler = require('./UserEmailsConfirmationHandler')
  7. const { endorseAffiliation } = require('../Institutions/InstitutionsAPI')
  8. const Errors = require('../Errors/Errors')
  9. const HttpErrorHandler = require('../Errors/HttpErrorHandler')
  10. const HttpErrors = require('@overleaf/o-error/http')
  11. function add(req, res, next) {
  12. const userId = AuthenticationController.getLoggedInUserId(req)
  13. const email = EmailHelper.parseEmail(req.body.email)
  14. if (!email) {
  15. return res.sendStatus(422)
  16. }
  17. const affiliationOptions = {
  18. university: req.body.university,
  19. role: req.body.role,
  20. department: req.body.department
  21. }
  22. UserUpdater.addEmailAddress(userId, email, affiliationOptions, function(
  23. error
  24. ) {
  25. if (error) {
  26. return UserEmailsController._handleEmailError(error, req, res, next)
  27. }
  28. UserEmailsConfirmationHandler.sendConfirmationEmail(userId, email, function(
  29. error
  30. ) {
  31. if (error) {
  32. return next(error)
  33. }
  34. res.sendStatus(204)
  35. })
  36. })
  37. }
  38. function resendConfirmation(req, res, next) {
  39. const userId = AuthenticationController.getLoggedInUserId(req)
  40. const email = EmailHelper.parseEmail(req.body.email)
  41. if (!email) {
  42. return res.sendStatus(422)
  43. }
  44. UserGetter.getUserByAnyEmail(email, { _id: 1 }, function(error, user) {
  45. if (error) {
  46. return next(error)
  47. }
  48. if (!user || user._id.toString() !== userId) {
  49. return res.sendStatus(422)
  50. }
  51. UserEmailsConfirmationHandler.sendConfirmationEmail(userId, email, function(
  52. error
  53. ) {
  54. if (error) {
  55. return next(error)
  56. }
  57. res.sendStatus(200)
  58. })
  59. })
  60. }
  61. module.exports = UserEmailsController = {
  62. list(req, res, next) {
  63. const userId = AuthenticationController.getLoggedInUserId(req)
  64. UserGetter.getUserFullEmails(userId, function(error, fullEmails) {
  65. if (error) {
  66. return next(error)
  67. }
  68. res.json(fullEmails)
  69. })
  70. },
  71. add,
  72. remove(req, res, next) {
  73. const userId = AuthenticationController.getLoggedInUserId(req)
  74. const email = EmailHelper.parseEmail(req.body.email)
  75. if (!email) {
  76. return res.sendStatus(422)
  77. }
  78. UserUpdater.removeEmailAddress(userId, email, function(error) {
  79. if (error) {
  80. return next(error)
  81. }
  82. res.sendStatus(200)
  83. })
  84. },
  85. setDefault(req, res, next) {
  86. const userId = AuthenticationController.getLoggedInUserId(req)
  87. const email = EmailHelper.parseEmail(req.body.email)
  88. if (!email) {
  89. return res.sendStatus(422)
  90. }
  91. UserUpdater.setDefaultEmailAddress(userId, email, err => {
  92. if (err) {
  93. return UserEmailsController._handleEmailError(err, req, res, next)
  94. }
  95. AuthenticationController.setInSessionUser(req, { email: email })
  96. res.sendStatus(200)
  97. })
  98. },
  99. endorse(req, res, next) {
  100. const userId = AuthenticationController.getLoggedInUserId(req)
  101. const email = EmailHelper.parseEmail(req.body.email)
  102. if (!email) {
  103. return res.sendStatus(422)
  104. }
  105. endorseAffiliation(
  106. userId,
  107. email,
  108. req.body.role,
  109. req.body.department,
  110. function(error) {
  111. if (error) {
  112. return next(error)
  113. }
  114. res.sendStatus(204)
  115. }
  116. )
  117. },
  118. resendConfirmation,
  119. showConfirm(req, res, next) {
  120. res.render('user/confirm_email', {
  121. token: req.query.token,
  122. title: 'confirm_email'
  123. })
  124. },
  125. confirm(req, res, next) {
  126. const { token } = req.body
  127. if (!token) {
  128. return res.status(422).json({
  129. message: req.i18n.translate('confirmation_link_broken')
  130. })
  131. }
  132. UserEmailsConfirmationHandler.confirmEmailFromToken(token, function(error) {
  133. if (error) {
  134. if (error instanceof Errors.NotFoundError) {
  135. res.status(404).json({
  136. message: req.i18n.translate('confirmation_token_invalid')
  137. })
  138. } else {
  139. next(error)
  140. }
  141. } else {
  142. res.sendStatus(200)
  143. }
  144. })
  145. },
  146. _handleEmailError(error, req, res, next) {
  147. if (error instanceof Errors.UnconfirmedEmailError) {
  148. return HttpErrorHandler.conflict(req, res, 'email must be confirmed')
  149. } else if (error instanceof Errors.EmailExistsError) {
  150. const message = req.i18n.translate('email_already_registered')
  151. return HttpErrorHandler.conflict(req, res, message)
  152. } else if (error.message === '422: Email does not belong to university') {
  153. const message = req.i18n.translate('email_does_not_belong_to_university')
  154. return HttpErrorHandler.conflict(req, res, message)
  155. }
  156. next(new HttpErrors.InternalServerError().withCause(error))
  157. }
  158. }