mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

* 🏗️ feat: Add Group model and schema with GroupType enum * 🏗️ feat: Introduce Permissions module and refactor role-based access control * 🏗️ feat: Refactor permissions handling and consolidate permission schemas * 🏗️ feat: Refactor role permissions handling and improve role initialization logic * 🏗️ feat: Update Role.spec.js to improve imports and enhance test structure * 🏗️ feat: Update access control logic to ensure proper permission checks in role handling * 🏗️ chore: Bump versions for librechat-data-provider to 0.7.75 and @librechat/data-schemas to 0.0.6 * 🏗️ feat: Improve role permissions handling by ensuring defaults are applied correctly * 🏗️ feat: Update role permissions schema to comment out unused SHARE permission * 🏗️ chore: Bump version of librechat-data-provider to 0.7.77 and remove unused groups field from IUser interface * 🏗️ chore: Downgrade version of librechat-data-provider to 0.7.76 * 🔧 chore: Bump versions for librechat-data-provider to 0.7.77 and data-schemas to 0.0.6 * 🏗️ chore: Update version of librechat-data-provider to 0.7.789 --------- Co-authored-by: Danny Avila <danny@librechat.ai>
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { useMemo, useCallback, useContext } from 'react';
|
|
import type { TUser, PermissionTypes, Permissions } from 'librechat-data-provider';
|
|
import { AuthContext } from '~/hooks/AuthContext';
|
|
|
|
const useHasAccess = ({
|
|
permissionType,
|
|
permission,
|
|
}: {
|
|
permissionType: PermissionTypes;
|
|
permission: Permissions;
|
|
}) => {
|
|
const authContext = useContext(AuthContext);
|
|
const user = authContext?.user;
|
|
const roles = authContext?.roles;
|
|
const isAuthenticated = authContext?.isAuthenticated || false;
|
|
|
|
const checkAccess = useCallback(
|
|
({
|
|
user,
|
|
permissionType,
|
|
permission,
|
|
}: {
|
|
user?: TUser | null;
|
|
permissionType: PermissionTypes;
|
|
permission: Permissions;
|
|
}) => {
|
|
if (!authContext) {
|
|
return false;
|
|
}
|
|
|
|
if (isAuthenticated && user?.role != null && roles && roles[user.role]) {
|
|
return roles[user.role]?.permissions?.[permissionType]?.[permission] === true;
|
|
}
|
|
return false;
|
|
},
|
|
[authContext, isAuthenticated, roles],
|
|
);
|
|
|
|
const hasAccess = useMemo(
|
|
() => checkAccess({ user, permissionType, permission }),
|
|
[user, permissionType, permission, checkAccess],
|
|
);
|
|
|
|
return hasAccess;
|
|
};
|
|
|
|
export default useHasAccess;
|