Fix array fill logicl; allow matching on any permissions
This commit is contained in:
parent
9b4f2deb78
commit
5d5a5c2afc
3 changed files with 14 additions and 6 deletions
|
@ -3,16 +3,26 @@ import { usePermissions } from '@/plugins/usePermissions';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
action: string | string[];
|
action: string | string[];
|
||||||
|
matchAny?: boolean;
|
||||||
renderOnError?: React.ReactNode | null;
|
renderOnError?: React.ReactNode | null;
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Can = ({ action, renderOnError, children }: Props) => {
|
const Can = ({ action, matchAny = false, renderOnError, children }: Props) => {
|
||||||
const can = usePermissions(action);
|
const can = usePermissions(action);
|
||||||
|
|
||||||
|
if (matchAny) {
|
||||||
|
console.log('Can.tsx', can);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{can.every(p => p) ? children : renderOnError}
|
{
|
||||||
|
((matchAny && can.filter(p => p).length > 0) || (!matchAny && can.every(p => p))) ?
|
||||||
|
children
|
||||||
|
:
|
||||||
|
renderOnError
|
||||||
|
}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,9 +6,7 @@ export const usePermissions = (action: string | string[]): boolean[] => {
|
||||||
|
|
||||||
return useDeepMemo(() => {
|
return useDeepMemo(() => {
|
||||||
if (userPermissions[0] === '*') {
|
if (userPermissions[0] === '*') {
|
||||||
return ([] as boolean[]).fill(
|
return Array(Array.isArray(action) ? action.length : 1).fill(true);
|
||||||
true, 0, Array.isArray(action) ? action.length : 1,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (Array.isArray(action) ? action : [ action ])
|
return (Array.isArray(action) ? action : [ action ])
|
||||||
|
|
|
@ -47,7 +47,7 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>)
|
||||||
<Can action={'user.*'}>
|
<Can action={'user.*'}>
|
||||||
<NavLink to={`${match.url}/users`}>Users</NavLink>
|
<NavLink to={`${match.url}/users`}>Users</NavLink>
|
||||||
</Can>
|
</Can>
|
||||||
<Can action={'settings.*'}>
|
<Can action={['settings.*', 'file.sftp']} matchAny={true}>
|
||||||
<NavLink to={`${match.url}/settings`}>Settings</NavLink>
|
<NavLink to={`${match.url}/settings`}>Settings</NavLink>
|
||||||
</Can>
|
</Can>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue