2019-05-29 05:21:06 -04:00
|
|
|
const UserHandler = require('./UserHandler')
|
|
|
|
const UserDeleter = require('./UserDeleter')
|
|
|
|
const UserGetter = require('./UserGetter')
|
|
|
|
const { User } = require('../../models/User')
|
2019-08-28 08:59:41 -04:00
|
|
|
const NewsletterManager = require('../Newsletter/NewsletterManager')
|
2019-05-29 05:21:06 -04:00
|
|
|
const UserRegistrationHandler = require('./UserRegistrationHandler')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const metrics = require('metrics-sharelatex')
|
|
|
|
const AuthenticationManager = require('../Authentication/AuthenticationManager')
|
|
|
|
const AuthenticationController = require('../Authentication/AuthenticationController')
|
|
|
|
const UserSessionsManager = require('./UserSessionsManager')
|
|
|
|
const UserUpdater = require('./UserUpdater')
|
|
|
|
const SudoModeHandler = require('../SudoMode/SudoModeHandler')
|
|
|
|
const Errors = require('../Errors/Errors')
|
2019-07-19 05:40:23 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-07-31 04:22:31 -04:00
|
|
|
const HttpErrors = require('@overleaf/o-error/http')
|
2019-06-14 12:31:46 -04:00
|
|
|
const EmailHandler = require('../Email/EmailHandler')
|
2019-09-25 10:29:10 -04:00
|
|
|
const UrlHelper = require('../Helpers/UrlHelper')
|
2020-02-20 11:08:18 -05:00
|
|
|
const { promisify } = require('util')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
const UserController = {
|
2019-05-29 05:21:06 -04:00
|
|
|
tryDeleteUser(req, res, next) {
|
2019-08-28 08:59:41 -04:00
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
2019-05-29 05:21:06 -04:00
|
|
|
const { password } = req.body
|
2019-11-19 09:19:08 -05:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
if (password == null || password === '') {
|
|
|
|
logger.err(
|
2019-08-28 08:59:41 -04:00
|
|
|
{ userId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'no password supplied for attempt to delete account'
|
|
|
|
)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
AuthenticationManager.authenticate(
|
|
|
|
{ _id: userId },
|
|
|
|
password,
|
|
|
|
(err, user) => {
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn(
|
|
|
|
{ userId },
|
|
|
|
'error authenticating during attempt to delete account'
|
|
|
|
)
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
if (!user) {
|
|
|
|
logger.err({ userId }, 'auth failed during attempt to delete account')
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
UserDeleter.deleteUser(
|
|
|
|
userId,
|
|
|
|
{ deleterUser: user, ipAddress: req.ip },
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
let errorData = {
|
|
|
|
message: 'error while deleting user account',
|
|
|
|
info: { userId }
|
2019-07-19 05:40:23 -04:00
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
if (err instanceof Errors.SubscriptionAdminDeletionError) {
|
|
|
|
// set info.public.error for JSON response so frontend can display
|
|
|
|
// a specific message
|
|
|
|
errorData.info.public = {
|
|
|
|
error: 'SubscriptionAdminDeletionError'
|
|
|
|
}
|
|
|
|
return next(
|
|
|
|
new HttpErrors.UnprocessableEntityError(errorData).withCause(
|
|
|
|
err
|
|
|
|
)
|
2019-07-19 05:40:23 -04:00
|
|
|
)
|
2019-08-28 08:59:41 -04:00
|
|
|
} else {
|
|
|
|
return next(new OError(errorData).withCause(err))
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
const sessionId = req.sessionID
|
|
|
|
if (typeof req.logout === 'function') {
|
|
|
|
req.logout()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
req.session.destroy(err => {
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn({ err }, 'error destorying session')
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
UserSessionsManager.untrackSession(user, sessionId)
|
|
|
|
res.sendStatus(200)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
unsubscribe(req, res, next) {
|
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
UserGetter.getUser(userId, (err, user) => {
|
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
NewsletterManager.unsubscribe(user, err => {
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn(
|
|
|
|
{ err, user },
|
|
|
|
'Failed to unsubscribe user from newsletter'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
res.send()
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-07-31 04:22:31 -04:00
|
|
|
updateUserSettings(req, res, next) {
|
2019-08-28 08:59:41 -04:00
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
User.findById(userId, (err, user) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null || user == null) {
|
2019-08-28 08:59:41 -04:00
|
|
|
logger.err({ err, userId }, 'problem updaing user settings')
|
2019-05-29 05:21:06 -04:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.body.first_name != null) {
|
|
|
|
user.first_name = req.body.first_name.trim()
|
|
|
|
}
|
|
|
|
if (req.body.last_name != null) {
|
|
|
|
user.last_name = req.body.last_name.trim()
|
|
|
|
}
|
|
|
|
if (req.body.role != null) {
|
|
|
|
user.role = req.body.role.trim()
|
|
|
|
}
|
|
|
|
if (req.body.institution != null) {
|
|
|
|
user.institution = req.body.institution.trim()
|
|
|
|
}
|
|
|
|
if (req.body.mode != null) {
|
|
|
|
user.ace.mode = req.body.mode
|
|
|
|
}
|
|
|
|
if (req.body.editorTheme != null) {
|
|
|
|
user.ace.theme = req.body.editorTheme
|
|
|
|
}
|
|
|
|
if (req.body.overallTheme != null) {
|
|
|
|
user.ace.overallTheme = req.body.overallTheme
|
|
|
|
}
|
|
|
|
if (req.body.fontSize != null) {
|
|
|
|
user.ace.fontSize = req.body.fontSize
|
|
|
|
}
|
|
|
|
if (req.body.autoComplete != null) {
|
|
|
|
user.ace.autoComplete = req.body.autoComplete
|
|
|
|
}
|
|
|
|
if (req.body.autoPairDelimiters != null) {
|
|
|
|
user.ace.autoPairDelimiters = req.body.autoPairDelimiters
|
|
|
|
}
|
|
|
|
if (req.body.spellCheckLanguage != null) {
|
|
|
|
user.ace.spellCheckLanguage = req.body.spellCheckLanguage
|
|
|
|
}
|
|
|
|
if (req.body.pdfViewer != null) {
|
|
|
|
user.ace.pdfViewer = req.body.pdfViewer
|
|
|
|
}
|
|
|
|
if (req.body.syntaxValidation != null) {
|
|
|
|
user.ace.syntaxValidation = req.body.syntaxValidation
|
|
|
|
}
|
|
|
|
if (req.body.fontFamily != null) {
|
|
|
|
user.ace.fontFamily = req.body.fontFamily
|
|
|
|
}
|
|
|
|
if (req.body.lineHeight != null) {
|
|
|
|
user.ace.lineHeight = req.body.lineHeight
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
user.save(err => {
|
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
const newEmail =
|
|
|
|
req.body.email != null
|
|
|
|
? req.body.email.trim().toLowerCase()
|
|
|
|
: undefined
|
|
|
|
if (
|
|
|
|
newEmail == null ||
|
|
|
|
newEmail === user.email ||
|
|
|
|
req.externalAuthenticationSystemUsed()
|
|
|
|
) {
|
|
|
|
// end here, don't update email
|
|
|
|
AuthenticationController.setInSessionUser(req, {
|
|
|
|
first_name: user.first_name,
|
|
|
|
last_name: user.last_name
|
|
|
|
})
|
2019-08-28 08:59:41 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (newEmail.indexOf('@') === -1) {
|
|
|
|
// email invalid
|
2019-08-28 08:59:41 -04:00
|
|
|
res.sendStatus(400)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
// update the user email
|
2019-08-28 08:59:41 -04:00
|
|
|
UserUpdater.changeEmailAddress(userId, newEmail, err => {
|
2019-07-31 04:22:31 -04:00
|
|
|
if (err) {
|
|
|
|
let errorData = {
|
|
|
|
message: 'problem updaing users email address',
|
2019-08-28 08:59:41 -04:00
|
|
|
info: { userId, newEmail, public: {} }
|
2019-07-31 04:22:31 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err instanceof Errors.EmailExistsError) {
|
2019-07-31 04:22:31 -04:00
|
|
|
errorData.info.public.message = req.i18n.translate(
|
|
|
|
'email_already_registered'
|
|
|
|
)
|
|
|
|
return next(
|
|
|
|
new HttpErrors.ConflictError(errorData).withCause(err)
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-07-31 04:22:31 -04:00
|
|
|
errorData.info.public.message = req.i18n.translate(
|
|
|
|
'problem_changing_email_address'
|
|
|
|
)
|
|
|
|
next(
|
|
|
|
new HttpErrors.InternalServerError(errorData).withCause(err)
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
User.findById(userId, (err, user) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
logger.err(
|
2019-08-28 08:59:41 -04:00
|
|
|
{ err, userId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'error getting user for email update'
|
|
|
|
)
|
|
|
|
return res.send(500)
|
|
|
|
}
|
|
|
|
AuthenticationController.setInSessionUser(req, {
|
|
|
|
email: user.email,
|
|
|
|
first_name: user.first_name,
|
|
|
|
last_name: user.last_name
|
|
|
|
})
|
2019-08-28 08:59:41 -04:00
|
|
|
UserHandler.populateTeamInvites(user, err => {
|
2019-05-29 05:21:06 -04:00
|
|
|
// need to refresh this in the background
|
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'error populateTeamInvites')
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-02-20 11:08:18 -05:00
|
|
|
doLogout(req, cb) {
|
2019-05-29 05:21:06 -04:00
|
|
|
metrics.inc('user.logout')
|
|
|
|
const user = AuthenticationController.getSessionUser(req)
|
|
|
|
logger.log({ user }, 'logging out')
|
|
|
|
const sessionId = req.sessionID
|
|
|
|
if (typeof req.logout === 'function') {
|
|
|
|
req.logout()
|
|
|
|
} // passport logout
|
2019-08-28 08:59:41 -04:00
|
|
|
req.session.destroy(err => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ err }, 'error destorying session')
|
2019-08-28 08:59:41 -04:00
|
|
|
return cb(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (user != null) {
|
|
|
|
UserSessionsManager.untrackSession(user, sessionId)
|
|
|
|
SudoModeHandler.clearSudoMode(user._id)
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
cb()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
logout(req, res, next) {
|
2019-09-25 10:29:10 -04:00
|
|
|
const requestedRedirect = req.body.redirect
|
|
|
|
? UrlHelper.getSafeRedirectPath(req.body.redirect)
|
|
|
|
: undefined
|
|
|
|
const redirectUrl = requestedRedirect || '/login'
|
|
|
|
|
2020-02-20 11:08:18 -05:00
|
|
|
UserController.doLogout(req, err => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
res.redirect(redirectUrl)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
expireDeletedUser(req, res, next) {
|
|
|
|
const userId = req.params.userId
|
|
|
|
UserDeleter.expireDeletedUser(userId, error => {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
expireDeletedUsersAfterDuration(req, res, next) {
|
|
|
|
UserDeleter.expireDeletedUsersAfterDuration(error => {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
register(req, res, next) {
|
|
|
|
const { email } = req.body
|
|
|
|
if (email == null || email === '') {
|
2019-08-28 08:59:41 -04:00
|
|
|
return res.sendStatus(422) // Unprocessable Entity
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
UserRegistrationHandler.registerNewUserAndSendActivationEmail(
|
2019-05-29 05:21:06 -04:00
|
|
|
email,
|
2019-08-28 08:59:41 -04:00
|
|
|
(error, user, setNewPasswordUrl) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
res.json({
|
2019-05-29 05:21:06 -04:00
|
|
|
email: user.email,
|
|
|
|
setNewPasswordUrl
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
clearSessions(req, res, next) {
|
|
|
|
metrics.inc('user.clear-sessions')
|
|
|
|
const user = AuthenticationController.getSessionUser(req)
|
2019-08-28 08:59:41 -04:00
|
|
|
UserSessionsManager.revokeAllUserSessions(user, [req.sessionID], err => {
|
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
res.sendStatus(201)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
changePassword(req, res, next) {
|
|
|
|
metrics.inc('user.password-change')
|
2019-06-14 12:31:46 -04:00
|
|
|
const internalError = {
|
|
|
|
message: { type: 'error', text: req.i18n.translate('internal_error') }
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
2019-06-14 12:31:46 -04:00
|
|
|
AuthenticationManager.authenticate(
|
2019-08-28 08:59:41 -04:00
|
|
|
{ _id: userId },
|
2019-06-14 12:31:46 -04:00
|
|
|
req.body.currentPassword,
|
|
|
|
(err, user) => {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500).json(internalError)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-14 12:31:46 -04:00
|
|
|
if (!user) {
|
|
|
|
return res.status(400).json({
|
2019-05-29 05:21:06 -04:00
|
|
|
message: {
|
|
|
|
type: 'error',
|
|
|
|
text: 'Your old password is wrong'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-06-14 12:31:46 -04:00
|
|
|
if (req.body.newPassword1 !== req.body.newPassword2) {
|
|
|
|
return res.status(400).json({
|
|
|
|
message: {
|
|
|
|
type: 'error',
|
|
|
|
text: req.i18n.translate('password_change_passwords_do_not_match')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const validationError = AuthenticationManager.validatePassword(
|
|
|
|
req.body.newPassword1
|
|
|
|
)
|
|
|
|
if (validationError != null) {
|
|
|
|
return res.status(400).json({
|
|
|
|
message: {
|
|
|
|
type: 'error',
|
|
|
|
text: validationError.message
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
AuthenticationManager.setUserPassword(
|
|
|
|
user._id,
|
|
|
|
req.body.newPassword1,
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500).json(internalError)
|
|
|
|
}
|
|
|
|
// log errors but do not wait for response
|
|
|
|
EmailHandler.sendEmail(
|
|
|
|
'passwordChanged',
|
|
|
|
{ to: user.email },
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
logger.warn(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
UserSessionsManager.revokeAllUserSessions(
|
|
|
|
user,
|
|
|
|
[req.sessionID],
|
|
|
|
err => {
|
|
|
|
if (err != null) {
|
|
|
|
return res.status(500).json(internalError)
|
|
|
|
}
|
|
|
|
res.json({
|
|
|
|
message: {
|
|
|
|
type: 'success',
|
|
|
|
email: user.email,
|
|
|
|
text: req.i18n.translate('password_change_successful')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
|
2020-02-20 11:08:18 -05:00
|
|
|
UserController.promises = {
|
|
|
|
doLogout: promisify(UserController.doLogout)
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
module.exports = UserController
|