From 42b5f91b9f849b6089f9af648ef78cb2ebbb1ea9 Mon Sep 17 00:00:00 2001 From: Alexandre Bourdin Date: Fri, 16 Feb 2024 10:23:29 +0100 Subject: [PATCH] Merge pull request #17153 from overleaf/ab-send-group-sso-invite-self [web] Send the SSO linking invite when the group admin is adding self to the group GitOrigin-RevId: f87ce6cfb006a0e353394e4102881e6220e5e6d9 --- .../Subscription/TeamInvitesHandler.js | 24 +++++++++++- .../Subscription/TeamInvitesHandlerTests.js | 39 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/services/web/app/src/Features/Subscription/TeamInvitesHandler.js b/services/web/app/src/Features/Subscription/TeamInvitesHandler.js index ed8dc76f50..4540581ce4 100644 --- a/services/web/app/src/Features/Subscription/TeamInvitesHandler.js +++ b/services/web/app/src/Features/Subscription/TeamInvitesHandler.js @@ -80,7 +80,9 @@ async function acceptInvite(token, userId) { ) } if (subscription.ssoConfig) { - const ssoConfig = await SSOConfig.findById(subscription.ssoConfig) + const ssoConfig = await SSOConfig.findById( + subscription.ssoConfig._id || subscription.ssoConfig + ) if (ssoConfig?.enabled) { await Modules.promises.hooks.fire( 'scheduleGroupSSOReminder', @@ -153,6 +155,26 @@ async function _createInvite(subscription, email, inviter) { // legacy: remove any invite that might have been created in the past await _removeInviteFromTeam(subscription._id, email) + try { + if (subscription.ssoConfig) { + const ssoConfig = await SSOConfig.findById( + subscription.ssoConfig._id || subscription.ssoConfig + ) + if (ssoConfig?.enabled) { + await Modules.promises.hooks.fire( + 'sendGroupSSOReminder', + inviter._id, + subscription._id + ) + } + } + } catch (error) { + logger.error( + { err: error, userId: inviter._id, subscriptionId: subscription._id }, + 'Failed to schedule Group SSO invite for group admin' + ) + } + return { email: inviter.email, first_name: inviter.first_name, diff --git a/services/web/test/unit/src/Subscription/TeamInvitesHandlerTests.js b/services/web/test/unit/src/Subscription/TeamInvitesHandlerTests.js index dd83437c4b..cbd2a6fe0a 100644 --- a/services/web/test/unit/src/Subscription/TeamInvitesHandlerTests.js +++ b/services/web/test/unit/src/Subscription/TeamInvitesHandlerTests.js @@ -259,6 +259,45 @@ describe('TeamInvitesHandler', function () { ) }) + it('sends an SSO invite if SSO is enabled and inviting self', function (done) { + this.subscription.ssoConfig = new ObjectId('abc123abc123') + this.SSOConfig.findById + .withArgs(this.subscription.ssoConfig) + .resolves({ enabled: true }) + + this.TeamInvitesHandler.createInvite( + this.manager._id, + this.subscription, + this.manager.email, + (err, invite) => { + sinon.assert.calledWith( + this.Modules.promises.hooks.fire, + 'sendGroupSSOReminder', + this.manager._id, + this.subscription._id + ) + done(err) + } + ) + }) + + it('does not send an SSO invite if SSO is disabled and inviting self', function (done) { + this.subscription.ssoConfig = new ObjectId('abc123abc123') + this.SSOConfig.findById + .withArgs(this.subscription.ssoConfig) + .resolves({ enabled: false }) + + this.TeamInvitesHandler.createInvite( + this.manager._id, + this.subscription, + this.manager.email, + (err, invite) => { + sinon.assert.notCalled(this.Modules.promises.hooks.fire) + done(err) + } + ) + }) + it('sends a notification if inviting registered user', function (done) { const id = new ObjectId('6a6b3a8014829a865bbf700d') const managedUsersEnabled = false