@umituz/web-dashboard 7.5.0 → 7.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/domains/permissions/PermissionsContext.test.tsx +144 -0
- package/src/domains/permissions/PermissionsContext.tsx +50 -17
- package/src/domains/permissions/hasPermission.test.ts +51 -1
- package/src/domains/permissions/hasPermission.ts +27 -4
- package/src/domains/permissions/index.ts +8 -1
- package/src/domains/permissions/types.ts +17 -0
- package/src/domains/settings/components/PermissionMatrixScreen.test.tsx +90 -0
- package/src/domains/settings/components/PermissionMatrixScreen.tsx +65 -33
- package/src/styles/chrome.css +14 -31
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-dashboard",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.6.0",
|
|
4
4
|
"description": "Installable app skeleton for the fleet — layouts, analytics (dependency-free SVG charts), calendar, onboarding flow permissions/module registry; udb-* stylesheet on design-system tokens",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"@types/react-dom": "^19.2.3",
|
|
81
81
|
"@typescript-eslint/eslint-plugin": "^8.57.2",
|
|
82
82
|
"@typescript-eslint/parser": "^8.57.2",
|
|
83
|
-
"@umituz/web-design-system": "^4.
|
|
83
|
+
"@umituz/web-design-system": "^4.29.4",
|
|
84
84
|
"eslint": "^10.0.2",
|
|
85
85
|
"framer-motion": "^12.23.12",
|
|
86
86
|
"jsdom": "^29.1.1",
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { render, screen } from "@testing-library/react";
|
|
3
|
+
import { MemoryRouter, Route, Routes } from "react-router";
|
|
4
|
+
import {
|
|
5
|
+
Can,
|
|
6
|
+
PermissionsProvider,
|
|
7
|
+
RequirePermission,
|
|
8
|
+
usePermissions,
|
|
9
|
+
} from "./PermissionsContext";
|
|
10
|
+
import type { PermissionMatrix } from "./types";
|
|
11
|
+
|
|
12
|
+
const MATRIX: PermissionMatrix = {
|
|
13
|
+
owner: "*",
|
|
14
|
+
editor: ["orders.view", "orders.edit"],
|
|
15
|
+
viewer: ["orders.view"],
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const mount = (role: string | null, node: React.ReactNode) =>
|
|
19
|
+
render(
|
|
20
|
+
<PermissionsProvider matrix={MATRIX} role={role}>
|
|
21
|
+
{node}
|
|
22
|
+
</PermissionsProvider>,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
describe("usePermissions", () => {
|
|
26
|
+
it("exposes role, can and canAny from the provider", () => {
|
|
27
|
+
const captured: ReturnType<typeof usePermissions>[] = [];
|
|
28
|
+
const Probe = () => {
|
|
29
|
+
captured.push(usePermissions());
|
|
30
|
+
return null;
|
|
31
|
+
};
|
|
32
|
+
mount("editor", <Probe />);
|
|
33
|
+
const value = captured[0]!;
|
|
34
|
+
expect(value.role).toBe("editor");
|
|
35
|
+
expect(value.can("orders.edit")).toBe(true);
|
|
36
|
+
expect(value.can("settings.manage")).toBe(false);
|
|
37
|
+
expect(value.canAny(["settings.manage", "orders.view"])).toBe(true);
|
|
38
|
+
expect(value.canAny(["settings.manage", "x.y"])).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("denies everything without a role", () => {
|
|
42
|
+
const captured: ReturnType<typeof usePermissions>[] = [];
|
|
43
|
+
const Probe = () => {
|
|
44
|
+
captured.push(usePermissions());
|
|
45
|
+
return null;
|
|
46
|
+
};
|
|
47
|
+
mount(null, <Probe />);
|
|
48
|
+
const value = captured[0]!;
|
|
49
|
+
expect(value.can("orders.view")).toBe(false);
|
|
50
|
+
expect(value.canAny(["orders.view"])).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("RequirePermission", () => {
|
|
55
|
+
it("renders children when the permission is granted", () => {
|
|
56
|
+
mount("editor", (
|
|
57
|
+
<RequirePermission permission="orders.edit">
|
|
58
|
+
<span>Edit tools</span>
|
|
59
|
+
</RequirePermission>
|
|
60
|
+
));
|
|
61
|
+
expect(screen.getByText("Edit tools")).not.toBeNull();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("renders the fallback when denied", () => {
|
|
65
|
+
mount("viewer", (
|
|
66
|
+
<RequirePermission permission="orders.edit" fallback={<span>No access</span>}>
|
|
67
|
+
<span>Edit tools</span>
|
|
68
|
+
</RequirePermission>
|
|
69
|
+
));
|
|
70
|
+
expect(screen.queryByText("Edit tools")).toBeNull();
|
|
71
|
+
expect(screen.getByText("No access")).not.toBeNull();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("accepts anyOf for alternative permissions", () => {
|
|
75
|
+
mount("viewer", (
|
|
76
|
+
<RequirePermission anyOf={["orders.edit", "orders.view"]}>
|
|
77
|
+
<span>Visible</span>
|
|
78
|
+
</RequirePermission>
|
|
79
|
+
));
|
|
80
|
+
expect(screen.getByText("Visible")).not.toBeNull();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("denies when neither permission nor anyOf is given", () => {
|
|
84
|
+
mount("owner", (
|
|
85
|
+
<RequirePermission fallback={<span>Denied</span>}>
|
|
86
|
+
<span>Visible</span>
|
|
87
|
+
</RequirePermission>
|
|
88
|
+
));
|
|
89
|
+
expect(screen.queryByText("Visible")).toBeNull();
|
|
90
|
+
expect(screen.getByText("Denied")).not.toBeNull();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("redirects to redirectTo when denied", () => {
|
|
94
|
+
render(
|
|
95
|
+
<MemoryRouter initialEntries={["/dashboard"]}>
|
|
96
|
+
<Routes>
|
|
97
|
+
<Route
|
|
98
|
+
path="/dashboard"
|
|
99
|
+
element={
|
|
100
|
+
<PermissionsProvider matrix={MATRIX} role="viewer">
|
|
101
|
+
<RequirePermission permission="orders.edit" redirectTo="/signin">
|
|
102
|
+
<span>Dashboard</span>
|
|
103
|
+
</RequirePermission>
|
|
104
|
+
</PermissionsProvider>
|
|
105
|
+
}
|
|
106
|
+
/>
|
|
107
|
+
<Route path="/signin" element={<span>Sign in</span>} />
|
|
108
|
+
</Routes>
|
|
109
|
+
</MemoryRouter>,
|
|
110
|
+
);
|
|
111
|
+
expect(screen.queryByText("Dashboard")).toBeNull();
|
|
112
|
+
expect(screen.getByText("Sign in")).not.toBeNull();
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("Can", () => {
|
|
117
|
+
it("renders children while allowed and the fallback otherwise", () => {
|
|
118
|
+
const { rerender } = mount("editor", (
|
|
119
|
+
<Can permission="orders.view" fallback={<span>Hidden</span>}>
|
|
120
|
+
<span>Orders</span>
|
|
121
|
+
</Can>
|
|
122
|
+
));
|
|
123
|
+
expect(screen.getByText("Orders")).not.toBeNull();
|
|
124
|
+
|
|
125
|
+
rerender(
|
|
126
|
+
<PermissionsProvider matrix={MATRIX} role="viewer">
|
|
127
|
+
<Can permission="orders.edit" fallback={<span>Hidden</span>}>
|
|
128
|
+
<span>Orders</span>
|
|
129
|
+
</Can>
|
|
130
|
+
</PermissionsProvider>,
|
|
131
|
+
);
|
|
132
|
+
expect(screen.queryByText("Orders")).toBeNull();
|
|
133
|
+
expect(screen.getByText("Hidden")).not.toBeNull();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("supports anyOf", () => {
|
|
137
|
+
mount("viewer", (
|
|
138
|
+
<Can anyOf={["settings.manage", "orders.view"]}>
|
|
139
|
+
<span>Orders</span>
|
|
140
|
+
</Can>
|
|
141
|
+
));
|
|
142
|
+
expect(screen.getByText("Orders")).not.toBeNull();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createContext, useCallback, useContext, useMemo, type ReactNode } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { Navigate } from "react-router";
|
|
3
|
+
import { hasAnyPermission, hasPermission } from "./hasPermission";
|
|
3
4
|
import type { Permission, PermissionMatrix, RoleKey } from "./types";
|
|
4
5
|
|
|
5
6
|
export interface PermissionsProviderProps {
|
|
@@ -12,6 +13,7 @@ interface PermissionsContextValue {
|
|
|
12
13
|
matrix: PermissionMatrix;
|
|
13
14
|
role: RoleKey | null;
|
|
14
15
|
can: (permission: Permission) => boolean;
|
|
16
|
+
canAny: (permissions: readonly Permission[]) => boolean;
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
const PermissionsContext = createContext<PermissionsContextValue | null>(null);
|
|
@@ -26,16 +28,21 @@ export const PermissionsProvider = ({
|
|
|
26
28
|
[matrix, role],
|
|
27
29
|
);
|
|
28
30
|
|
|
31
|
+
const canAny = useCallback(
|
|
32
|
+
(permissions: readonly Permission[]) =>
|
|
33
|
+
hasAnyPermission(matrix, role, permissions),
|
|
34
|
+
[matrix, role],
|
|
35
|
+
);
|
|
36
|
+
|
|
29
37
|
const value = useMemo<PermissionsContextValue>(
|
|
30
|
-
() => ({ matrix, role: role ?? null, can }),
|
|
31
|
-
[matrix, role, can],
|
|
38
|
+
() => ({ matrix, role: role ?? null, can, canAny }),
|
|
39
|
+
[matrix, role, can, canAny],
|
|
32
40
|
);
|
|
33
41
|
|
|
34
42
|
return (
|
|
35
43
|
<PermissionsContext.Provider value={value}>
|
|
36
44
|
{children}
|
|
37
45
|
</PermissionsContext.Provider>
|
|
38
|
-
|
|
39
46
|
);
|
|
40
47
|
};
|
|
41
48
|
|
|
@@ -47,29 +54,55 @@ export const usePermissions = (): PermissionsContextValue => {
|
|
|
47
54
|
return context;
|
|
48
55
|
};
|
|
49
56
|
|
|
57
|
+
const resolveAllowed = (
|
|
58
|
+
can: (permission: Permission) => boolean,
|
|
59
|
+
canAny: (permissions: readonly Permission[]) => boolean,
|
|
60
|
+
permission?: Permission,
|
|
61
|
+
anyOf?: readonly Permission[],
|
|
62
|
+
): boolean =>
|
|
63
|
+
permission
|
|
64
|
+
? anyOf
|
|
65
|
+
? can(permission) || canAny(anyOf)
|
|
66
|
+
: can(permission)
|
|
67
|
+
: anyOf
|
|
68
|
+
? canAny(anyOf)
|
|
69
|
+
: false;
|
|
70
|
+
|
|
50
71
|
export interface RequirePermissionProps {
|
|
51
|
-
permission
|
|
52
|
-
|
|
72
|
+
permission?: Permission;
|
|
73
|
+
anyOf?: readonly Permission[];
|
|
74
|
+
children?: ReactNode;
|
|
53
75
|
fallback?: ReactNode;
|
|
76
|
+
redirectTo?: string;
|
|
54
77
|
}
|
|
55
78
|
|
|
56
79
|
export const RequirePermission = ({
|
|
57
80
|
permission,
|
|
81
|
+
anyOf,
|
|
58
82
|
children,
|
|
59
83
|
fallback = null,
|
|
84
|
+
redirectTo,
|
|
60
85
|
}: RequirePermissionProps) => {
|
|
61
|
-
const { can } = usePermissions();
|
|
62
|
-
if (can
|
|
63
|
-
|
|
86
|
+
const { can, canAny } = usePermissions();
|
|
87
|
+
if (resolveAllowed(can, canAny, permission, anyOf)) return <>{children}</>;
|
|
88
|
+
if (redirectTo !== undefined) return <Navigate to={redirectTo} replace />;
|
|
64
89
|
return <>{fallback}</>;
|
|
65
|
-
|
|
66
90
|
};
|
|
67
91
|
|
|
68
|
-
export interface CanProps
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
92
|
+
export interface CanProps {
|
|
93
|
+
permission?: Permission;
|
|
94
|
+
anyOf?: readonly Permission[];
|
|
95
|
+
children?: ReactNode;
|
|
96
|
+
fallback?: ReactNode;
|
|
97
|
+
}
|
|
74
98
|
|
|
75
|
-
|
|
99
|
+
export const Can = ({
|
|
100
|
+
permission,
|
|
101
|
+
anyOf,
|
|
102
|
+
children,
|
|
103
|
+
fallback = null,
|
|
104
|
+
}: CanProps) => {
|
|
105
|
+
const { can, canAny } = usePermissions();
|
|
106
|
+
const allowed = resolveAllowed(can, canAny, permission, anyOf);
|
|
107
|
+
return allowed ? <>{children}</> : <>{fallback}</>;
|
|
108
|
+
};
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
effectivePermissions,
|
|
4
|
+
hasAnyPermission,
|
|
5
|
+
hasPermission,
|
|
6
|
+
} from "./hasPermission";
|
|
3
7
|
import type { PermissionMatrix } from "./types";
|
|
4
8
|
|
|
5
9
|
const MATRIX: PermissionMatrix = {
|
|
6
10
|
owner: "*",
|
|
7
11
|
admin: ["dashboard.view", "sales.*", "reports.view"],
|
|
12
|
+
colonAdmin: ["orders:*"],
|
|
8
13
|
viewer: ["dashboard.view"],
|
|
9
14
|
nobody: [],
|
|
10
15
|
};
|
|
@@ -30,6 +35,12 @@ describe("hasPermission", () => {
|
|
|
30
35
|
expect(hasPermission(MATRIX, "admin", "salesforce.view")).toBe(false);
|
|
31
36
|
});
|
|
32
37
|
|
|
38
|
+
it("honors colon-separated module wildcards", () => {
|
|
39
|
+
expect(hasPermission(MATRIX, "colonAdmin", "orders:create")).toBe(true);
|
|
40
|
+
expect(hasPermission(MATRIX, "colonAdmin", "orders:delete")).toBe(true);
|
|
41
|
+
expect(hasPermission(MATRIX, "colonAdmin", "ordersArchive:create")).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
33
44
|
it("denies unknown roles", () => {
|
|
34
45
|
expect(hasPermission(MATRIX, "intruder", "dashboard.view")).toBe(false);
|
|
35
46
|
});
|
|
@@ -47,4 +58,43 @@ describe("hasPermission", () => {
|
|
|
47
58
|
expect(hasPermission(MATRIX, null, "dashboard.view")).toBe(false);
|
|
48
59
|
expect(hasPermission(undefined, undefined, "x")).toBe(false);
|
|
49
60
|
});
|
|
61
|
+
|
|
62
|
+
it("hasAnyPermission requires at least one grant", () => {
|
|
63
|
+
expect(
|
|
64
|
+
hasAnyPermission(MATRIX, "admin", ["sales.view", "settings.manage"]),
|
|
65
|
+
).toBe(true);
|
|
66
|
+
expect(
|
|
67
|
+
hasAnyPermission(MATRIX, "viewer", ["sales.view", "settings.manage"]),
|
|
68
|
+
).toBe(false);
|
|
69
|
+
expect(hasAnyPermission(MATRIX, "viewer", [])).toBe(false);
|
|
70
|
+
expect(hasAnyPermission(MATRIX, null, ["dashboard.view"])).toBe(false);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("effectivePermissions", () => {
|
|
75
|
+
it("returns the base set without overrides", () => {
|
|
76
|
+
const result = effectivePermissions(["a.b", "c.d"]);
|
|
77
|
+
expect([...result].sort()).toEqual(["a.b", "c.d"]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("unions grants and subtracts revokes", () => {
|
|
81
|
+
const result = effectivePermissions(["a.b", "c.d"], {
|
|
82
|
+
grant: ["e.f"],
|
|
83
|
+
revoke: ["a.b"],
|
|
84
|
+
});
|
|
85
|
+
expect([...result].sort()).toEqual(["c.d", "e.f"]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("lets revokes win over grants", () => {
|
|
89
|
+
const result = effectivePermissions(["a.b"], {
|
|
90
|
+
grant: ["c.d"],
|
|
91
|
+
revoke: ["c.d"],
|
|
92
|
+
});
|
|
93
|
+
expect([...result]).toEqual(["a.b"]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("deduplicates repeated permissions", () => {
|
|
97
|
+
const result = effectivePermissions(["a.b"], { grant: ["a.b"] });
|
|
98
|
+
expect(result.size).toBe(1);
|
|
99
|
+
});
|
|
50
100
|
});
|
|
@@ -2,19 +2,27 @@ import {
|
|
|
2
2
|
PERMISSION_WILDCARD,
|
|
3
3
|
type Permission,
|
|
4
4
|
type PermissionMatrix,
|
|
5
|
+
type PermissionOverrides,
|
|
5
6
|
type RoleGrant,
|
|
6
7
|
type RoleKey,
|
|
7
8
|
} from "./types";
|
|
8
9
|
|
|
9
|
-
const
|
|
10
|
+
const MODULE_WILDCARD_SUFFIXES = [".*", ":*"] as const;
|
|
11
|
+
|
|
12
|
+
const matchesModuleWildcard = (
|
|
13
|
+
entry: Permission,
|
|
14
|
+
permission: Permission,
|
|
15
|
+
): boolean =>
|
|
16
|
+
MODULE_WILDCARD_SUFFIXES.some((suffix) => {
|
|
17
|
+
if (!entry.endsWith(suffix)) return false;
|
|
18
|
+
return permission.startsWith(entry.slice(0, -suffix.length + 1));
|
|
19
|
+
});
|
|
10
20
|
|
|
11
21
|
const grantIncludes = (grant: RoleGrant, permission: Permission): boolean => {
|
|
12
22
|
if (grant === PERMISSION_WILDCARD) return true;
|
|
13
23
|
return grant.some(
|
|
14
24
|
(entry) =>
|
|
15
|
-
entry === permission ||
|
|
16
|
-
(entry.endsWith(MODULE_WILDCARD_SUFFIX) &&
|
|
17
|
-
permission.startsWith(entry.slice(0, -1))),
|
|
25
|
+
entry === permission || matchesModuleWildcard(entry, permission),
|
|
18
26
|
);
|
|
19
27
|
};
|
|
20
28
|
|
|
@@ -28,3 +36,18 @@ export const hasPermission = (
|
|
|
28
36
|
if (grant === undefined) return false;
|
|
29
37
|
return grantIncludes(grant, permission);
|
|
30
38
|
};
|
|
39
|
+
|
|
40
|
+
export const hasAnyPermission = (
|
|
41
|
+
matrix: PermissionMatrix | null | undefined,
|
|
42
|
+
role: RoleKey | null | undefined,
|
|
43
|
+
permissions: readonly Permission[],
|
|
44
|
+
): boolean => permissions.some((p) => hasPermission(matrix, role, p));
|
|
45
|
+
|
|
46
|
+
export const effectivePermissions = (
|
|
47
|
+
base: readonly Permission[],
|
|
48
|
+
overrides?: PermissionOverrides,
|
|
49
|
+
): Set<Permission> => {
|
|
50
|
+
const grants = overrides?.grant ?? [];
|
|
51
|
+
const revokes = overrides?.revoke ?? [];
|
|
52
|
+
return new Set([...base, ...grants].filter((p) => !revokes.includes(p)));
|
|
53
|
+
};
|
|
@@ -7,11 +7,18 @@ export {
|
|
|
7
7
|
type RequirePermissionProps,
|
|
8
8
|
type CanProps,
|
|
9
9
|
} from "./PermissionsContext";
|
|
10
|
-
export {
|
|
10
|
+
export {
|
|
11
|
+
hasPermission,
|
|
12
|
+
hasAnyPermission,
|
|
13
|
+
effectivePermissions,
|
|
14
|
+
} from "./hasPermission";
|
|
11
15
|
export {
|
|
12
16
|
PERMISSION_WILDCARD,
|
|
13
17
|
type Permission,
|
|
14
18
|
type PermissionMatrix,
|
|
19
|
+
type PermissionOverrides,
|
|
20
|
+
type PermissionEntry,
|
|
21
|
+
type PermissionGroup,
|
|
15
22
|
type RoleGrant,
|
|
16
23
|
type RoleKey,
|
|
17
24
|
type RoleInfo,
|
|
@@ -13,3 +13,20 @@ export interface RoleInfo {
|
|
|
13
13
|
label: string;
|
|
14
14
|
description?: string;
|
|
15
15
|
}
|
|
16
|
+
|
|
17
|
+
export interface PermissionOverrides {
|
|
18
|
+
grant?: readonly Permission[];
|
|
19
|
+
revoke?: readonly Permission[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface PermissionEntry {
|
|
23
|
+
key: Permission;
|
|
24
|
+
label?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface PermissionGroup {
|
|
28
|
+
key?: string;
|
|
29
|
+
label?: string;
|
|
30
|
+
permissions: readonly (Permission | PermissionEntry)[];
|
|
31
|
+
}
|
|
32
|
+
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { renderToString } from "react-dom/server";
|
|
3
|
+
import { PermissionMatrixScreen } from "./PermissionMatrixScreen";
|
|
4
|
+
import type { PermissionMatrix } from "../../permissions/types";
|
|
5
|
+
|
|
6
|
+
const MATRIX: PermissionMatrix = {
|
|
7
|
+
owner: "*",
|
|
8
|
+
editor: ["orders.view", "orders.edit"],
|
|
9
|
+
viewer: ["orders.view"],
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const ROLES = [
|
|
13
|
+
{ key: "owner", label: "Owner" },
|
|
14
|
+
{ key: "editor", label: "Editor" },
|
|
15
|
+
{ key: "viewer", label: "Viewer" },
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
describe("PermissionMatrixScreen", () => {
|
|
19
|
+
it("renders grouped columns with labels and colSpan headers", () => {
|
|
20
|
+
const html = renderToString(
|
|
21
|
+
<PermissionMatrixScreen
|
|
22
|
+
matrix={MATRIX}
|
|
23
|
+
roles={ROLES}
|
|
24
|
+
groups={[
|
|
25
|
+
{
|
|
26
|
+
key: "orders",
|
|
27
|
+
label: "Orders",
|
|
28
|
+
permissions: [
|
|
29
|
+
{ key: "orders.view", label: "View orders" },
|
|
30
|
+
{ key: "orders.edit", label: "Edit orders" },
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
key: "settings",
|
|
35
|
+
label: "Settings",
|
|
36
|
+
permissions: ["settings.manage"],
|
|
37
|
+
},
|
|
38
|
+
]}
|
|
39
|
+
/>,
|
|
40
|
+
);
|
|
41
|
+
expect(html).toContain('colSpan="2"');
|
|
42
|
+
expect(html).toContain("Orders");
|
|
43
|
+
expect(html).toContain("Settings");
|
|
44
|
+
expect(html).toContain("View orders");
|
|
45
|
+
expect(html).toContain("Edit orders");
|
|
46
|
+
expect(html).toContain("settings.manage");
|
|
47
|
+
expect(html).toContain("udb-matrix__check");
|
|
48
|
+
expect(html).toContain("udb-matrix__deny");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("renders flat string columns when groups are omitted", () => {
|
|
52
|
+
const html = renderToString(
|
|
53
|
+
<PermissionMatrixScreen
|
|
54
|
+
matrix={MATRIX}
|
|
55
|
+
roles={ROLES}
|
|
56
|
+
permissions={["orders.view", "orders.edit"]}
|
|
57
|
+
/>,
|
|
58
|
+
);
|
|
59
|
+
expect(html).not.toContain("udb-matrix__group");
|
|
60
|
+
expect(html).toContain("orders.view");
|
|
61
|
+
expect(html).toContain("orders.edit");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("renders toggle buttons with aria-pressed in editable mode", () => {
|
|
65
|
+
const html = renderToString(
|
|
66
|
+
<PermissionMatrixScreen
|
|
67
|
+
matrix={MATRIX}
|
|
68
|
+
roles={ROLES}
|
|
69
|
+
permissions={["orders.view", "orders.edit"]}
|
|
70
|
+
onToggle={() => undefined}
|
|
71
|
+
/>,
|
|
72
|
+
);
|
|
73
|
+
expect(html).toContain('aria-pressed="true"');
|
|
74
|
+
expect(html).toContain('aria-pressed="false"');
|
|
75
|
+
expect(html).toContain('aria-label="Editor — orders.view"');
|
|
76
|
+
expect(html).toContain("udb-matrix__cell--on");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("stays read-only without onToggle", () => {
|
|
80
|
+
const html = renderToString(
|
|
81
|
+
<PermissionMatrixScreen
|
|
82
|
+
matrix={MATRIX}
|
|
83
|
+
roles={ROLES}
|
|
84
|
+
permissions={["orders.view"]}
|
|
85
|
+
/>,
|
|
86
|
+
);
|
|
87
|
+
expect(html).not.toContain("<button");
|
|
88
|
+
expect(html).not.toContain("aria-pressed");
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { Check, Minus } from "lucide-react";
|
|
2
|
+
import {
|
|
3
|
+
Table,
|
|
4
|
+
TableBody,
|
|
5
|
+
TableCell,
|
|
6
|
+
TableHead,
|
|
7
|
+
TableHeader,
|
|
8
|
+
TableRow,
|
|
9
|
+
} from "@umituz/web-design-system/organisms";
|
|
2
10
|
import { cn } from "@umituz/web-design-system/utils/cn";
|
|
3
11
|
import { hasPermission } from "../../permissions/hasPermission";
|
|
4
12
|
import type {
|
|
5
13
|
Permission,
|
|
14
|
+
PermissionEntry,
|
|
15
|
+
PermissionGroup,
|
|
6
16
|
PermissionMatrix,
|
|
7
17
|
RoleInfo,
|
|
8
18
|
} from "../../permissions/types";
|
|
@@ -10,45 +20,72 @@ import type {
|
|
|
10
20
|
export interface PermissionMatrixScreenProps {
|
|
11
21
|
matrix: PermissionMatrix;
|
|
12
22
|
roles: RoleInfo[];
|
|
13
|
-
|
|
23
|
+
groups?: PermissionGroup[];
|
|
24
|
+
permissions?: (Permission | PermissionEntry)[];
|
|
14
25
|
onToggle?: (role: string, permission: Permission, granted: boolean) => void;
|
|
15
26
|
}
|
|
16
27
|
|
|
28
|
+
const toEntry = (
|
|
29
|
+
column: Permission | PermissionEntry,
|
|
30
|
+
): { key: Permission; label: string } =>
|
|
31
|
+
typeof column === "string"
|
|
32
|
+
? { key: column, label: column }
|
|
33
|
+
: { key: column.key, label: column.label ?? column.key };
|
|
34
|
+
|
|
17
35
|
export const PermissionMatrixScreen = ({
|
|
18
36
|
matrix,
|
|
19
37
|
roles,
|
|
38
|
+
groups,
|
|
20
39
|
permissions,
|
|
21
40
|
onToggle,
|
|
22
|
-
}: PermissionMatrixScreenProps) =>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
<th className="udb-table__th" />
|
|
28
|
-
{permissions.map((permission) => (
|
|
29
|
-
<th key={permission} className="udb-table__th udb-table__th--code">
|
|
30
|
-
{permission}
|
|
31
|
-
</th>
|
|
41
|
+
}: PermissionMatrixScreenProps) => {
|
|
42
|
+
const columns = groups
|
|
43
|
+
? groups.flatMap((group) => group.permissions.map(toEntry))
|
|
44
|
+
: (permissions ?? []).map(toEntry);
|
|
45
|
+
const hasGroups = Boolean(groups && groups.length > 0);
|
|
32
46
|
|
|
47
|
+
return (
|
|
48
|
+
<Table className="udb-matrix">
|
|
49
|
+
<TableHeader>
|
|
50
|
+
{hasGroups && (
|
|
51
|
+
<TableRow className="udb-matrix__group-row">
|
|
52
|
+
<TableHead className="udb-matrix__corner" />
|
|
53
|
+
{groups!.map((group, index) => (
|
|
54
|
+
<TableHead
|
|
55
|
+
key={group.key ?? group.label ?? index}
|
|
56
|
+
colSpan={group.permissions.length}
|
|
57
|
+
scope="colgroup"
|
|
58
|
+
className="udb-matrix__group"
|
|
59
|
+
>
|
|
60
|
+
{group.label ?? ""}
|
|
61
|
+
</TableHead>
|
|
62
|
+
))}
|
|
63
|
+
</TableRow>
|
|
64
|
+
)}
|
|
65
|
+
<TableRow>
|
|
66
|
+
<TableHead />
|
|
67
|
+
{columns.map((column) => (
|
|
68
|
+
<TableHead key={column.key} className="udb-matrix__col">
|
|
69
|
+
{column.label}
|
|
70
|
+
</TableHead>
|
|
33
71
|
))}
|
|
34
|
-
</
|
|
35
|
-
|
|
36
|
-
</thead>
|
|
72
|
+
</TableRow>
|
|
73
|
+
</TableHeader>
|
|
37
74
|
|
|
38
|
-
<
|
|
75
|
+
<TableBody>
|
|
39
76
|
{roles.map((role) => (
|
|
40
|
-
<
|
|
41
|
-
<
|
|
77
|
+
<TableRow key={role.key}>
|
|
78
|
+
<TableCell className="udb-matrix__role">{role.label}</TableCell>
|
|
42
79
|
|
|
43
|
-
{
|
|
44
|
-
const granted = hasPermission(matrix, role.key,
|
|
80
|
+
{columns.map((column) => {
|
|
81
|
+
const granted = hasPermission(matrix, role.key, column.key);
|
|
45
82
|
const cell = granted ? (
|
|
46
83
|
<Check className="udb-matrix__check" aria-hidden="true" />
|
|
47
84
|
) : (
|
|
48
85
|
<Minus className="udb-matrix__deny" aria-hidden="true" />
|
|
49
86
|
);
|
|
50
87
|
return (
|
|
51
|
-
<
|
|
88
|
+
<TableCell key={column.key} className="udb-matrix__cell-td">
|
|
52
89
|
{onToggle ? (
|
|
53
90
|
<button
|
|
54
91
|
type="button"
|
|
@@ -57,27 +94,22 @@ export const PermissionMatrixScreen = ({
|
|
|
57
94
|
granted && "udb-matrix__cell--on",
|
|
58
95
|
)}
|
|
59
96
|
aria-pressed={granted}
|
|
60
|
-
|
|
97
|
+
aria-label={`${role.label} — ${column.label}`}
|
|
98
|
+
onClick={() => onToggle(role.key, column.key, granted)}
|
|
61
99
|
>
|
|
62
100
|
{cell}
|
|
63
101
|
</button>
|
|
64
|
-
|
|
65
102
|
) : (
|
|
66
103
|
cell
|
|
67
104
|
)}
|
|
68
|
-
</
|
|
69
|
-
|
|
105
|
+
</TableCell>
|
|
70
106
|
);
|
|
71
107
|
})}
|
|
72
|
-
</
|
|
73
|
-
|
|
108
|
+
</TableRow>
|
|
74
109
|
))}
|
|
75
|
-
</
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
</div>
|
|
80
|
-
|
|
81
|
-
);
|
|
110
|
+
</TableBody>
|
|
111
|
+
</Table>
|
|
112
|
+
);
|
|
113
|
+
};
|
|
82
114
|
|
|
83
115
|
export default PermissionMatrixScreen;
|
package/src/styles/chrome.css
CHANGED
|
@@ -1275,50 +1275,33 @@
|
|
|
1275
1275
|
font-size: var(--udb-font-size-sm);
|
|
1276
1276
|
color: var(--color-muted-foreground);
|
|
1277
1277
|
}
|
|
1278
|
-
.udb-
|
|
1279
|
-
width: 100%;
|
|
1280
|
-
overflow-x: auto;
|
|
1281
|
-
border: 1px solid var(--color-border);
|
|
1282
|
-
border-radius: var(--radius-lg);
|
|
1283
|
-
background: var(--color-card);
|
|
1284
|
-
}
|
|
1285
|
-
.udb-table {
|
|
1286
|
-
width: 100%;
|
|
1287
|
-
border-collapse: collapse;
|
|
1288
|
-
font-size: var(--udb-font-size-md);
|
|
1289
|
-
}
|
|
1290
|
-
.udb-table__th {
|
|
1291
|
-
padding: var(--udb-space-2) var(--udb-space-3);
|
|
1292
|
-
border-bottom: 1px solid var(--udb-border-strong);
|
|
1293
|
-
text-align: left;
|
|
1278
|
+
.udb-matrix__group-row th {
|
|
1294
1279
|
font-size: var(--udb-font-size-xs);
|
|
1295
1280
|
font-weight: var(--font-weight-bold);
|
|
1296
1281
|
text-transform: uppercase;
|
|
1297
1282
|
letter-spacing: 0.06em;
|
|
1298
1283
|
color: var(--color-muted-foreground);
|
|
1299
|
-
|
|
1284
|
+
text-align: center;
|
|
1285
|
+
border-bottom: none;
|
|
1286
|
+
padding-bottom: 0;
|
|
1300
1287
|
}
|
|
1301
|
-
.udb-
|
|
1288
|
+
.udb-matrix__corner {
|
|
1289
|
+
border-bottom: none;
|
|
1290
|
+
padding-bottom: 0;
|
|
1291
|
+
}
|
|
1292
|
+
.udb-matrix__col {
|
|
1302
1293
|
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
1294
|
+
font-size: var(--udb-font-size-xs);
|
|
1303
1295
|
text-transform: none;
|
|
1304
1296
|
letter-spacing: 0;
|
|
1297
|
+
text-align: center;
|
|
1298
|
+
white-space: nowrap;
|
|
1305
1299
|
}
|
|
1306
|
-
.udb-
|
|
1307
|
-
padding: var(--udb-space-3);
|
|
1308
|
-
border-bottom: 1px solid var(--color-border);
|
|
1309
|
-
color: var(--color-foreground);
|
|
1310
|
-
}
|
|
1311
|
-
.udb-table__row:last-child .udb-table__td {
|
|
1312
|
-
border-bottom: none;
|
|
1313
|
-
}
|
|
1314
|
-
.udb-table__row:hover .udb-table__td {
|
|
1315
|
-
background: var(--color-muted);
|
|
1316
|
-
}
|
|
1317
|
-
.udb-table__td--strong {
|
|
1300
|
+
.udb-matrix__role {
|
|
1318
1301
|
font-weight: var(--font-weight-medium);
|
|
1319
1302
|
white-space: nowrap;
|
|
1320
1303
|
}
|
|
1321
|
-
.udb-
|
|
1304
|
+
.udb-matrix__cell-td {
|
|
1322
1305
|
text-align: center;
|
|
1323
1306
|
}
|
|
1324
1307
|
.udb-matrix__cell {
|