From b3bef85b87f493bb218390ff86a8fdb4ef3e902a Mon Sep 17 00:00:00 2001 From: nivedin Date: Wed, 13 Dec 2023 14:00:19 +0530 Subject: [PATCH] fix: inherit auth-headers closest to path bug --- .../src/helpers/collection/collection.ts | 67 +++++++++++++++++-- .../helpers/teams/TeamCollectionAdapter.ts | 6 +- .../src/newstore/collections.ts | 5 +- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/packages/hoppscotch-common/src/helpers/collection/collection.ts b/packages/hoppscotch-common/src/helpers/collection/collection.ts index 08804da6d..65ea18ff6 100644 --- a/packages/hoppscotch-common/src/helpers/collection/collection.ts +++ b/packages/hoppscotch-common/src/helpers/collection/collection.ts @@ -110,6 +110,46 @@ export function updateSaveContextForAffectedRequests( } } +/** + * Used to check the new folder path is close to the save context folder path or not + * @param folderPathCurrent The path saved as the inherited path in the inherited properties + * @param newFolderPath The incomming path + * @param saveContextPath The save context of the request + * @returns The path which is close to saveContext.folderPath + */ +function folderPathCloseToSaveContext( + folderPathCurrent: string | undefined, + newFolderPath: string, + saveContextPath: string +) { + if (!folderPathCurrent) return newFolderPath + const folderPathCurrentArray = folderPathCurrent.split("/") + const newFolderPathArray = newFolderPath.split("/") + + const saveContextFolderPathArray = saveContextPath.split("/") + + let folderPathCurrentMatch = 0 + + for (let i = 0; i < folderPathCurrentArray.length; i++) { + if (folderPathCurrentArray[i] === saveContextFolderPathArray[i]) { + folderPathCurrentMatch++ + } + } + + let newFolderPathMatch = 0 + + for (let i = 0; i < newFolderPathArray.length; i++) { + if (newFolderPathArray[i] === saveContextFolderPathArray[i]) { + newFolderPathMatch++ + } + } + + if (folderPathCurrentMatch > newFolderPathMatch) { + return folderPathCurrent + } + return newFolderPath +} + export function updateInheritedPropertiesForAffectedRequests( path: string, inheritedProperties: HoppInheritedProperty, @@ -137,9 +177,28 @@ export function updateInheritedPropertiesForAffectedRequests( } const tabsEffectedByAuth = tabs.filter((tab) => { + if (workspace === "personal") { + return ( + tab.value.document.saveContext?.originLocation === "user-collection" && + tab.value.document.saveContext.folderPath.startsWith(path) && + path === + folderPathCloseToSaveContext( + tab.value.document.inheritedProperties?.auth.parentID, + path, + tab.value.document.saveContext.folderPath + ) + ) + } + return ( - tab.value.document.inheritedProperties && - tab.value.document.inheritedProperties.auth.parentID === path + tab.value.document.saveContext?.originLocation === "team-collection" && + tab.value.document.saveContext.collectionID?.startsWith(path) && + path === + folderPathCloseToSaveContext( + tab.value.document.inheritedProperties?.auth.parentID, + path, + tab.value.document.saveContext.collectionID + ) ) }) @@ -163,9 +222,7 @@ export function updateInheritedPropertiesForAffectedRequests( return { ...header, inheritedHeader: inheritedProperties.headers.find( - (inheritedHeader) => - inheritedHeader.inheritedHeader?.key === - header.inheritedHeader?.key + (inheritedHeader) => inheritedHeader.parentID === path )?.inheritedHeader, } } diff --git a/packages/hoppscotch-common/src/helpers/teams/TeamCollectionAdapter.ts b/packages/hoppscotch-common/src/helpers/teams/TeamCollectionAdapter.ts index ad2042721..b6a1e0863 100644 --- a/packages/hoppscotch-common/src/helpers/teams/TeamCollectionAdapter.ts +++ b/packages/hoppscotch-common/src/helpers/teams/TeamCollectionAdapter.ts @@ -1080,7 +1080,7 @@ export default class NewTeamCollectionAdapter { authType: "inherit", authActive: true, } - auth.parentID = folderPath ?? parentFolder.id + auth.parentID = [...path.slice(0, i + 1)].join("/") auth.parentName = parentFolder.title } @@ -1091,7 +1091,7 @@ export default class NewTeamCollectionAdapter { if (parentFolderAuth?.authType === "inherit" && path.length === 1) { auth = { - parentID: folderPath ?? parentFolder.id, + parentID: [...path.slice(0, i + 1)].join("/"), parentName: parentFolder.title, inheritedAuth: auth.inheritedAuth, } @@ -1099,7 +1099,7 @@ export default class NewTeamCollectionAdapter { if (parentFolderAuth?.authType !== "inherit") { auth = { - parentID: folderPath ?? parentFolder.id, + parentID: [...path.slice(0, i + 1)].join("/"), parentName: parentFolder.title, inheritedAuth: parentFolderAuth, } diff --git a/packages/hoppscotch-common/src/newstore/collections.ts b/packages/hoppscotch-common/src/newstore/collections.ts index 11efea540..7979855d0 100644 --- a/packages/hoppscotch-common/src/newstore/collections.ts +++ b/packages/hoppscotch-common/src/newstore/collections.ts @@ -105,11 +105,10 @@ export function cascadeParentCollectionForHeaderAuth( const parentFolderAuth = parentFolder.auth const parentFolderHeaders = parentFolder.headers - // check if the parent folder has authType 'inherit' and if it is the root folder if (parentFolderAuth?.authType === "inherit" && path.length === 1) { auth = { - parentID: folderPath, + parentID: [...path.slice(0, i + 1)].join("/"), parentName: parentFolder.name, inheritedAuth: auth.inheritedAuth, } @@ -117,7 +116,7 @@ export function cascadeParentCollectionForHeaderAuth( if (parentFolderAuth?.authType !== "inherit") { auth = { - parentID: folderPath, + parentID: [...path.slice(0, i + 1)].join("/"), parentName: parentFolder.name, inheritedAuth: parentFolderAuth, }