@umituz/web-dashboard 8.9.0 → 8.11.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 +1 -1
- package/src/domains/layouts/types/layout.ts +1 -0
- package/src/domains/layouts/types/sidebar.ts +1 -0
- package/src/domains/layouts/utils/dashboard.test.ts +41 -23
- package/src/domains/layouts/utils/dashboard.ts +26 -8
- package/src/domains/permissions/AccessDenied.tsx +43 -0
- package/src/domains/permissions/PermissionsContext.test.tsx +49 -0
- package/src/domains/permissions/PermissionsContext.tsx +47 -0
- package/src/domains/permissions/hasPermission.ts +3 -1
- package/src/domains/permissions/index.ts +7 -0
- package/src/domains/tour/tourEngine.ts +3 -3
- package/src/domains/tour/types.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-dashboard",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.11.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",
|
|
@@ -64,37 +64,55 @@ describe("getPageTitle", () => {
|
|
|
64
64
|
});
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
describe("filterSidebarItems & filterProfileItems (
|
|
68
|
-
it("filters items based on role access", () => {
|
|
67
|
+
describe("filterSidebarItems & filterProfileItems (Dynamic RBAC & Permission Control)", () => {
|
|
68
|
+
it("filters items based on generic SaaS role access", () => {
|
|
69
69
|
const items = [
|
|
70
70
|
{ path: "/overview", label: "Overview" },
|
|
71
|
-
{ path: "/
|
|
71
|
+
{ path: "/editor-tools", label: "Editor Tools", roles: ["editor", "admin"] },
|
|
72
72
|
{ path: "/admin", label: "Admin Panel", roles: ["admin"] },
|
|
73
73
|
];
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const teacherUser = { id: "2", role: "teacher" };
|
|
84
|
-
const teacherItems = items.filter((item) =>
|
|
85
|
-
filterSidebarItems([item], teacherUser).length > 0
|
|
86
|
-
);
|
|
87
|
-
expect(teacherItems.map((i) => i.path)).toEqual(["/overview", "/studio"]);
|
|
88
|
-
|
|
89
|
-
// Admin user
|
|
75
|
+
const viewerUser = { id: "1", role: "viewer" };
|
|
76
|
+
const viewerItems = filterSidebarItems(items, viewerUser);
|
|
77
|
+
expect(viewerItems.map((i) => i.path)).toEqual(["/overview"]);
|
|
78
|
+
|
|
79
|
+
const editorUser = { id: "2", role: "editor" };
|
|
80
|
+
const editorItems = filterSidebarItems(items, editorUser);
|
|
81
|
+
expect(editorItems.map((i) => i.path)).toEqual(["/overview", "/editor-tools"]);
|
|
82
|
+
|
|
90
83
|
const adminUser = { id: "3", role: "admin" };
|
|
91
|
-
const adminItems = items
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
84
|
+
const adminItems = filterSidebarItems(items, adminUser);
|
|
85
|
+
expect(adminItems.map((i) => i.path)).toEqual(["/overview", "/editor-tools", "/admin"]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("filters items based on dynamic permission matrix", () => {
|
|
89
|
+
const matrix = {
|
|
90
|
+
viewer: ["projects.read"],
|
|
91
|
+
editor: ["projects.read", "projects.write"],
|
|
92
|
+
admin: ["*"],
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const items = [
|
|
96
|
+
{ path: "/projects", label: "Projects", permission: "projects.read" },
|
|
97
|
+
{ path: "/projects/new", label: "Create Project", permission: "projects.write" },
|
|
98
|
+
{ path: "/billing", label: "Billing", permission: "billing.manage" },
|
|
99
|
+
];
|
|
100
|
+
|
|
101
|
+
expect(filterSidebarItems(items, { role: "viewer", matrix }).map((i) => i.path)).toEqual([
|
|
102
|
+
"/projects",
|
|
103
|
+
]);
|
|
104
|
+
expect(filterSidebarItems(items, { role: "editor", matrix }).map((i) => i.path)).toEqual([
|
|
105
|
+
"/projects",
|
|
106
|
+
"/projects/new",
|
|
107
|
+
]);
|
|
108
|
+
expect(filterSidebarItems(items, { role: "admin", matrix }).map((i) => i.path)).toEqual([
|
|
109
|
+
"/projects",
|
|
110
|
+
"/projects/new",
|
|
111
|
+
"/billing",
|
|
112
|
+
]);
|
|
95
113
|
});
|
|
96
114
|
|
|
97
|
-
it("filters items based on explicit permissions", () => {
|
|
115
|
+
it("filters items based on explicit permissions array", () => {
|
|
98
116
|
const items = [
|
|
99
117
|
{ path: "/public", label: "Public" },
|
|
100
118
|
{ path: "/billing", label: "Billing", permission: "billing.manage" },
|
|
@@ -65,11 +65,15 @@ export function getPageTitle(
|
|
|
65
65
|
return null;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
import { hasAnyPermission, hasPermission } from "../../permissions/hasPermission";
|
|
69
|
+
import type { PermissionMatrix } from "../../permissions/types";
|
|
70
|
+
|
|
68
71
|
export interface AccessControllableItem {
|
|
69
72
|
enabled?: boolean;
|
|
70
73
|
requiredApp?: "mobile" | "web";
|
|
71
74
|
roles?: readonly string[];
|
|
72
75
|
permission?: string;
|
|
76
|
+
anyOf?: readonly string[];
|
|
73
77
|
permissions?: readonly string[];
|
|
74
78
|
}
|
|
75
79
|
|
|
@@ -77,6 +81,7 @@ export interface AccessControllableUser {
|
|
|
77
81
|
role?: string;
|
|
78
82
|
roles?: readonly string[];
|
|
79
83
|
permissions?: readonly string[];
|
|
84
|
+
matrix?: PermissionMatrix;
|
|
80
85
|
hasMobileApp?: boolean;
|
|
81
86
|
hasWebApp?: boolean;
|
|
82
87
|
}
|
|
@@ -100,17 +105,30 @@ export function isItemPermitted(
|
|
|
100
105
|
}
|
|
101
106
|
|
|
102
107
|
if (item.permission) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
108
|
+
if (user?.matrix && user?.role) {
|
|
109
|
+
if (!hasPermission(user.matrix, user.role, item.permission)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
const userPerms = user?.permissions ?? [];
|
|
114
|
+
if (!userPerms.includes("*") && !userPerms.includes(item.permission)) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
106
117
|
}
|
|
107
118
|
}
|
|
108
119
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
|
|
120
|
+
const anyOfList = item.anyOf ?? item.permissions;
|
|
121
|
+
if (anyOfList && anyOfList.length > 0) {
|
|
122
|
+
if (user?.matrix && user?.role) {
|
|
123
|
+
if (!hasAnyPermission(user.matrix, user.role, anyOfList)) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
const userPerms = user?.permissions ?? [];
|
|
128
|
+
if (!userPerms.includes("*")) {
|
|
129
|
+
const hasAny = anyOfList.some((p) => userPerms.includes(p));
|
|
130
|
+
if (!hasAny) return false;
|
|
131
|
+
}
|
|
114
132
|
}
|
|
115
133
|
}
|
|
116
134
|
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { ShieldAlert } from "lucide-react";
|
|
3
|
+
import { Button } from "@umituz/web-design-system/atoms";
|
|
4
|
+
|
|
5
|
+
export interface AccessDeniedProps {
|
|
6
|
+
title?: ReactNode;
|
|
7
|
+
description?: ReactNode;
|
|
8
|
+
actionLabel?: string;
|
|
9
|
+
onAction?: () => void;
|
|
10
|
+
icon?: ReactNode;
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const AccessDenied = ({
|
|
15
|
+
title,
|
|
16
|
+
description,
|
|
17
|
+
actionLabel,
|
|
18
|
+
onAction,
|
|
19
|
+
icon,
|
|
20
|
+
children,
|
|
21
|
+
}: AccessDeniedProps) => {
|
|
22
|
+
return (
|
|
23
|
+
<div className="flex min-h-[320px] w-full flex-col items-center justify-center rounded-2xl border border-line bg-surface p-8 text-center shadow-xs">
|
|
24
|
+
<div className="mb-4 flex h-14 w-14 items-center justify-center rounded-full bg-rose-500/10 text-rose-500">
|
|
25
|
+
{icon ?? <ShieldAlert className="h-7 w-7" aria-hidden="true" />}
|
|
26
|
+
</div>
|
|
27
|
+
{title && (
|
|
28
|
+
<h2 className="mb-2 font-display text-xl font-bold tracking-tight text-ink">{title}</h2>
|
|
29
|
+
)}
|
|
30
|
+
{description && (
|
|
31
|
+
<p className="max-w-md text-sm text-ink-2 leading-relaxed">{description}</p>
|
|
32
|
+
)}
|
|
33
|
+
{children && <div className="mt-4">{children}</div>}
|
|
34
|
+
{actionLabel && onAction && (
|
|
35
|
+
<div className="mt-6">
|
|
36
|
+
<Button variant="default" size="sm" onClick={onAction}>
|
|
37
|
+
{actionLabel}
|
|
38
|
+
</Button>
|
|
39
|
+
</div>
|
|
40
|
+
)}
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
};
|
|
@@ -4,7 +4,9 @@ import { MemoryRouter, Route, Routes } from "react-router";
|
|
|
4
4
|
import {
|
|
5
5
|
Can,
|
|
6
6
|
PermissionsProvider,
|
|
7
|
+
ProtectedRoute,
|
|
7
8
|
RequirePermission,
|
|
9
|
+
usePermission,
|
|
8
10
|
usePermissions,
|
|
9
11
|
} from "./PermissionsContext";
|
|
10
12
|
import type { PermissionMatrix } from "./types";
|
|
@@ -142,3 +144,50 @@ describe("Can", () => {
|
|
|
142
144
|
expect(screen.getByText("Orders")).not.toBeNull();
|
|
143
145
|
});
|
|
144
146
|
});
|
|
147
|
+
|
|
148
|
+
describe("usePermission", () => {
|
|
149
|
+
it("returns true when permission is granted and false otherwise", () => {
|
|
150
|
+
let allowed = false;
|
|
151
|
+
const Probe = () => {
|
|
152
|
+
allowed = usePermission("orders.edit");
|
|
153
|
+
return null;
|
|
154
|
+
};
|
|
155
|
+
mount("editor", <Probe />);
|
|
156
|
+
expect(allowed).toBe(true);
|
|
157
|
+
|
|
158
|
+
mount("viewer", <Probe />);
|
|
159
|
+
expect(allowed).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe("ProtectedRoute", () => {
|
|
164
|
+
it("renders children when authorized", () => {
|
|
165
|
+
mount("editor", (
|
|
166
|
+
<MemoryRouter>
|
|
167
|
+
<ProtectedRoute permission="orders.edit">
|
|
168
|
+
<span>Protected Area</span>
|
|
169
|
+
</ProtectedRoute>
|
|
170
|
+
</MemoryRouter>
|
|
171
|
+
));
|
|
172
|
+
expect(screen.getByText("Protected Area")).not.toBeNull();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("renders fallback AccessDenied with custom props when unauthorized without redirect", () => {
|
|
176
|
+
mount("viewer", (
|
|
177
|
+
<MemoryRouter>
|
|
178
|
+
<ProtectedRoute
|
|
179
|
+
permission="orders.edit"
|
|
180
|
+
accessDeniedProps={{
|
|
181
|
+
title: "Access Forbidden",
|
|
182
|
+
description: "You cannot view this area",
|
|
183
|
+
}}
|
|
184
|
+
>
|
|
185
|
+
<span>Protected Area</span>
|
|
186
|
+
</ProtectedRoute>
|
|
187
|
+
</MemoryRouter>
|
|
188
|
+
));
|
|
189
|
+
expect(screen.queryByText("Protected Area")).toBeNull();
|
|
190
|
+
expect(screen.getByText("Access Forbidden")).not.toBeNull();
|
|
191
|
+
expect(screen.getByText("You cannot view this area")).not.toBeNull();
|
|
192
|
+
});
|
|
193
|
+
});
|
|
@@ -2,6 +2,7 @@ import { createContext, useCallback, useContext, useMemo, type ReactNode } from
|
|
|
2
2
|
import { Navigate } from "react-router";
|
|
3
3
|
import { hasAnyPermission, hasPermission } from "./hasPermission";
|
|
4
4
|
import type { Permission, PermissionMatrix, RoleKey } from "./types";
|
|
5
|
+
import { AccessDenied, type AccessDeniedProps } from "./AccessDenied";
|
|
5
6
|
|
|
6
7
|
export interface PermissionsProviderProps {
|
|
7
8
|
matrix: PermissionMatrix;
|
|
@@ -106,3 +107,49 @@ export const Can = ({
|
|
|
106
107
|
const allowed = resolveAllowed(can, canAny, permission, anyOf);
|
|
107
108
|
return allowed ? <>{children}</> : <>{fallback}</>;
|
|
108
109
|
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Convenience hook that checks whether the current user has a specific permission.
|
|
113
|
+
*
|
|
114
|
+
* Example:
|
|
115
|
+
* ```tsx
|
|
116
|
+
* const canCreateProject = usePermission("projects.create");
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export const usePermission = (permission: Permission): boolean => {
|
|
120
|
+
const { can } = usePermissions();
|
|
121
|
+
return can(permission);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export interface ProtectedRouteProps {
|
|
125
|
+
permission?: Permission;
|
|
126
|
+
anyOf?: readonly Permission[];
|
|
127
|
+
children: ReactNode;
|
|
128
|
+
redirectTo?: string;
|
|
129
|
+
fallback?: ReactNode;
|
|
130
|
+
accessDeniedProps?: AccessDeniedProps;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Route-level permission guard. If the user lacks the permission:
|
|
135
|
+
* - Renders `fallback` if provided
|
|
136
|
+
* - Redirects to `redirectTo` if provided
|
|
137
|
+
* - Otherwise displays `<AccessDenied {...accessDeniedProps} />`
|
|
138
|
+
*/
|
|
139
|
+
export const ProtectedRoute = ({
|
|
140
|
+
permission,
|
|
141
|
+
anyOf,
|
|
142
|
+
children,
|
|
143
|
+
redirectTo,
|
|
144
|
+
fallback,
|
|
145
|
+
accessDeniedProps,
|
|
146
|
+
}: ProtectedRouteProps) => {
|
|
147
|
+
const { can, canAny } = usePermissions();
|
|
148
|
+
const allowed = resolveAllowed(can, canAny, permission, anyOf);
|
|
149
|
+
if (!allowed) {
|
|
150
|
+
if (fallback !== undefined) return <>{fallback}</>;
|
|
151
|
+
if (redirectTo !== undefined) return <Navigate to={redirectTo} replace />;
|
|
152
|
+
return <AccessDenied {...accessDeniedProps} />;
|
|
153
|
+
}
|
|
154
|
+
return <>{children}</>;
|
|
155
|
+
};
|
|
@@ -22,7 +22,9 @@ const grantIncludes = (grant: RoleGrant, permission: Permission): boolean => {
|
|
|
22
22
|
if (grant === PERMISSION_WILDCARD) return true;
|
|
23
23
|
return grant.some(
|
|
24
24
|
(entry) =>
|
|
25
|
-
entry ===
|
|
25
|
+
entry === PERMISSION_WILDCARD ||
|
|
26
|
+
entry === permission ||
|
|
27
|
+
matchesModuleWildcard(entry, permission),
|
|
26
28
|
);
|
|
27
29
|
};
|
|
28
30
|
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
export {
|
|
2
2
|
PermissionsProvider,
|
|
3
3
|
usePermissions,
|
|
4
|
+
usePermission,
|
|
4
5
|
RequirePermission,
|
|
6
|
+
ProtectedRoute,
|
|
5
7
|
Can,
|
|
6
8
|
type PermissionsProviderProps,
|
|
7
9
|
type RequirePermissionProps,
|
|
10
|
+
type ProtectedRouteProps,
|
|
8
11
|
type CanProps,
|
|
9
12
|
} from "./PermissionsContext";
|
|
13
|
+
export {
|
|
14
|
+
AccessDenied,
|
|
15
|
+
type AccessDeniedProps,
|
|
16
|
+
} from "./AccessDenied";
|
|
10
17
|
export {
|
|
11
18
|
hasPermission,
|
|
12
19
|
hasAnyPermission,
|
|
@@ -74,9 +74,9 @@ export function createProductTour(config: TourConfig): TourEngineInstance {
|
|
|
74
74
|
onStepChange,
|
|
75
75
|
} = config;
|
|
76
76
|
|
|
77
|
-
const nextBtnDefault = labels.next
|
|
78
|
-
const prevBtnDefault = labels.prev
|
|
79
|
-
const doneBtnDefault = labels.done
|
|
77
|
+
const nextBtnDefault = labels.next;
|
|
78
|
+
const prevBtnDefault = labels.prev;
|
|
79
|
+
const doneBtnDefault = labels.done;
|
|
80
80
|
const progressTextTemplate = labels.progress ?? "{{current}} / {{total}}";
|
|
81
81
|
|
|
82
82
|
let completedSuccessfully = false;
|