From ae7dd9ef082036dd5056c87c7d73524bca473263 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 5 Dec 2024 00:42:54 +0300 Subject: [PATCH 1/2] added role check for the current user in getActivePolicies --- src/libs/PolicyUtils.ts | 9 +++++---- src/pages/workspace/WorkspaceNewRoomPage.tsx | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index f6b277d69d6b..641969ce1a6e 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -71,9 +71,10 @@ Onyx.connect({ * Filter out the active policies, which will exclude policies with pending deletion * These are policies that we can use to create reports with in NewDot. */ -function getActivePolicies(policies: OnyxCollection | null): Policy[] { +function getActivePolicies(policies: OnyxCollection | null, currentUserLogin: string | undefined): Policy[] { return Object.values(policies ?? {}).filter( - (policy): policy is Policy => !!policy && policy.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !!policy.name && !!policy.id, + (policy): policy is Policy => + !!policy && policy.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !!policy.name && !!policy.id && !!getPolicyRole(policy, currentUserLogin), ); } /** @@ -636,7 +637,7 @@ function getPolicy(policyID: string | undefined): OnyxEntry { /** Return active policies where current user is an admin */ function getActiveAdminWorkspaces(policies: OnyxCollection | null, currentUserLogin: string | undefined): Policy[] { - const activePolicies = getActivePolicies(policies); + const activePolicies = getActivePolicies(policies, currentUserLogin); return activePolicies.filter((policy) => shouldShowPolicy(policy, NetworkStore.isOffline(), currentUserLogin) && isPolicyAdmin(policy, currentUserLogin)); } @@ -652,7 +653,7 @@ function canSendInvoice(policies: OnyxCollection | null, currentUserLogi } function hasWorkspaceWithInvoices(currentUserLogin: string | undefined): boolean { - const activePolicies = getActivePolicies(allPolicies); + const activePolicies = getActivePolicies(allPolicies, currentUserLogin); return activePolicies.some((policy) => shouldShowPolicy(policy, NetworkStore.isOffline(), currentUserLogin) && policy.areInvoicesEnabled); } diff --git a/src/pages/workspace/WorkspaceNewRoomPage.tsx b/src/pages/workspace/WorkspaceNewRoomPage.tsx index 37686ba1c3a7..c64c53306a1f 100644 --- a/src/pages/workspace/WorkspaceNewRoomPage.tsx +++ b/src/pages/workspace/WorkspaceNewRoomPage.tsx @@ -64,14 +64,14 @@ function WorkspaceNewRoomPage() { const workspaceOptions = useMemo( () => - PolicyUtils.getActivePolicies(policies) + PolicyUtils.getActivePolicies(policies, session?.email) ?.filter((policy) => policy.type !== CONST.POLICY.TYPE.PERSONAL) .map((policy) => ({ label: policy.name, value: policy.id, })) .sort((a, b) => localeCompare(a.label, b.label)) ?? [], - [policies], + [policies, session?.email], ); const [policyID, setPolicyID] = useState(() => { if (!!activeWorkspaceOrDefaultID && workspaceOptions.some((option) => option.value === activeWorkspaceOrDefaultID)) { From 50392ae992232701ec93d8f01440c97a8cce0e75 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Mon, 9 Dec 2024 16:44:40 +0300 Subject: [PATCH 2/2] added comments and tests --- src/libs/PolicyUtils.ts | 1 + tests/unit/PolicyUtilsTest.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 641969ce1a6e..8faa0db3b7fc 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -69,6 +69,7 @@ Onyx.connect({ /** * Filter out the active policies, which will exclude policies with pending deletion + * and policies the current user doesn't belong to. * These are policies that we can use to create reports with in NewDot. */ function getActivePolicies(policies: OnyxCollection | null, currentUserLogin: string | undefined): Policy[] { diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 71830443063a..c37d7970f1cf 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -1,10 +1,24 @@ import * as PolicyUtils from '@libs/PolicyUtils'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy} from '@src/types/onyx'; +import createCollection from '../utils/collections/createCollection'; +import createRandomPolicy from '../utils/collections/policies'; function toLocaleDigitMock(dot: string): string { return dot; } describe('PolicyUtils', () => { + describe('getActivePolicies', () => { + it("getActivePolicies should filter out policies that the current user doesn't belong to", () => { + const policies = createCollection( + (item) => `${ONYXKEYS.COLLECTION.POLICY}${item.id}`, + (index) => ({...createRandomPolicy(index + 1), ...(!index && {role: null})} as Policy), + 2, + ); + expect(PolicyUtils.getActivePolicies(policies, undefined)).toHaveLength(1); + }); + }); describe('getRateDisplayValue', () => { it('should return an empty string for NaN', () => { const rate = PolicyUtils.getRateDisplayValue('invalid' as unknown as number, toLocaleDigitMock);