Skip to content

Commit

Permalink
Merge pull request #1323 from IFRCGo/fix/use-permissions
Browse files Browse the repository at this point in the history
Check guest user permissions in usePermissions hook
  • Loading branch information
samshara authored Sep 12, 2024
2 parents b5a1843 + 30b65f6 commit a2444df
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
11 changes: 5 additions & 6 deletions app/src/App/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ const allFlashUpdates = customWrapRoute({
context: {
title: 'All Flash Updates',
visibility: 'is-authenticated',
permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin,
permissions: ({ isIfrcAdmin }) => isIfrcAdmin,
},
});

Expand All @@ -838,7 +838,7 @@ const flashUpdateFormNew = customWrapRoute({
context: {
title: 'New Flash Update',
visibility: 'is-authenticated',
permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin,
permissions: ({ isIfrcAdmin }) => isIfrcAdmin,
},
});

Expand All @@ -853,7 +853,7 @@ const flashUpdateFormEdit = customWrapRoute({
context: {
title: 'Edit Flash Update',
visibility: 'is-authenticated',
permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin,
permissions: ({ isIfrcAdmin }) => isIfrcAdmin,
},
});

Expand All @@ -869,7 +869,7 @@ const flashUpdateFormDetails = customWrapRoute({
context: {
title: 'Flash Update Details',
visibility: 'anything',
permissions: ({ isIfrcAdmin, isGuestUser }) => !isGuestUser && isIfrcAdmin,
permissions: ({ isIfrcAdmin }) => isIfrcAdmin,
},
});

Expand Down Expand Up @@ -1083,8 +1083,7 @@ const newPerOverviewForm = customWrapRoute({
permissions: ({
isSuperUser,
isPerAdmin,
isGuestUser,
}) => !isGuestUser && (isSuperUser || isPerAdmin),
}) => isSuperUser || isPerAdmin,
},
});

Expand Down
36 changes: 26 additions & 10 deletions app/src/hooks/domain/usePermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions app/src/views/CountryNsOverviewStrategicPriorities/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 3 additions & 5 deletions app/src/views/CountryPreparedness/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<PrivateCountryPreparedness />
Expand Down

0 comments on commit a2444df

Please sign in to comment.