Skip to content

Commit a2444df

Browse files
authored
Merge pull request #1323 from IFRCGo/fix/use-permissions
Check guest user permissions in usePermissions hook
2 parents b5a1843 + 30b65f6 commit a2444df

File tree

6 files changed

+39
-28
lines changed

6 files changed

+39
-28
lines changed

app/src/App/routes/index.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ const allFlashUpdates = customWrapRoute({
823823
context: {
824824
title: 'All Flash Updates',
825825
visibility: 'is-authenticated',
826-
permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin,
826+
permissions: ({ isIfrcAdmin }) => isIfrcAdmin,
827827
},
828828
});
829829

@@ -838,7 +838,7 @@ const flashUpdateFormNew = customWrapRoute({
838838
context: {
839839
title: 'New Flash Update',
840840
visibility: 'is-authenticated',
841-
permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin,
841+
permissions: ({ isIfrcAdmin }) => isIfrcAdmin,
842842
},
843843
});
844844

@@ -853,7 +853,7 @@ const flashUpdateFormEdit = customWrapRoute({
853853
context: {
854854
title: 'Edit Flash Update',
855855
visibility: 'is-authenticated',
856-
permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin,
856+
permissions: ({ isIfrcAdmin }) => isIfrcAdmin,
857857
},
858858
});
859859

@@ -869,7 +869,7 @@ const flashUpdateFormDetails = customWrapRoute({
869869
context: {
870870
title: 'Flash Update Details',
871871
visibility: 'anything',
872-
permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin,
872+
permissions: ({ isIfrcAdmin }) => isIfrcAdmin,
873873
},
874874
});
875875

@@ -1083,8 +1083,7 @@ const newPerOverviewForm = customWrapRoute({
10831083
permissions: ({
10841084
isSuperUser,
10851085
isPerAdmin,
1086-
isGuestUser,
1087-
}) => !isGuestUser && (isSuperUser || isPerAdmin),
1086+
}) => isSuperUser || isPerAdmin,
10881087
},
10891088
});
10901089

