Skip to content

Commit

Permalink
fix: check guest user permissions in usePermissions hook
Browse files Browse the repository at this point in the history
  • Loading branch information
samshara committed Sep 12, 2024
1 parent b5a1843 commit e2cb0d3
Showing 1 changed file with 26 additions and 10 deletions.
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

0 comments on commit e2cb0d3

Please sign in to comment.