-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Permission Context and Update User Permissions Handling
- Introduced a new PermissionContext to manage user permissions and super admin status across the application. - Updated AppRouter to utilize PermissionProvider for managing permissions. - Changed UserModel to store permissions as strings instead of objects. - Refactored OrganizationFacilities and OrganizationPatients components to use updated permission checks and organization identifiers. - Enhanced OrganizationLayout to conditionally render navigation items based on user permissions. This commit improves the permission management system and ensures consistent handling of user permissions throughout the application.
- Loading branch information
Showing
7 changed files
with
168 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { createContext, useContext, useMemo } from "react"; | ||
|
||
interface PermissionContextType { | ||
/** | ||
* Check if user has a specific permission in either their user permissions or object permissions | ||
* @param permission - The permission to check | ||
* @param objectPermissions - Optional object-specific permissions | ||
*/ | ||
hasPermission: (permission: string, objectPermissions?: string[]) => boolean; | ||
/** | ||
* Raw permissions array from the user | ||
*/ | ||
userPermissions: string[]; | ||
/** | ||
* Whether the user is a super admin | ||
*/ | ||
isSuperAdmin: boolean; | ||
} | ||
|
||
const PermissionContext = createContext<PermissionContextType | null>(null); | ||
|
||
interface PermissionProviderProps { | ||
children: React.ReactNode; | ||
userPermissions: string[]; | ||
isSuperAdmin?: boolean; | ||
} | ||
|
||
export function PermissionProvider({ | ||
children, | ||
userPermissions, | ||
isSuperAdmin = false, | ||
}: PermissionProviderProps) { | ||
const value = useMemo(() => { | ||
const hasPermission = ( | ||
permission: string, | ||
objectPermissions?: string[], | ||
) => { | ||
if (isSuperAdmin) return true; | ||
return ( | ||
userPermissions.includes(permission) || | ||
(objectPermissions?.includes(permission) ?? false) | ||
); | ||
}; | ||
|
||
return { | ||
hasPermission, | ||
userPermissions, | ||
isSuperAdmin, | ||
}; | ||
}, [userPermissions, isSuperAdmin]); | ||
|
||
return ( | ||
<PermissionContext.Provider value={value}> | ||
{children} | ||
</PermissionContext.Provider> | ||
); | ||
} | ||
|
||
export function usePermissions() { | ||
const context = useContext(PermissionContext); | ||
if (!context) { | ||
throw new Error("usePermissions must be used within a PermissionProvider"); | ||
} | ||
return context; | ||
} | ||
|
||
export function useHasPermission( | ||
permission: string, | ||
objectPermissions?: string[], | ||
) { | ||
const { hasPermission } = usePermissions(); | ||
return hasPermission(permission, objectPermissions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters