Procházet zdrojové kódy

Merge pull request #3221 from overleaf/jel-institution-sso-email

Note which institution email was linked/unlinked in email alert

GitOrigin-RevId: 7e5ef743c8ec8b12c14f833fdd2b01676c95c1b6
Jessica Lawshe před 6 roky
rodič
revize
35897cf492

+ 13 - 7
services/web/app/src/Features/User/SAMLIdentityManager.js

@@ -128,14 +128,17 @@ async function _addInstitutionEmail(userId, email, providerId, auditLog) {
   }
 }
 
-async function _sendLinkedEmail(userId, providerName) {
+async function _sendLinkedEmail(userId, providerName, institutionEmail) {
   const user = await UserGetter.promises.getUser(userId, { email: 1 })
   const emailOptions = {
     to: user.email,
     actionDescribed: `an Institutional SSO account at ${providerName} was linked to your account ${
       user.email
     }`,
-    action: 'institutional SSO account linked'
+    action: 'institutional SSO account linked',
+    message: [
+      `<span style="display:inline-block;padding: 0 20px;width:100%;">Linked: <br/><b>${institutionEmail}</b></span>`
+    ]
   }
   EmailHandler.sendEmail('securityAlert', emailOptions, error => {
     if (error) {
@@ -144,11 +147,14 @@ async function _sendLinkedEmail(userId, providerName) {
   })
 }
 
-function _sendUnlinkedEmail(primaryEmail, providerName) {
+function _sendUnlinkedEmail(primaryEmail, providerName, institutionEmail) {
   const emailOptions = {
     to: primaryEmail,
-    actionDescribed: `an Institutional SSO account at ${providerName} is no longer linked to your account ${primaryEmail}`,
-    action: 'institutional SSO account no longer linked'
+    actionDescribed: `an Institutional SSO account at ${providerName} was unlinked from your account ${primaryEmail}`,
+    action: 'institutional SSO account no longer linked',
+    message: [
+      `<span style="display:inline-block;padding: 0 20px;width:100%;">No longer linked: <br/><b>${institutionEmail}</b></span>`
+    ]
   }
   EmailHandler.sendEmail('securityAlert', emailOptions, error => {
     if (error) {
@@ -208,7 +214,7 @@ async function linkAccounts(
     auditLog
   )
   await _addInstitutionEmail(userId, institutionEmail, providerId, auditLog)
-  await _sendLinkedEmail(userId, providerName)
+  await _sendLinkedEmail(userId, providerName, institutionEmail)
   // update v1 affiliations record
   if (hasEntitlement) {
     await InstitutionsAPI.promises.addEntitlement(userId, institutionEmail)
@@ -256,7 +262,7 @@ async function unlinkAccounts(
   // update v1 affiliations record
   await InstitutionsAPI.promises.removeEntitlement(userId, institutionEmail)
   // send email
-  _sendUnlinkedEmail(primaryEmail, providerName)
+  _sendUnlinkedEmail(primaryEmail, providerName, institutionEmail)
 }
 
 async function updateEntitlement(

+ 12 - 2
services/web/test/unit/src/User/SAMLIdentityManagerTests.js

@@ -5,6 +5,8 @@ const SandboxedModule = require('sandboxed-module')
 const modulePath = '../../../../app/src/Features/User/SAMLIdentityManager.js'
 
 describe('SAMLIdentityManager', function() {
+  const linkedEmail = 'another@example.com'
+
   beforeEach(function() {
     this.Errors = {
       EmailExistsError: sinon.stub(),
@@ -274,7 +276,13 @@ describe('SAMLIdentityManager', function() {
           },
           () => {
             expect(this.User.update).to.have.been.called
-            expect(this.EmailHandler.sendEmail).to.have.been.called
+            expect(this.EmailHandler.sendEmail).to.have.been.calledOnce
+            const emailArgs = this.EmailHandler.sendEmail.lastCall.args
+            expect(emailArgs[0]).to.equal('securityAlert')
+            expect(emailArgs[1].to).to.equal(this.user.email)
+            expect(emailArgs[1].actionDescribed).to.contain('was linked')
+            expect(emailArgs[1].message[0]).to.contain('Linked')
+            expect(emailArgs[1].message[0]).to.contain(this.user.email)
           }
         )
       })
@@ -282,7 +290,6 @@ describe('SAMLIdentityManager', function() {
   })
 
   describe('unlinkAccounts', function() {
-    const linkedEmail = 'another@example.com'
     it('should update the audit log', async function() {
       await this.SAMLIdentityManager.unlinkAccounts(
         this.user._id,
@@ -344,6 +351,9 @@ describe('SAMLIdentityManager', function() {
       const emailArgs = this.EmailHandler.sendEmail.lastCall.args
       expect(emailArgs[0]).to.equal('securityAlert')
       expect(emailArgs[1].to).to.equal(this.user.email)
+      expect(emailArgs[1].actionDescribed).to.contain('was unlinked')
+      expect(emailArgs[1].message[0]).to.contain('No longer linked')
+      expect(emailArgs[1].message[0]).to.contain(linkedEmail)
     })
 
     describe('errors', function() {