From 1122a83b60077c93888f5ddb7bd05a02196664d4 Mon Sep 17 00:00:00 2001 From: Jessica Lawshe Date: Wed, 12 Jan 2022 10:19:23 -0600 Subject: [PATCH] Merge pull request #6254 from overleaf/jel-saml-entitlement [web] Always update entitlement in v1 after SAML callback GitOrigin-RevId: 2569d6d8e6142786ad2875c62c9cd4568837654a --- .../src/Features/User/SAMLIdentityManager.js | 6 --- .../test/acceptance/src/mocks/MockV1Api.js | 45 ++++++++++++------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/services/web/app/src/Features/User/SAMLIdentityManager.js b/services/web/app/src/Features/User/SAMLIdentityManager.js index 8659bf84a1..543e2b9748 100644 --- a/services/web/app/src/Features/User/SAMLIdentityManager.js +++ b/services/web/app/src/Features/User/SAMLIdentityManager.js @@ -339,12 +339,6 @@ async function updateEntitlement( } // update v2 user await User.updateOne(query, update).exec() - // update v1 affiliations record - if (hasEntitlement) { - await InstitutionsAPI.promises.addEntitlement(userId, institutionEmail) - } else { - await InstitutionsAPI.promises.removeEntitlement(userId, institutionEmail) - } } function entitlementAttributeMatches(entitlementAttribute, entitlementMatcher) { diff --git a/services/web/test/acceptance/src/mocks/MockV1Api.js b/services/web/test/acceptance/src/mocks/MockV1Api.js index c88f3ebdfb..e970e64b08 100644 --- a/services/web/test/acceptance/src/mocks/MockV1Api.js +++ b/services/web/test/acceptance/src/mocks/MockV1Api.js @@ -91,7 +91,8 @@ class MockV1Api extends AbstractMockApi { ) } - addAffiliation(userId, email) { + addAffiliation(userId, email, entitlement) { + let newAffiliation = true const institution = {} if (!email) return if (!this.affiliations[userId]) this.affiliations[userId] = [] @@ -101,26 +102,36 @@ class MockV1Api extends AbstractMockApi { return affiliationData.email === email }) ) - return + newAffiliation = false - const domain = email.split('@').pop() + if (newAffiliation) { + const domain = email.split('@').pop() - if (this.blocklistedDomains.indexOf(domain.replace('.com', '')) !== -1) { - return - } + if (this.blocklistedDomains.indexOf(domain.replace('.com', '')) !== -1) { + return + } - if (this.allInstitutionDomains.has(domain)) { - for (const [institutionId, domainData] of Object.entries( - this.institutionDomains - )) { - if (domainData[domain]) { - institution.id = institutionId + if (this.allInstitutionDomains.has(domain)) { + for (const [institutionId, domainData] of Object.entries( + this.institutionDomains + )) { + if (domainData[domain]) { + institution.id = institutionId + } } } + + if (institution.id) { + this.affiliations[userId].push({ email, institution }) + } } - if (institution.id) { - this.affiliations[userId].push({ email, institution }) + if (entitlement !== undefined) { + this.affiliations[userId].forEach(affiliation => { + if (affiliation.email === email) { + affiliation.cached_entitlement = entitlement + } + }) } } @@ -231,7 +242,11 @@ class MockV1Api extends AbstractMockApi { }) this.app.post('/api/v2/users/:userId/affiliations', (req, res) => { - this.addAffiliation(req.params.userId, req.body.email) + this.addAffiliation( + req.params.userId, + req.body.email, + req.body.entitlement + ) res.sendStatus(201) })