From e2cb0d3f5ffce856b4a287383a50ac638999e462 Mon Sep 17 00:00:00 2001 From: samshara Date: Wed, 21 Aug 2024 14:47:44 +0545 Subject: [PATCH 1/2] fix: check guest user permissions in usePermissions hook --- app/src/hooks/domain/usePermissions.ts | 36 +++++++++++++++++++------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/app/src/hooks/domain/usePermissions.ts b/app/src/hooks/domain/usePermissions.ts index f7b1e8c13d..eb49908fcc 100644 --- a/app/src/hooks/domain/usePermissions.ts +++ b/app/src/hooks/domain/usePermissions.ts @@ -8,26 +8,42 @@ function usePermissions() { const perms = useMemo( () => { + const isGuestUser = !!userMe?.limit_access_to_guest; + const isDrefRegionalCoordinator = (regionId: number | undefined) => ( - isDefined(regionId) && !!userMe?.is_dref_coordinator_for_regions?.includes(regionId) + !isGuestUser + && isDefined(regionId) + && !!userMe?.is_dref_coordinator_for_regions?.includes(regionId) ); const isCountryAdmin = (countryId: number | undefined) => ( - isDefined(countryId) && !!userMe?.is_admin_for_countries?.includes(countryId) + !isGuestUser + && isDefined(countryId) + && !!userMe?.is_admin_for_countries?.includes(countryId) ); const isRegionAdmin = (regionId: number | undefined) => ( - isDefined(regionId) && !!userMe?.is_admin_for_regions?.includes(regionId) + !isGuestUser + && isDefined(regionId) + && !!userMe?.is_admin_for_regions?.includes(regionId) ); const isRegionPerAdmin = (regionId: number | undefined) => ( - isDefined(regionId) && !!userMe?.is_per_admin_for_regions.includes(regionId) + !isGuestUser + && isDefined(regionId) + && !!userMe?.is_per_admin_for_regions.includes(regionId) ); const isCountryPerAdmin = (countryId: number | undefined) => ( - isDefined(countryId) && !!userMe?.is_per_admin_for_countries.includes(countryId) + !isGuestUser + && isDefined(countryId) + && !!userMe?.is_per_admin_for_countries.includes(countryId) ); - const isPerAdmin = (userMe?.is_per_admin_for_countries.length ?? 0) > 0 - || (userMe?.is_admin_for_regions.length ?? 0) > 0; + const isPerAdmin = !isGuestUser + && ((userMe?.is_per_admin_for_countries.length ?? 0) > 0 + || (userMe?.is_admin_for_regions.length ?? 0) > 0); + + const isIfrcAdmin = !isGuestUser + && (!!userMe?.is_ifrc_admin || !!userMe?.email?.toLowerCase().endsWith('@ifrc.org')); - const isGuestUser = (userMe?.limit_access_to_guest); + const isSuperUser = !isGuestUser && !!userMe?.is_superuser; return { isDrefRegionalCoordinator, @@ -36,8 +52,8 @@ function usePermissions() { isRegionPerAdmin, isCountryPerAdmin, isPerAdmin, - isIfrcAdmin: !!userMe?.is_ifrc_admin || !!userMe?.email?.toLowerCase().endsWith('@ifrc.org'), - isSuperUser: !!userMe?.is_superuser, + isIfrcAdmin, + isSuperUser, isGuestUser, }; }, From 30b65f66689eccd4ccd9232271fe631390a7c438 Mon Sep 17 00:00:00 2001 From: samshara Date: Mon, 9 Sep 2024 14:04:26 +0545 Subject: [PATCH 2/2] fix: remove unnecessary check for guest user permission --- app/src/App/routes/index.tsx | 11 +++++------ .../LocalUnitsTable/LocalUnitTableActions/index.tsx | 4 ++-- .../NationalSocietyLocalUnits/index.tsx | 3 +-- .../CountryNsOverviewStrategicPriorities/index.tsx | 5 ++--- app/src/views/CountryPreparedness/index.tsx | 8 +++----- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/src/App/routes/index.tsx b/app/src/App/routes/index.tsx index 9c7a8ccc01..9540ea91b7 100644 --- a/app/src/App/routes/index.tsx +++ b/app/src/App/routes/index.tsx @@ -823,7 +823,7 @@ const allFlashUpdates = customWrapRoute({ context: { title: 'All Flash Updates', visibility: 'is-authenticated', - permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin, + permissions: ({ isIfrcAdmin }) => isIfrcAdmin, }, }); @@ -838,7 +838,7 @@ const flashUpdateFormNew = customWrapRoute({ context: { title: 'New Flash Update', visibility: 'is-authenticated', - permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin, + permissions: ({ isIfrcAdmin }) => isIfrcAdmin, }, }); @@ -853,7 +853,7 @@ const flashUpdateFormEdit = customWrapRoute({ context: { title: 'Edit Flash Update', visibility: 'is-authenticated', - permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin, + permissions: ({ isIfrcAdmin }) => isIfrcAdmin, }, }); @@ -869,7 +869,7 @@ const flashUpdateFormDetails = customWrapRoute({ context: { title: 'Flash Update Details', visibility: 'anything', - permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin, + permissions: ({ isIfrcAdmin }) => isIfrcAdmin, }, }); @@ -1083,8 +1083,7 @@ const newPerOverviewForm = customWrapRoute({ permissions: ({ isSuperUser, isPerAdmin, - isGuestUser, - }) => !isGuestUser && (isSuperUser || isPerAdmin), + }) => isSuperUser || isPerAdmin, }, }); diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx index 5782af847c..5f4ece4fe0 100644 --- a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx +++ b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx @@ -37,10 +37,10 @@ function LocalUnitsTableActions(props: Props) { onActionSuccess, } = props; - const { isCountryAdmin, isSuperUser, isGuestUser } = usePermissions(); + const { isCountryAdmin, isSuperUser } = usePermissions(); const strings = useTranslation(i18n); - const hasValidatePermission = !isGuestUser && (isSuperUser || isCountryAdmin(countryId)); + const hasValidatePermission = isSuperUser || isCountryAdmin(countryId); const [readOnlyLocalUnitModal, setReadOnlyLocalUnitModal] = useState(false); diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/index.tsx b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/index.tsx index 7c5708c8c2..bdcc77c460 100644 --- a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/index.tsx +++ b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/index.tsx @@ -111,8 +111,7 @@ function NationalSocietyLocalUnits(props: Props) { const strings = useTranslation(i18n); - const hasAddLocalUnitPermission = !isGuestUser - && (isCountryAdmin(countryResponse?.id) || isSuperUser); + const hasAddLocalUnitPermission = isCountryAdmin(countryResponse?.id) || isSuperUser; useEffect(() => { document.addEventListener('fullscreenchange', handleFullScreenChange); diff --git a/app/src/views/CountryNsOverviewStrategicPriorities/index.tsx b/app/src/views/CountryNsOverviewStrategicPriorities/index.tsx index 089a2e80ff..3ce5575b9a 100644 --- a/app/src/views/CountryNsOverviewStrategicPriorities/index.tsx +++ b/app/src/views/CountryNsOverviewStrategicPriorities/index.tsx @@ -40,15 +40,14 @@ export function Component() { isCountryPerAdmin, isSuperUser, isRegionPerAdmin, - isGuestUser, } = usePermissions(); const countryDetails = useCountry({ id: Number(countryId) }); const regionId = isDefined(countryDetails) ? Number(countryDetails?.region) : undefined; const isPerAdmin = isSuperUser - || (!isGuestUser && isCountryPerAdmin(Number(countryId))) - || (!isGuestUser && isRegionPerAdmin(regionId)); + || isCountryPerAdmin(Number(countryId)) + || isRegionPerAdmin(regionId); const { pending: publicPerStatsPending, diff --git a/app/src/views/CountryPreparedness/index.tsx b/app/src/views/CountryPreparedness/index.tsx index 69985d84bf..91ef05f718 100644 --- a/app/src/views/CountryPreparedness/index.tsx +++ b/app/src/views/CountryPreparedness/index.tsx @@ -13,16 +13,14 @@ export function Component() { isCountryPerAdmin, isSuperUser, isRegionPerAdmin, - isGuestUser, } = usePermissions(); const countryDetails = useCountry({ id: Number(countryId) }); - const hasPermission = !isGuestUser && ( - isSuperUser + const hasPermission = isSuperUser || isCountryPerAdmin(Number(countryId)) - || isRegionPerAdmin(Number(countryDetails?.region)) - ); + || isRegionPerAdmin(Number(countryDetails?.region)); + if (hasPermission) { return (