2018-06-08 17:05:19 +00:00
|
|
|
AuthenticationController = require('../Authentication/AuthenticationController')
|
|
|
|
UserGetter = require("./UserGetter")
|
|
|
|
UserUpdater = require("./UserUpdater")
|
|
|
|
EmailHelper = require("../Helpers/EmailHelper")
|
2018-06-19 12:55:34 +00:00
|
|
|
UserEmailsConfirmationHandler = require "./UserEmailsConfirmationHandler"
|
2018-07-10 19:49:24 +00:00
|
|
|
{ endorseAffiliation } = require("../Institutions/InstitutionsAPI")
|
2018-06-08 17:05:19 +00:00
|
|
|
logger = require("logger-sharelatex")
|
2018-06-19 12:55:34 +00:00
|
|
|
Errors = require "../Errors/Errors"
|
2018-06-08 17:05:19 +00:00
|
|
|
|
|
|
|
module.exports = UserEmailsController =
|
|
|
|
|
2018-06-19 12:55:34 +00:00
|
|
|
list: (req, res, next) ->
|
2018-06-08 17:05:19 +00:00
|
|
|
userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
UserGetter.getUserFullEmails userId, (error, fullEmails) ->
|
2018-06-19 12:55:34 +00:00
|
|
|
return next(error) if error?
|
2018-06-08 17:05:19 +00:00
|
|
|
res.json fullEmails
|
|
|
|
|
|
|
|
|
2018-06-19 12:55:34 +00:00
|
|
|
add: (req, res, next) ->
|
2018-06-08 17:05:19 +00:00
|
|
|
userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
email = EmailHelper.parseEmail(req.body.email)
|
|
|
|
return res.sendStatus 422 unless email?
|
|
|
|
|
2018-06-19 11:46:15 +00:00
|
|
|
affiliationOptions =
|
|
|
|
university: req.body.university
|
|
|
|
role: req.body.role
|
|
|
|
department: req.body.department
|
|
|
|
UserUpdater.addEmailAddress userId, email, affiliationOptions, (error)->
|
2018-07-17 10:12:09 +00:00
|
|
|
if error?
|
|
|
|
return UserEmailsController._handleEmailError error, req, res, next
|
2018-06-19 12:55:34 +00:00
|
|
|
UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (err) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.sendStatus 204
|
2018-06-08 17:05:19 +00:00
|
|
|
|
|
|
|
|
2018-06-19 12:55:34 +00:00
|
|
|
remove: (req, res, next) ->
|
2018-06-08 17:05:19 +00:00
|
|
|
userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
email = EmailHelper.parseEmail(req.body.email)
|
|
|
|
return res.sendStatus 422 unless email?
|
|
|
|
|
|
|
|
UserUpdater.removeEmailAddress userId, email, (error)->
|
2018-06-19 12:55:34 +00:00
|
|
|
return next(error) if error?
|
2018-06-08 17:05:19 +00:00
|
|
|
res.sendStatus 200
|
|
|
|
|
|
|
|
|
2018-06-19 12:55:34 +00:00
|
|
|
setDefault: (req, res, next) ->
|
2018-06-08 17:05:19 +00:00
|
|
|
userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
email = EmailHelper.parseEmail(req.body.email)
|
|
|
|
return res.sendStatus 422 unless email?
|
|
|
|
|
2018-07-16 12:26:52 +00:00
|
|
|
UserUpdater.updateV1AndSetDefaultEmailAddress userId, email, (error)->
|
|
|
|
if error?
|
2018-07-17 10:12:09 +00:00
|
|
|
return UserEmailsController._handleEmailError error, req, res, next
|
2018-07-16 12:26:52 +00:00
|
|
|
else
|
|
|
|
return res.sendStatus 200
|
2018-06-19 12:55:34 +00:00
|
|
|
|
2018-07-05 08:46:06 +00:00
|
|
|
|
|
|
|
endorse: (req, res, next) ->
|
|
|
|
userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
email = EmailHelper.parseEmail(req.body.email)
|
|
|
|
return res.sendStatus 422 unless email?
|
|
|
|
|
|
|
|
endorseAffiliation userId, email, req.body.role, req.body.department, (error)->
|
|
|
|
return next(error) if error?
|
|
|
|
res.sendStatus 204
|
|
|
|
|
2018-07-12 15:39:04 +00:00
|
|
|
resendConfirmation: (req, res, next) ->
|
|
|
|
userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
email = EmailHelper.parseEmail(req.body.email)
|
|
|
|
return res.sendStatus 422 unless email?
|
|
|
|
UserGetter.getUserByAnyEmail email, {_id:1}, (error, user) ->
|
|
|
|
return next(error) if error?
|
|
|
|
if !user? or user?._id?.toString() != userId
|
|
|
|
logger.log {userId, email, foundUserId: user?._id}, "email doesn't match logged in user"
|
|
|
|
return res.sendStatus 422
|
|
|
|
logger.log {userId, email}, 'resending email confirmation token'
|
2018-07-13 09:42:31 +00:00
|
|
|
UserEmailsConfirmationHandler.sendConfirmationEmail userId, email, (error) ->
|
2018-07-12 15:39:04 +00:00
|
|
|
return next(error) if error?
|
|
|
|
res.sendStatus 200
|
2018-07-05 08:46:06 +00:00
|
|
|
|
2018-06-19 12:55:34 +00:00
|
|
|
showConfirm: (req, res, next) ->
|
|
|
|
res.render 'user/confirm_email', {
|
|
|
|
token: req.query.token,
|
|
|
|
title: 'confirm_email'
|
|
|
|
}
|
|
|
|
|
|
|
|
confirm: (req, res, next) ->
|
|
|
|
token = req.body.token
|
|
|
|
if !token?
|
|
|
|
return res.sendStatus 422
|
|
|
|
UserEmailsConfirmationHandler.confirmEmailFromToken token, (error) ->
|
|
|
|
if error?
|
|
|
|
if error instanceof Errors.NotFoundError
|
|
|
|
res.status(404).json({
|
|
|
|
message: 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
|
|
|
|
})
|
|
|
|
else
|
|
|
|
next(error)
|
|
|
|
else
|
2018-06-19 11:46:15 +00:00
|
|
|
res.sendStatus 200
|
2018-07-17 10:12:09 +00:00
|
|
|
|
|
|
|
_handleEmailError: (error, req, res, next) ->
|
|
|
|
if error instanceof Errors.UnconfirmedEmailError
|
|
|
|
return res.status(409).json {
|
|
|
|
message: 'email must be confirmed'
|
|
|
|
}
|
|
|
|
else if error instanceof Errors.EmailExistsError
|
|
|
|
return res.status(409).json {
|
|
|
|
message: req.i18n.translate("email_already_registered")
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return next(error)
|