Skip to content

Commit 5d5a5c2

Browse files
committed
Fix array fill logicl; allow matching on any permissions
1 parent 9b4f2de commit 5d5a5c2

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

resources/scripts/components/elements/Can.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@ import { usePermissions } from '@/plugins/usePermissions';
33

44
interface Props {
55
action: string | string[];
6+
matchAny?: boolean;
67
renderOnError?: React.ReactNode | null;
78
children: React.ReactNode;
89
}
910

10-
const Can = ({ action, renderOnError, children }: Props) => {
11+
const Can = ({ action, matchAny = false, renderOnError, children }: Props) => {
1112
const can = usePermissions(action);
1213

14+
if (matchAny) {
15+
console.log('Can.tsx', can);
16+
}
17+
1318
return (
1419
<>
15-
{can.every(p => p) ? children : renderOnError}
20+
{
21+
((matchAny && can.filter(p => p).length > 0) || (!matchAny && can.every(p => p))) ?
22+
children
23+
:
24+
renderOnError
25+
}
1626
</>
1727
);
1828
};

resources/scripts/plugins/usePermissions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ export const usePermissions = (action: string | string[]): boolean[] => {
66

77
return useDeepMemo(() => {
88
if (userPermissions[0] === '*') {
9-
return ([] as boolean[]).fill(
10-
true, 0, Array.isArray(action) ? action.length : 1,
11-
);
9+
return Array(Array.isArray(action) ? action.length : 1).fill(true);
1210
}
1311

1412
return (Array.isArray(action) ? action : [ action ])

resources/scripts/routers/ServerRouter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>)
4747
<Can action={'user.*'}>
4848
<NavLink to={`${match.url}/users`}>Users</NavLink>
4949
</Can>
50-
<Can action={'settings.*'}>
50+
<Can action={['settings.*', 'file.sftp']} matchAny={true}>
5151
<NavLink to={`${match.url}/settings`}>Settings</NavLink>
5252
</Can>
5353
</div>

0 commit comments

Comments
 (0)