UserEmailsController.js 5.6 KB

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