adds access modifiers for each role

This commit is contained in:
Satyam-Rastogi
2026-05-12 21:08:19 +05:30
parent e85ecd2571
commit 7598f8982a
7 changed files with 517 additions and 119 deletions
+44
View File
@@ -0,0 +1,44 @@
import { useCallback, useMemo } from 'react';
import { useAuth } from '../context/AuthContext';
import { useMockStore } from '../data/mockStore';
import { resolveOrgRoleKey } from '../utils/permissions';
/**
* Hook that provides permission checks against the dynamic orgPermissions matrix.
*
* Usage:
* const { can, canAny, canAll, orgRoleKey } = usePermissions();
* if (can('jobs', 'edit')) { ... }
* if (canAny('leads', ['log', 'verify'])) { ... }
*/
export const usePermissions = () => {
const { user } = useAuth();
const { orgPermissions, orgMembers } = useMockStore();
const orgRoleKey = useMemo(
() => resolveOrgRoleKey(user, orgMembers),
[user, orgMembers],
);
const can = useCallback(
(moduleKey, action) => {
if (!orgRoleKey) return false;
if (orgRoleKey === 'OWNER') return true;
const actions = orgPermissions[moduleKey]?.[orgRoleKey] || [];
return actions.includes(action);
},
[orgRoleKey, orgPermissions],
);
const canAny = useCallback(
(moduleKey, actions) => actions.some(a => can(moduleKey, a)),
[can],
);
const canAll = useCallback(
(moduleKey, actions) => actions.every(a => can(moduleKey, a)),
[can],
);
return { can, canAny, canAll, orgRoleKey };
};