app/src/hooks/domain/usePermissions.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,42 @@ function usePermissions() {
88

99
const perms = useMemo(
1010
() => {
11+
const isGuestUser = !!userMe?.limit_access_to_guest;
12+
1113
const isDrefRegionalCoordinator = (regionId: number | undefined) => (
12-
isDefined(regionId) && !!userMe?.is_dref_coordinator_for_regions?.includes(regionId)
14+
!isGuestUser
15+
&& isDefined(regionId)
16+
&& !!userMe?.is_dref_coordinator_for_regions?.includes(regionId)
1317
);
1418
const isCountryAdmin = (countryId: number | undefined) => (
15-
isDefined(countryId) && !!userMe?.is_admin_for_countries?.includes(countryId)
19+
!isGuestUser
20+
&& isDefined(countryId)
21+
&& !!userMe?.is_admin_for_countries?.includes(countryId)
1622
);
1723
const isRegionAdmin = (regionId: number | undefined) => (
18-
isDefined(regionId) && !!userMe?.is_admin_for_regions?.includes(regionId)
24+
!isGuestUser
25+
&& isDefined(regionId)
26+
&& !!userMe?.is_admin_for_regions?.includes(regionId)
1927
);
2028
const isRegionPerAdmin = (regionId: number | undefined) => (
21-
isDefined(regionId) && !!userMe?.is_per_admin_for_regions.includes(regionId)
29+
!isGuestUser
30+
&& isDefined(regionId)
31+
&& !!userMe?.is_per_admin_for_regions.includes(regionId)
2232
);
2333
const isCountryPerAdmin = (countryId: number | undefined) => (
24-
isDefined(countryId) && !!userMe?.is_per_admin_for_countries.includes(countryId)
34+
!isGuestUser
35+
&& isDefined(countryId)
36+
&& !!userMe?.is_per_admin_for_countries.includes(countryId)
2537
);
2638

27-
const isPerAdmin = (userMe?.is_per_admin_for_countries.length ?? 0) > 0
28-
|| (userMe?.is_admin_for_regions.length ?? 0) > 0;
39+
const isPerAdmin = !isGuestUser
40+
&& ((userMe?.is_per_admin_for_countries.length ?? 0) > 0
41+
|| (userMe?.is_admin_for_regions.length ?? 0) > 0);
42+
43+
const isIfrcAdmin = !isGuestUser
44+
&& (!!userMe?.is_ifrc_admin || !!userMe?.email?.toLowerCase().endsWith('@ifrc.org'));
2945

30-
const isGuestUser = (userMe?.limit_access_to_guest);
46+
const isSuperUser = !isGuestUser && !!userMe?.is_superuser;
3147

3248
return {
3349
isDrefRegionalCoordinator,
@@ -36,8 +52,8 @@ function usePermissions() {
3652
isRegionPerAdmin,
3753
isCountryPerAdmin,
3854
isPerAdmin,
39-
isIfrcAdmin: !!userMe?.is_ifrc_admin || !!userMe?.email?.toLowerCase().endsWith('@ifrc.org'),
40-
isSuperUser: !!userMe?.is_superuser,
55+
isIfrcAdmin,
56+
isSuperUser,
4157
isGuestUser,
4258
};
4359
},

app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitsTable/LocalUnitTableActions/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ function LocalUnitsTableActions(props: Props) {
3737
onActionSuccess,
3838
} = props;
3939

40-
const { isCountryAdmin, isSuperUser, isGuestUser } = usePermissions();
40+
const { isCountryAdmin, isSuperUser } = usePermissions();
4141
const strings = useTranslation(i18n);
4242

43-
const hasValidatePermission = !isGuestUser && (isSuperUser || isCountryAdmin(countryId));
43+
const hasValidatePermission = isSuperUser || isCountryAdmin(countryId);
4444

4545
const [readOnlyLocalUnitModal, setReadOnlyLocalUnitModal] = useState(false);
4646

app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ function NationalSocietyLocalUnits(props: Props) {
111111

112112
const strings = useTranslation(i18n);
113113

114-
const hasAddLocalUnitPermission = !isGuestUser
115-
&& (isCountryAdmin(countryResponse?.id) || isSuperUser);
114+
const hasAddLocalUnitPermission = isCountryAdmin(countryResponse?.id) || isSuperUser;
116115

117116
useEffect(() => {
118117
document.addEventListener('fullscreenchange', handleFullScreenChange);

app/src/views/CountryNsOverviewStrategicPriorities/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ export function Component() {
4040
isCountryPerAdmin,
4141
isSuperUser,
4242
isRegionPerAdmin,
43-
isGuestUser,
4443
} = usePermissions();
4544

4645
const countryDetails = useCountry({ id: Number(countryId) });
4746
const regionId = isDefined(countryDetails) ? Number(countryDetails?.region) : undefined;
4847

4948
const isPerAdmin = isSuperUser
50-
|| (!isGuestUser && isCountryPerAdmin(Number(countryId)))
51-
|| (!isGuestUser && isRegionPerAdmin(regionId));
49+
|| isCountryPerAdmin(Number(countryId))
50+
|| isRegionPerAdmin(regionId);
5251

5352
const {
5453
pending: publicPerStatsPending,

app/src/views/CountryPreparedness/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ export function Component() {
1313
isCountryPerAdmin,
1414
isSuperUser,
1515
isRegionPerAdmin,
16-
isGuestUser,
1716
} = usePermissions();
1817

1918
const countryDetails = useCountry({ id: Number(countryId) });
2019

21-
const hasPermission = !isGuestUser && (
22-
isSuperUser
20+
const hasPermission = isSuperUser
2321
|| isCountryPerAdmin(Number(countryId))
24-
|| isRegionPerAdmin(Number(countryDetails?.region))
25-
);
22+
|| isRegionPerAdmin(Number(countryDetails?.region));
23+
2624
if (hasPermission) {
2725
return (
2826
<PrivateCountryPreparedness />

0 commit comments

Comments
 (0)