Merge pull request #2634 from overleaf/jel-ensure-affiliations

Ensure affiliations after loading the settings page

GitOrigin-RevId: bf9982fb436d0e75d6fbd8418ca2d19e00fe3b66
This commit is contained in:
Eric Mc Sween
2020-03-02 07:32:12 -05:00
committed by Copybot
parent 864394d4ad
commit b2ee3bf2bb
5 changed files with 308 additions and 103 deletions
@@ -8,6 +8,7 @@ const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex')
const AuthenticationManager = require('../Authentication/AuthenticationManager')
const AuthenticationController = require('../Authentication/AuthenticationController')
const Features = require('../../infrastructure/Features')
const UserSessionsManager = require('./UserSessionsManager')
const UserUpdater = require('./UserUpdater')
const SudoModeHandler = require('../SudoMode/SudoModeHandler')
@@ -18,6 +19,51 @@ const EmailHandler = require('../Email/EmailHandler')
const UrlHelper = require('../Helpers/UrlHelper')
const { promisify } = require('util')
async function _ensureAffiliations(userId, emailData) {
if (emailData.samlProviderId) {
await UserUpdater.promises.confirmEmail(userId, emailData.email)
} else {
await UserUpdater.promises.addAffiliationForNewUser(userId, emailData.email)
}
}
async function ensureAffiliations(userId) {
if (!Features.hasFeature('affiliations')) {
return
}
const user = await UserGetter.promises.getUser(userId)
if (!user) {
return new Errors.UserNotFoundError({ info: { userId } })
}
const flaggedEmails = user.emails.filter(email => email.affiliationUnchecked)
if (flaggedEmails.length === 0) {
return
}
if (flaggedEmails.length > 1) {
logger.error(
{ userId },
`Unexpected number of flagged emails: ${flaggedEmails.length}`
)
}
await _ensureAffiliations(userId, flaggedEmails[0])
}
async function ensureAffiliationsMiddleware(req, res, next) {
if (!Features.hasFeature('affiliations') || !req.query.ensureAffiliations) {
return next()
}
const userId = AuthenticationController.getLoggedInUserId(req)
try {
await ensureAffiliations(userId)
} catch (error) {
return next(error)
}
return next()
}
const UserController = {
tryDeleteUser(req, res, next) {
const userId = AuthenticationController.getLoggedInUserId(req)
@@ -399,7 +445,9 @@ const UserController = {
}
UserController.promises = {
doLogout: promisify(UserController.doLogout)
doLogout: promisify(UserController.doLogout),
ensureAffiliations,
ensureAffiliationsMiddleware
}
module.exports = UserController