adds access modifiers for each role
This commit is contained in:
@@ -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 };
|
||||
};
|
||||
Reference in New Issue
Block a user