UserEmailsController.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const AuthenticationController = require('../Authentication/AuthenticationController')
  2. const UserGetter = require('./UserGetter')
  3. const UserUpdater = require('./UserUpdater')
  4. const EmailHandler = require('../Email/EmailHandler')
  5. const EmailHelper = require('../Helpers/EmailHelper')
  6. const UserAuditLogHandler = require('./UserAuditLogHandler')
  7. const UserEmailsConfirmationHandler = require('./UserEmailsConfirmationHandler')
  8. const { endorseAffiliation } = require('../Institutions/InstitutionsAPI')
  9. const Errors = require('../Errors/Errors')
  10. const HttpErrorHandler = require('../Errors/HttpErrorHandler')
  11. const { expressify } = require('../../util/promises')
  12. async function add(req, res, next) {
  13. const userId = AuthenticationController.getLoggedInUserId(req)
  14. const email = EmailHelper.parseEmail(req.body.email)
  15. if (!email) {
  16. return res.sendStatus(422)
  17. }
  18. const user = await UserGetter.promises.getUser(userId, { email: 1 })
  19. const affiliationOptions = {
  20. university: req.body.university,
  21. role: req.body.role,
  22. department: req.body.department
  23. }
  24. await UserAuditLogHandler.promises.addEntry(
  25. user._id,
  26. 'add-email',
  27. user._id,
  28. req.ip,
  29. {
  30. newSecondaryEmail: email
  31. }
  32. )
  33. try {
  34. await UserUpdater.promises.addEmailAddress(
  35. userId,
  36. email,
  37. affiliationOptions
  38. )
  39. } catch (error) {
  40. return UserEmailsController._handleEmailError(error, req, res, next)
  41. }
  42. const emailOptions = {
  43. to: user.email,
  44. actionDescribed: `a secondary email address has been added to your account ${
  45. user.email
  46. }`,
  47. message: [
  48. `<span style="display:inline-block;padding: 0 20px;width:100%;">Added: <br/><b>${email}</b></span>`
  49. ],
  50. action: 'secondary email address added'
  51. }
  52. await EmailHandler.promises.sendEmail('securityAlert', emailOptions)
  53. await UserEmailsConfirmationHandler.promises.sendConfirmationEmail(
  54. userId,
  55. email
  56. )
  57. res.sendStatus(204)
  58. }
  59. function resendConfirmation(req, res, next) {
  60. const userId = AuthenticationController.getLoggedInUserId(req)
  61. const email = EmailHelper.parseEmail(req.body.email)
  62. if (!email) {
  63. return res.sendStatus(422)
  64. }
  65. UserGetter.getUserByAnyEmail(email, { _id: 1 }, function(error, user) {
  66. if (error) {
  67. return next(error)
  68. }
  69. if (!user || user._id.toString() !== userId) {
  70. return res.sendStatus(422)
  71. }
  72. UserEmailsConfirmationHandler.sendConfirmationEmail(userId, email, function(
  73. error
  74. ) {
  75. if (error) {
  76. return next(error)
  77. }
  78. res.sendStatus(200)
  79. })
  80. })
  81. }
  82. const UserEmailsController = {
  83. list(req, res, next) {
  84. const userId = AuthenticationController.getLoggedInUserId(req)
  85. UserGetter.getUserFullEmails(userId, function(error, fullEmails) {
  86. if (error) {
  87. return next(error)
  88. }
  89. res.json(fullEmails)
  90. })
  91. },
  92. add: expressify(add),
  93. remove(req, res, next) {
  94. const userId = AuthenticationController.getLoggedInUserId(req)
  95. const email = EmailHelper.parseEmail(req.body.email)
  96. if (!email) {
  97. return res.sendStatus(422)
  98. }
  99. UserUpdater.removeEmailAddress(userId, email, function(error) {
  100. if (error) {
  101. return next(error)
  102. }
  103. res.sendStatus(200)
  104. })
  105. },
  106. setDefault(req, res, next) {
  107. const userId = AuthenticationController.getLoggedInUserId(req)
  108. const email = EmailHelper.parseEmail(req.body.email)
  109. if (!email) {
  110. return res.sendStatus(422)
  111. }
  112. UserUpdater.setDefaultEmailAddress(userId, email, false, err => {
  113. if (err) {
  114. return UserEmailsController._handleEmailError(err, req, res, next)
  115. }
  116. AuthenticationController.setInSessionUser(req, { email: email })
  117. res.sendStatus(200)
  118. })
  119. },
  120. endorse(req, res, next) {
  121. const userId = AuthenticationController.getLoggedInUserId(req)
  122. const email = EmailHelper.parseEmail(req.body.email)
  123. if (!email) {
  124. return res.sendStatus(422)
  125. }
  126. endorseAffiliation(
  127. userId,
  128. email,
  129. req.body.role,
  130. req.body.department,
  131. function(error) {
  132. if (error) {
  133. return next(error)
  134. }
  135. res.sendStatus(204)
  136. }
  137. )
  138. },
  139. resendConfirmation,
  140. showConfirm(req, res, next) {
  141. res.render('user/confirm_email', {
  142. token: req.query.token,
  143. title: 'confirm_email'
  144. })
  145. },
  146. confirm(req, res, next) {
  147. const { token } = req.body
  148. if (!token) {
  149. return res.status(422).json({
  150. message: req.i18n.translate('confirmation_link_broken')
  151. })
  152. }
  153. UserEmailsConfirmationHandler.confirmEmailFromToken(token, function(error) {
  154. if (error) {
  155. if (error instanceof Errors.NotFoundError) {
  156. res.status(404).json({
  157. message: req.i18n.translate('confirmation_token_invalid')
  158. })
  159. } else {
  160. next(error)
  161. }
  162. } else {
  163. res.sendStatus(200)
  164. }
  165. })
  166. },
  167. _handleEmailError(error, req, res, next) {
  168. if (error instanceof Errors.UnconfirmedEmailError) {
  169. return HttpErrorHandler.conflict(req, res, 'email must be confirmed')
  170. } else if (error instanceof Errors.EmailExistsError) {
  171. const message = req.i18n.translate('email_already_registered')
  172. return HttpErrorHandler.conflict(req, res, message)
  173. } else if (error.message === '422: Email does not belong to university') {
  174. const message = req.i18n.translate('email_does_not_belong_to_university')
  175. return HttpErrorHandler.conflict(req, res, message)
  176. }
  177. next(error)
  178. }
  179. }
  180. module.exports = UserEmailsController