2019-05-29 05:21:06 -04:00
|
|
|
let UserEmailsController
|
|
|
|
const AuthenticationController = require('../Authentication/AuthenticationController')
|
|
|
|
const UserGetter = require('./UserGetter')
|
|
|
|
const UserUpdater = require('./UserUpdater')
|
|
|
|
const EmailHelper = require('../Helpers/EmailHelper')
|
|
|
|
const UserEmailsConfirmationHandler = require('./UserEmailsConfirmationHandler')
|
|
|
|
const { endorseAffiliation } = require('../Institutions/InstitutionsAPI')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const Errors = require('../Errors/Errors')
|
2019-07-31 04:22:31 -04:00
|
|
|
const HttpErrors = require('@overleaf/o-error/http')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
module.exports = UserEmailsController = {
|
|
|
|
list(req, res, next) {
|
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
2019-09-24 04:43:43 -04:00
|
|
|
UserGetter.getUserFullEmails(userId, function(error, fullEmails) {
|
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
res.json(fullEmails)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
add(req, res, next) {
|
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
const email = EmailHelper.parseEmail(req.body.email)
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!email) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return res.sendStatus(422)
|
|
|
|
}
|
|
|
|
|
|
|
|
const affiliationOptions = {
|
|
|
|
university: req.body.university,
|
|
|
|
role: req.body.role,
|
|
|
|
department: req.body.department
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
UserUpdater.addEmailAddress(userId, email, affiliationOptions, function(
|
|
|
|
error
|
|
|
|
) {
|
|
|
|
if (error) {
|
|
|
|
return UserEmailsController._handleEmailError(error, req, res, next)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
UserEmailsConfirmationHandler.sendConfirmationEmail(
|
|
|
|
userId,
|
|
|
|
email,
|
|
|
|
function(error) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
res.sendStatus(204)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
remove(req, res, next) {
|
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
const email = EmailHelper.parseEmail(req.body.email)
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!email) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return res.sendStatus(422)
|
|
|
|
}
|
|
|
|
|
2019-09-24 04:43:43 -04:00
|
|
|
UserUpdater.removeEmailAddress(userId, email, function(error) {
|
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
setDefault(req, res, next) {
|
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
const email = EmailHelper.parseEmail(req.body.email)
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!email) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return res.sendStatus(422)
|
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
UserUpdater.setDefaultEmailAddress(userId, email, err => {
|
|
|
|
if (err) {
|
|
|
|
return UserEmailsController._handleEmailError(err, req, res, next)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-07 10:04:18 -04:00
|
|
|
AuthenticationController.setInSessionUser(req, { email: email })
|
2019-07-22 12:55:35 -04:00
|
|
|
res.sendStatus(200)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
endorse(req, res, next) {
|
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
const email = EmailHelper.parseEmail(req.body.email)
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!email) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return res.sendStatus(422)
|
|
|
|
}
|
|
|
|
|
2019-09-24 04:43:43 -04:00
|
|
|
endorseAffiliation(
|
2019-05-29 05:21:06 -04:00
|
|
|
userId,
|
|
|
|
email,
|
|
|
|
req.body.role,
|
|
|
|
req.body.department,
|
|
|
|
function(error) {
|
2019-09-24 04:43:43 -04:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
res.sendStatus(204)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
resendConfirmation(req, res, next) {
|
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
const email = EmailHelper.parseEmail(req.body.email)
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!email) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return res.sendStatus(422)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
UserGetter.getUserByAnyEmail(email, { _id: 1 }, function(error, user) {
|
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!user || user._id.toString() !== userId) {
|
2019-05-29 05:21:06 -04:00
|
|
|
logger.log(
|
2019-09-24 04:43:43 -04:00
|
|
|
{ userId, email, foundUserId: user && user._id },
|
2019-05-29 05:21:06 -04:00
|
|
|
"email doesn't match logged in user"
|
|
|
|
)
|
|
|
|
return res.sendStatus(422)
|
|
|
|
}
|
|
|
|
logger.log({ userId, email }, 'resending email confirmation token')
|
2019-09-24 04:43:43 -04:00
|
|
|
UserEmailsConfirmationHandler.sendConfirmationEmail(
|
2019-05-29 05:21:06 -04:00
|
|
|
userId,
|
|
|
|
email,
|
|
|
|
function(error) {
|
2019-09-24 04:43:43 -04:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
showConfirm(req, res, next) {
|
2019-09-24 04:43:43 -04:00
|
|
|
res.render('user/confirm_email', {
|
2019-05-29 05:21:06 -04:00
|
|
|
token: req.query.token,
|
|
|
|
title: 'confirm_email'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
confirm(req, res, next) {
|
|
|
|
const { token } = req.body
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!token) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return res.sendStatus(422)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
UserEmailsConfirmationHandler.confirmEmailFromToken(token, function(error) {
|
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error instanceof Errors.NotFoundError) {
|
2019-09-24 04:43:43 -04:00
|
|
|
res.status(404).json({
|
2019-05-29 05:21:06 -04:00
|
|
|
message:
|
|
|
|
'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
|
|
|
|
})
|
|
|
|
} else {
|
2019-09-24 04:43:43 -04:00
|
|
|
next(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-24 04:43:43 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleEmailError(error, req, res, next) {
|
|
|
|
if (error instanceof Errors.UnconfirmedEmailError) {
|
2019-07-31 04:22:31 -04:00
|
|
|
return next(
|
|
|
|
new HttpErrors.ConflictError({
|
|
|
|
info: {
|
|
|
|
public: { message: 'email must be confirmed' }
|
|
|
|
}
|
|
|
|
}).withCause(error)
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (error instanceof Errors.EmailExistsError) {
|
2019-07-31 04:22:31 -04:00
|
|
|
return next(
|
|
|
|
new HttpErrors.ConflictError({
|
|
|
|
info: {
|
|
|
|
public: { message: req.i18n.translate('email_already_registered') }
|
|
|
|
}
|
|
|
|
}).withCause(error)
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-07-31 04:22:31 -04:00
|
|
|
next(new HttpErrors.InternalServerError().withCause(error))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|