Răsfoiți Sursa

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
Alexandre Bourdin 2 ani în urmă
părinte
comite
42b5f91b9f

+ 23 - 1
services/web/app/src/Features/Subscription/TeamInvitesHandler.js

@@ -80,7 +80,9 @@ async function acceptInvite(token, userId) {
     )
     )
   }
   }
   if (subscription.ssoConfig) {
   if (subscription.ssoConfig) {
-    const ssoConfig = await SSOConfig.findById(subscription.ssoConfig)
+    const ssoConfig = await SSOConfig.findById(
+      subscription.ssoConfig._id || subscription.ssoConfig
+    )
     if (ssoConfig?.enabled) {
     if (ssoConfig?.enabled) {
       await Modules.promises.hooks.fire(
       await Modules.promises.hooks.fire(
         'scheduleGroupSSOReminder',
         'scheduleGroupSSOReminder',
@@ -153,6 +155,26 @@ async function _createInvite(subscription, email, inviter) {
     // legacy: remove any invite that might have been created in the past
     // legacy: remove any invite that might have been created in the past
     await _removeInviteFromTeam(subscription._id, email)
     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 {
     return {
       email: inviter.email,
       email: inviter.email,
       first_name: inviter.first_name,
       first_name: inviter.first_name,

+ 39 - 0
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) {
     it('sends a notification if inviting registered user', function (done) {
       const id = new ObjectId('6a6b3a8014829a865bbf700d')
       const id = new ObjectId('6a6b3a8014829a865bbf700d')
       const managedUsersEnabled = false
       const managedUsersEnabled = false