@terreno/admin-frontend 57.1.0 → 57.2.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/dist/AdminModelForm.js +14 -7
- package/dist/AdminModelForm.js.map +1 -1
- package/dist/AdminModelList.js +3 -1
- package/dist/AdminModelList.js.map +1 -1
- package/dist/AdminModelTable.js +2 -1
- package/dist/AdminModelTable.js.map +1 -1
- package/dist/AdminRolesList.js +67 -6
- package/dist/AdminRolesList.js.map +1 -1
- package/dist/AdminScriptList.js +12 -10
- package/dist/AdminScriptList.js.map +1 -1
- package/dist/AdminShell.js +24 -11
- package/dist/AdminShell.js.map +1 -1
- package/dist/adminPageAccess.d.ts +5 -0
- package/dist/adminPageAccess.js +19 -0
- package/dist/adminPageAccess.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +13 -0
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/src/AdminModelForm.test.tsx +33 -0
- package/src/AdminModelForm.tsx +25 -9
- package/src/AdminModelList.tsx +3 -1
- package/src/AdminModelTable.tsx +3 -1
- package/src/AdminRolesList.test.tsx +84 -2
- package/src/AdminRolesList.tsx +147 -24
- package/src/AdminScriptList.tsx +42 -30
- package/src/AdminShell.test.tsx +89 -0
- package/src/AdminShell.tsx +31 -10
- package/src/adminPageAccess.test.ts +18 -0
- package/src/adminPageAccess.ts +18 -0
- package/src/index.tsx +1 -0
- package/src/types.ts +14 -0
package/src/AdminRolesList.tsx
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Badge,
|
|
3
|
+
Box,
|
|
4
|
+
Button,
|
|
5
|
+
CheckBox,
|
|
6
|
+
Heading,
|
|
7
|
+
Modal,
|
|
8
|
+
SelectField,
|
|
9
|
+
Spinner,
|
|
10
|
+
Text,
|
|
11
|
+
TextField,
|
|
12
|
+
} from "@terreno/ui";
|
|
2
13
|
import React, {useCallback, useMemo, useState} from "react";
|
|
3
14
|
|
|
4
15
|
import type {AdminScreenProps} from "./types";
|
|
@@ -24,6 +35,50 @@ const EMPTY_ROLE_FORM: RoleFormState = {
|
|
|
24
35
|
permissions: {},
|
|
25
36
|
};
|
|
26
37
|
|
|
38
|
+
const STANDARD_ACCESS_ACTIONS = ["read", "write", "writeOwned"] as const;
|
|
39
|
+
const STANDARD_ACCESS_OPTIONS = [
|
|
40
|
+
{label: "No access", value: "none"},
|
|
41
|
+
{label: "Read only", value: "read"},
|
|
42
|
+
{label: "Read + write owned", value: "writeOwned"},
|
|
43
|
+
{label: "Read + write all", value: "write"},
|
|
44
|
+
];
|
|
45
|
+
const ADMIN_PAGE_RESOURCE = "admin";
|
|
46
|
+
const ADMIN_PAGE_ACTION = "access";
|
|
47
|
+
|
|
48
|
+
const editorActionsForResource = ({
|
|
49
|
+
actions,
|
|
50
|
+
hasStandardAccess,
|
|
51
|
+
resource,
|
|
52
|
+
}: {
|
|
53
|
+
actions: readonly string[];
|
|
54
|
+
hasStandardAccess: boolean;
|
|
55
|
+
resource: string;
|
|
56
|
+
}): readonly string[] => {
|
|
57
|
+
const withoutStandard = hasStandardAccess
|
|
58
|
+
? actions.filter(
|
|
59
|
+
(action) =>
|
|
60
|
+
!STANDARD_ACCESS_ACTIONS.includes(action as (typeof STANDARD_ACCESS_ACTIONS)[number])
|
|
61
|
+
)
|
|
62
|
+
: actions;
|
|
63
|
+
if (resource === ADMIN_PAGE_RESOURCE) {
|
|
64
|
+
return withoutStandard.filter((action) => action !== ADMIN_PAGE_ACTION);
|
|
65
|
+
}
|
|
66
|
+
return withoutStandard;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const standardAccessValue = (actions: string[]): string => {
|
|
70
|
+
if (actions.includes("write")) {
|
|
71
|
+
return "write";
|
|
72
|
+
}
|
|
73
|
+
if (actions.includes("writeOwned")) {
|
|
74
|
+
return "writeOwned";
|
|
75
|
+
}
|
|
76
|
+
if (actions.includes("read")) {
|
|
77
|
+
return "read";
|
|
78
|
+
}
|
|
79
|
+
return "none";
|
|
80
|
+
};
|
|
81
|
+
|
|
27
82
|
const clonePermissions = (permissions?: Record<string, string[]>): Record<string, string[]> => {
|
|
28
83
|
return Object.fromEntries(
|
|
29
84
|
Object.entries(permissions ?? {}).map(([resource, actions]) => [resource, [...actions]])
|
|
@@ -45,6 +100,8 @@ export const AdminRolesList: React.FC<AdminScreenProps> = ({api, apiBase, baseUr
|
|
|
45
100
|
const roles = normalizeRoles(data);
|
|
46
101
|
const statements = normalizeStatements(statementsData);
|
|
47
102
|
const resources = useMemo(() => Object.keys(statements).sort(), [statements]);
|
|
103
|
+
const hasAdminPageStatement =
|
|
104
|
+
statements[ADMIN_PAGE_RESOURCE]?.includes(ADMIN_PAGE_ACTION) ?? false;
|
|
48
105
|
const [editingRole, setEditingRole] = useState<RbacRoleRow | null>(null);
|
|
49
106
|
const [form, setForm] = useState<RoleFormState>(EMPTY_ROLE_FORM);
|
|
50
107
|
const [isFormVisible, setIsFormVisible] = useState(false);
|
|
@@ -99,6 +156,24 @@ export const AdminRolesList: React.FC<AdminScreenProps> = ({api, apiBase, baseUr
|
|
|
99
156
|
});
|
|
100
157
|
}, []);
|
|
101
158
|
|
|
159
|
+
const handleStandardAccessChange = useCallback((resource: string, value: string): void => {
|
|
160
|
+
setForm((current) => {
|
|
161
|
+
const customActions = (current.permissions[resource] ?? []).filter(
|
|
162
|
+
(action) =>
|
|
163
|
+
!STANDARD_ACCESS_ACTIONS.includes(action as (typeof STANDARD_ACCESS_ACTIONS)[number])
|
|
164
|
+
);
|
|
165
|
+
const standardActions = value === "none" ? [] : value === "read" ? ["read"] : ["read", value];
|
|
166
|
+
const nextActions = [...customActions, ...standardActions];
|
|
167
|
+
const permissions = {...current.permissions};
|
|
168
|
+
if (nextActions.length === 0) {
|
|
169
|
+
Reflect.deleteProperty(permissions, resource);
|
|
170
|
+
} else {
|
|
171
|
+
permissions[resource] = nextActions;
|
|
172
|
+
}
|
|
173
|
+
return {...current, permissions};
|
|
174
|
+
});
|
|
175
|
+
}, []);
|
|
176
|
+
|
|
102
177
|
const handleSave = useCallback(async (): Promise<void> => {
|
|
103
178
|
const name = form.name.trim();
|
|
104
179
|
const displayName = form.displayName.trim();
|
|
@@ -277,31 +352,79 @@ export const AdminRolesList: React.FC<AdminScreenProps> = ({api, apiBase, baseUr
|
|
|
277
352
|
/>
|
|
278
353
|
<Box gap={2} testID="admin-role-permissions">
|
|
279
354
|
<Heading size="sm">Permissions</Heading>
|
|
280
|
-
{
|
|
281
|
-
<Box gap={
|
|
282
|
-
<
|
|
283
|
-
<
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
355
|
+
{hasAdminPageStatement ? (
|
|
356
|
+
<Box gap={2} testID="admin-role-page-access">
|
|
357
|
+
<Heading size="sm">Admin page</Heading>
|
|
358
|
+
<Text>
|
|
359
|
+
This is the only permission that opens the admin panel. Model and tool
|
|
360
|
+
permissions do not apply until it is granted.
|
|
361
|
+
</Text>
|
|
362
|
+
<Box
|
|
363
|
+
accessibilityHint="Toggles whether this role can open the admin page"
|
|
364
|
+
accessibilityLabel="Allow access to the admin page"
|
|
365
|
+
alignItems="center"
|
|
366
|
+
direction="row"
|
|
367
|
+
gap={1}
|
|
368
|
+
onClick={() => handlePermissionToggle(ADMIN_PAGE_RESOURCE, ADMIN_PAGE_ACTION)}
|
|
369
|
+
testID={`admin-role-permission-${ADMIN_PAGE_RESOURCE}-${ADMIN_PAGE_ACTION}`}
|
|
370
|
+
>
|
|
371
|
+
<CheckBox
|
|
372
|
+
selected={
|
|
373
|
+
form.permissions[ADMIN_PAGE_RESOURCE]?.includes(ADMIN_PAGE_ACTION) ?? false
|
|
374
|
+
}
|
|
375
|
+
/>
|
|
376
|
+
<Text>Allow access to the admin page</Text>
|
|
302
377
|
</Box>
|
|
303
378
|
</Box>
|
|
304
|
-
)
|
|
379
|
+
) : null}
|
|
380
|
+
{resources.map((resource) => {
|
|
381
|
+
const actions = statements[resource];
|
|
382
|
+
const hasStandardAccess = STANDARD_ACCESS_ACTIONS.every((action) =>
|
|
383
|
+
actions.includes(action)
|
|
384
|
+
);
|
|
385
|
+
const customActions = editorActionsForResource({
|
|
386
|
+
actions,
|
|
387
|
+
hasStandardAccess,
|
|
388
|
+
resource,
|
|
389
|
+
});
|
|
390
|
+
if (!hasStandardAccess && customActions.length === 0) {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
return (
|
|
394
|
+
<Box gap={1} key={resource}>
|
|
395
|
+
<Text bold>{resource}</Text>
|
|
396
|
+
{hasStandardAccess ? (
|
|
397
|
+
<SelectField
|
|
398
|
+
onChange={(value: string) => handleStandardAccessChange(resource, value)}
|
|
399
|
+
options={STANDARD_ACCESS_OPTIONS}
|
|
400
|
+
testID={`admin-role-access-${resource}`}
|
|
401
|
+
title="Access level"
|
|
402
|
+
value={standardAccessValue(form.permissions[resource] ?? [])}
|
|
403
|
+
/>
|
|
404
|
+
) : null}
|
|
405
|
+
<Box direction="row" gap={3} wrap>
|
|
406
|
+
{customActions.map((action) => {
|
|
407
|
+
const isSelected = form.permissions[resource]?.includes(action) ?? false;
|
|
408
|
+
return (
|
|
409
|
+
<Box
|
|
410
|
+
accessibilityHint={`Toggles the ${action} permission for ${resource}`}
|
|
411
|
+
accessibilityLabel={`${resource} ${action}`}
|
|
412
|
+
alignItems="center"
|
|
413
|
+
direction="row"
|
|
414
|
+
gap={1}
|
|
415
|
+
key={`${resource}:${action}`}
|
|
416
|
+
onClick={() => handlePermissionToggle(resource, action)}
|
|
417
|
+
testID={`admin-role-permission-${resource}-${action}`}
|
|
418
|
+
>
|
|
419
|
+
<CheckBox selected={isSelected} />
|
|
420
|
+
<Text>{action}</Text>
|
|
421
|
+
</Box>
|
|
422
|
+
);
|
|
423
|
+
})}
|
|
424
|
+
</Box>
|
|
425
|
+
</Box>
|
|
426
|
+
);
|
|
427
|
+
})}
|
|
305
428
|
</Box>
|
|
306
429
|
</Box>
|
|
307
430
|
</Box>
|
package/src/AdminScriptList.tsx
CHANGED
|
@@ -60,6 +60,8 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
|
|
|
60
60
|
});
|
|
61
61
|
const {config, isLoading, error} = useAdminConfig(api, resolvedApiBase);
|
|
62
62
|
const {useListScriptRunsQuery} = useAdminScripts(api, resolvedApiBase);
|
|
63
|
+
const canRunScripts = isAdmin && (config?.platformTools?.runScripts ?? true);
|
|
64
|
+
const canViewScripts = config?.platformTools?.viewScripts ?? true;
|
|
63
65
|
|
|
64
66
|
const [tab, setTab] = useState<Tab>("scripts");
|
|
65
67
|
const [historyPage, setHistoryPage] = useState(1);
|
|
@@ -72,16 +74,22 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
|
|
|
72
74
|
const [modalVisible, setModalVisible] = useState(false);
|
|
73
75
|
|
|
74
76
|
// Unfiltered most-recent runs power the per-script "Last run" badges on the Scripts tab.
|
|
75
|
-
const {data: latestRunsData} = useListScriptRunsQuery(
|
|
77
|
+
const {data: latestRunsData} = useListScriptRunsQuery(
|
|
78
|
+
{limit: HISTORY_PAGE_SIZE, page: 1},
|
|
79
|
+
{skip: !canViewScripts}
|
|
80
|
+
);
|
|
76
81
|
const latestRuns: ScriptRun[] = useMemo(() => latestRunsData?.data ?? [], [latestRunsData]);
|
|
77
82
|
const lastRuns = useMemo(() => latestRunByScript(latestRuns), [latestRuns]);
|
|
78
83
|
|
|
79
84
|
// Paginated history, optionally scoped to a single script via the per-script History link.
|
|
80
|
-
const {data: historyData} = useListScriptRunsQuery(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
+
const {data: historyData} = useListScriptRunsQuery(
|
|
86
|
+
{
|
|
87
|
+
limit: HISTORY_PAGE_SIZE,
|
|
88
|
+
name: historyScriptName ?? undefined,
|
|
89
|
+
page: historyPage,
|
|
90
|
+
},
|
|
91
|
+
{skip: !canViewScripts}
|
|
92
|
+
);
|
|
85
93
|
|
|
86
94
|
// Accumulate each loaded history page (de-duplicated by id) so "Load more" advances through
|
|
87
95
|
// pages instead of re-requesting a server-capped first page. Keying on the active filter lets
|
|
@@ -208,16 +216,18 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
|
|
|
208
216
|
Last run {relativeTime(run.created)} — {run.isDryRun ? "dry run" : "live run"}{" "}
|
|
209
217
|
{meta.label.toLowerCase()}
|
|
210
218
|
</Text>
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
219
|
+
{canViewScripts ? (
|
|
220
|
+
<Box
|
|
221
|
+
accessibilityHint={`Opens run history for ${script.name}`}
|
|
222
|
+
accessibilityLabel={`View run history for ${script.name}`}
|
|
223
|
+
onClick={() => handleViewHistory(script.name)}
|
|
224
|
+
testID={`admin-script-history-${script.name}`}
|
|
225
|
+
>
|
|
226
|
+
<Text color="link" size="sm">
|
|
227
|
+
History
|
|
228
|
+
</Text>
|
|
229
|
+
</Box>
|
|
230
|
+
) : null}
|
|
221
231
|
</Box>
|
|
222
232
|
);
|
|
223
233
|
};
|
|
@@ -235,12 +245,12 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
|
|
|
235
245
|
{renderLastRun(script)}
|
|
236
246
|
</Box>
|
|
237
247
|
<Button
|
|
238
|
-
disabled={!
|
|
248
|
+
disabled={!canRunScripts}
|
|
239
249
|
iconName="play"
|
|
240
250
|
onClick={() => handleRunScript(script)}
|
|
241
251
|
testID={`admin-script-run-${script.name}`}
|
|
242
252
|
text="Run"
|
|
243
|
-
tooltipText={!
|
|
253
|
+
tooltipText={!canRunScripts ? "You do not have permission to run scripts" : undefined}
|
|
244
254
|
variant="primary"
|
|
245
255
|
/>
|
|
246
256
|
</Box>
|
|
@@ -360,18 +370,20 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
|
|
|
360
370
|
|
|
361
371
|
return (
|
|
362
372
|
<Page color="transparent" maxWidth="100%" padding={0} scroll title="Scripts">
|
|
363
|
-
|
|
364
|
-
<
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
373
|
+
{canViewScripts ? (
|
|
374
|
+
<Box paddingX={4} paddingY={4}>
|
|
375
|
+
<SegmentedControl
|
|
376
|
+
items={[
|
|
377
|
+
"Scripts",
|
|
378
|
+
(latestRunsData?.total ?? latestRuns.length) > 0
|
|
379
|
+
? `Run history (${latestRunsData?.total ?? latestRuns.length})`
|
|
380
|
+
: "Run history",
|
|
381
|
+
]}
|
|
382
|
+
onChange={handleTabChange}
|
|
383
|
+
selectedIndex={tab === "history" ? 1 : 0}
|
|
384
|
+
/>
|
|
385
|
+
</Box>
|
|
386
|
+
) : null}
|
|
375
387
|
|
|
376
388
|
{tab === "scripts" ? renderScripts() : renderHistory()}
|
|
377
389
|
|
package/src/AdminShell.test.tsx
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
4
4
|
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
5
5
|
import {act, fireEvent} from "@testing-library/react-native";
|
|
6
|
+
import {assert} from "chai";
|
|
6
7
|
import React from "react";
|
|
7
8
|
import type {ScaledSize} from "react-native";
|
|
8
9
|
import {useWindowDimensions} from "react-native";
|
|
@@ -126,6 +127,23 @@ describe("AdminShell", () => {
|
|
|
126
127
|
expect(queryByTestId("admin-shell-mobile-header")).toBeNull();
|
|
127
128
|
});
|
|
128
129
|
|
|
130
|
+
it("shows a forbidden state when admin config returns 403", () => {
|
|
131
|
+
configState.config = null;
|
|
132
|
+
configState.error = {status: 403} as unknown as Error;
|
|
133
|
+
restoreWindowWidth?.();
|
|
134
|
+
restoreWindowWidth = setWindowWidth(1024);
|
|
135
|
+
|
|
136
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
137
|
+
<AdminShell api={mockApi} apiBase="/admin" routeBase="/admin">
|
|
138
|
+
<React.Fragment />
|
|
139
|
+
</AdminShell>
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(getByTestId("admin-shell-forbidden")).toBeTruthy();
|
|
143
|
+
expect(queryByTestId("admin-shell-error")).toBeNull();
|
|
144
|
+
expect(queryByTestId("admin-shell-sidebar")).toBeNull();
|
|
145
|
+
});
|
|
146
|
+
|
|
129
147
|
it("hides the fixed sidebar and shows a hamburger header below 768px", () => {
|
|
130
148
|
const {getByTestId, getByLabelText, queryByTestId, UNSAFE_root} = renderWithTheme(
|
|
131
149
|
<AdminShell api={mockApi} apiBase="/admin" routeBase="/admin">
|
|
@@ -263,6 +281,77 @@ describe("AdminShell", () => {
|
|
|
263
281
|
expect(mockRouterPush).toHaveBeenLastCalledWith("/admin/AdminAuditLog");
|
|
264
282
|
});
|
|
265
283
|
|
|
284
|
+
it("hides built-in platform tools denied by backend RBAC metadata", () => {
|
|
285
|
+
restoreWindowWidth?.();
|
|
286
|
+
restoreWindowWidth = setWindowWidth(1024);
|
|
287
|
+
configState.config = {
|
|
288
|
+
...buildConfig(),
|
|
289
|
+
platformTools: {
|
|
290
|
+
configuration: false,
|
|
291
|
+
roles: false,
|
|
292
|
+
scripts: false,
|
|
293
|
+
version: false,
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const {queryByTestId} = renderWithTheme(
|
|
298
|
+
<AdminShell
|
|
299
|
+
api={mockApi}
|
|
300
|
+
apiBase="/admin"
|
|
301
|
+
configurationPath="/admin/configuration"
|
|
302
|
+
customScreens={[{displayName: "Denied local screen", name: "denied"}]}
|
|
303
|
+
rolesPath="/roles"
|
|
304
|
+
routeBase="/admin"
|
|
305
|
+
>
|
|
306
|
+
<React.Fragment />
|
|
307
|
+
</AdminShell>
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
assert.isNull(queryByTestId("admin-shell-nav-scripts"));
|
|
311
|
+
assert.isNull(queryByTestId("admin-shell-nav-roles"));
|
|
312
|
+
assert.isNull(queryByTestId("admin-shell-nav-version"));
|
|
313
|
+
assert.isNull(queryByTestId("admin-shell-nav-configuration"));
|
|
314
|
+
assert.isNull(queryByTestId("admin-shell-nav-screen-denied"));
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("shows only the platform tools and models granted to the current role", () => {
|
|
318
|
+
restoreWindowWidth?.();
|
|
319
|
+
restoreWindowWidth = setWindowWidth(1024);
|
|
320
|
+
configState.config = {
|
|
321
|
+
...buildConfig(),
|
|
322
|
+
models: [
|
|
323
|
+
{
|
|
324
|
+
...buildConfig().models[0],
|
|
325
|
+
permissions: {create: false, delete: false, update: false},
|
|
326
|
+
},
|
|
327
|
+
],
|
|
328
|
+
platformTools: {
|
|
329
|
+
configuration: false,
|
|
330
|
+
roles: true,
|
|
331
|
+
scripts: false,
|
|
332
|
+
version: false,
|
|
333
|
+
},
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
337
|
+
<AdminShell
|
|
338
|
+
api={mockApi}
|
|
339
|
+
apiBase="/admin"
|
|
340
|
+
configurationPath="/admin/configuration"
|
|
341
|
+
rolesPath="/roles"
|
|
342
|
+
routeBase="/admin"
|
|
343
|
+
>
|
|
344
|
+
<React.Fragment />
|
|
345
|
+
</AdminShell>
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
assert.isNotNull(getByTestId("admin-shell-nav-model-Todo-clickable"));
|
|
349
|
+
assert.isNotNull(getByTestId("admin-shell-nav-roles-clickable"));
|
|
350
|
+
assert.isNull(queryByTestId("admin-shell-nav-scripts"));
|
|
351
|
+
assert.isNull(queryByTestId("admin-shell-nav-version"));
|
|
352
|
+
assert.isNull(queryByTestId("admin-shell-nav-configuration"));
|
|
353
|
+
});
|
|
354
|
+
|
|
266
355
|
it("hides empty Models and Screens headings", () => {
|
|
267
356
|
restoreWindowWidth?.();
|
|
268
357
|
restoreWindowWidth = setWindowWidth(1024);
|
package/src/AdminShell.tsx
CHANGED
|
@@ -5,8 +5,9 @@ import React, {useCallback, useEffect, useState} from "react";
|
|
|
5
5
|
import {useWindowDimensions} from "react-native";
|
|
6
6
|
import {SafeAreaView} from "react-native-safe-area-context";
|
|
7
7
|
import {type AdminBreadcrumbSegment, AdminBreadcrumbs} from "./AdminBreadcrumbs";
|
|
8
|
+
import {isAdminPageForbiddenError} from "./adminPageAccess";
|
|
8
9
|
import {groupAdminModelsByGroup} from "./adminShellNav";
|
|
9
|
-
import type {AdminApi, AdminCustomScreen, AdminModelConfig} from "./types";
|
|
10
|
+
import type {AdminApi, AdminConfigResponse, AdminCustomScreen, AdminModelConfig} from "./types";
|
|
10
11
|
import {resolveAdminBases} from "./types";
|
|
11
12
|
import {useAdminConfig} from "./useAdminConfig";
|
|
12
13
|
|
|
@@ -76,6 +77,7 @@ interface AdminShellSidebarNavProps {
|
|
|
76
77
|
grouped: ReturnType<typeof groupAdminModelsByGroup>;
|
|
77
78
|
navigate: (path: string) => void;
|
|
78
79
|
onNavigate?: () => void;
|
|
80
|
+
platformTools: NonNullable<AdminConfigResponse["platformTools"]>;
|
|
79
81
|
rolesPath?: string;
|
|
80
82
|
scripts: {name: string}[];
|
|
81
83
|
sidebarVariant: AdminShellSidebarVariant;
|
|
@@ -89,6 +91,7 @@ const AdminShellSidebarNav: React.FC<AdminShellSidebarNavProps> = ({
|
|
|
89
91
|
grouped,
|
|
90
92
|
navigate,
|
|
91
93
|
onNavigate,
|
|
94
|
+
platformTools,
|
|
92
95
|
rolesPath,
|
|
93
96
|
scripts,
|
|
94
97
|
sidebarVariant,
|
|
@@ -106,12 +109,12 @@ const AdminShellSidebarNav: React.FC<AdminShellSidebarNavProps> = ({
|
|
|
106
109
|
}))
|
|
107
110
|
.filter(({models: groupModels}) => groupModels.length > 0);
|
|
108
111
|
const hasPlatformLinks = Boolean(
|
|
109
|
-
scripts.length > 0 ||
|
|
110
|
-
rolesPath ||
|
|
111
|
-
versionConfigPath ||
|
|
112
|
+
(platformTools.scripts && scripts.length > 0) ||
|
|
113
|
+
(platformTools.roles && rolesPath) ||
|
|
114
|
+
(platformTools.version && versionConfigPath) ||
|
|
112
115
|
auditLogModel ||
|
|
113
116
|
featureFlagModel ||
|
|
114
|
-
configurationPath
|
|
117
|
+
(platformTools.configuration && configurationPath)
|
|
115
118
|
);
|
|
116
119
|
|
|
117
120
|
const runNav = useCallback(
|
|
@@ -190,7 +193,7 @@ const AdminShellSidebarNav: React.FC<AdminShellSidebarNavProps> = ({
|
|
|
190
193
|
<Text bold color={sectionLabelColor} size="sm">
|
|
191
194
|
Platform
|
|
192
195
|
</Text>
|
|
193
|
-
{scripts.length > 0 ? (
|
|
196
|
+
{platformTools.scripts && scripts.length > 0 ? (
|
|
194
197
|
<NavButton
|
|
195
198
|
label="Scripts"
|
|
196
199
|
onPress={() => {
|
|
@@ -202,7 +205,7 @@ const AdminShellSidebarNav: React.FC<AdminShellSidebarNavProps> = ({
|
|
|
202
205
|
testID="admin-shell-nav-scripts"
|
|
203
206
|
/>
|
|
204
207
|
) : null}
|
|
205
|
-
{rolesPath ? (
|
|
208
|
+
{platformTools.roles && rolesPath ? (
|
|
206
209
|
<NavButton
|
|
207
210
|
label="Roles"
|
|
208
211
|
onPress={() => {
|
|
@@ -214,7 +217,7 @@ const AdminShellSidebarNav: React.FC<AdminShellSidebarNavProps> = ({
|
|
|
214
217
|
testID="admin-shell-nav-roles"
|
|
215
218
|
/>
|
|
216
219
|
) : null}
|
|
217
|
-
{versionConfigPath ? (
|
|
220
|
+
{platformTools.version && versionConfigPath ? (
|
|
218
221
|
<NavButton
|
|
219
222
|
label="Version"
|
|
220
223
|
onPress={() => {
|
|
@@ -250,7 +253,7 @@ const AdminShellSidebarNav: React.FC<AdminShellSidebarNavProps> = ({
|
|
|
250
253
|
testID="admin-shell-nav-feature-flags"
|
|
251
254
|
/>
|
|
252
255
|
) : null}
|
|
253
|
-
{configurationPath ? (
|
|
256
|
+
{platformTools.configuration && configurationPath ? (
|
|
254
257
|
<NavButton
|
|
255
258
|
label="Configuration"
|
|
256
259
|
onPress={() => {
|
|
@@ -351,6 +354,15 @@ export const AdminShell: React.FC<AdminShellProps> = ({
|
|
|
351
354
|
}
|
|
352
355
|
|
|
353
356
|
if (error || !config) {
|
|
357
|
+
if (isAdminPageForbiddenError(error)) {
|
|
358
|
+
return (
|
|
359
|
+
<Box padding={4} testID="admin-shell-forbidden">
|
|
360
|
+
<Text color="error">
|
|
361
|
+
You do not have permission to open the admin page. Grant the admin:access permission.
|
|
362
|
+
</Text>
|
|
363
|
+
</Box>
|
|
364
|
+
);
|
|
365
|
+
}
|
|
354
366
|
return (
|
|
355
367
|
<Box padding={4} testID="admin-shell-error">
|
|
356
368
|
<Text color="error">Failed to load admin configuration.</Text>
|
|
@@ -359,8 +371,16 @@ export const AdminShell: React.FC<AdminShellProps> = ({
|
|
|
359
371
|
}
|
|
360
372
|
|
|
361
373
|
const backendScreens = config.customScreens ?? [];
|
|
362
|
-
const allCustomScreens =
|
|
374
|
+
const allCustomScreens = config.platformTools
|
|
375
|
+
? backendScreens
|
|
376
|
+
: [...backendScreens, ...(propCustomScreens ?? [])];
|
|
363
377
|
const scripts = config.scripts ?? [];
|
|
378
|
+
const platformTools = config.platformTools ?? {
|
|
379
|
+
configuration: true,
|
|
380
|
+
roles: true,
|
|
381
|
+
scripts: true,
|
|
382
|
+
version: true,
|
|
383
|
+
};
|
|
364
384
|
const grouped = groupAdminModelsByGroup(config.models as AdminModelConfig[]);
|
|
365
385
|
|
|
366
386
|
const sidebarIsColorful = sidebarVariant === "colorful";
|
|
@@ -372,6 +392,7 @@ export const AdminShell: React.FC<AdminShellProps> = ({
|
|
|
372
392
|
footer,
|
|
373
393
|
grouped,
|
|
374
394
|
navigate,
|
|
395
|
+
platformTools,
|
|
375
396
|
rolesPath,
|
|
376
397
|
scripts,
|
|
377
398
|
sidebarVariant,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
|
|
4
|
+
import {isAdminPageForbiddenError} from "./adminPageAccess";
|
|
5
|
+
|
|
6
|
+
describe("isAdminPageForbiddenError", () => {
|
|
7
|
+
it("detects RTK 403 errors", () => {
|
|
8
|
+
assert.isTrue(isAdminPageForbiddenError({status: 403}));
|
|
9
|
+
assert.isTrue(isAdminPageForbiddenError({originalStatus: 403, status: "FETCH_ERROR"}));
|
|
10
|
+
assert.isTrue(isAdminPageForbiddenError({status: {status: 403}}));
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("ignores other failures", () => {
|
|
14
|
+
assert.isFalse(isAdminPageForbiddenError(undefined));
|
|
15
|
+
assert.isFalse(isAdminPageForbiddenError({status: 500}));
|
|
16
|
+
assert.isFalse(isAdminPageForbiddenError(new Error("Failed to load")));
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect a 403 from GET /admin/config. That is the single admin-page gate:
|
|
3
|
+
* `admin:access` when RBAC is on, otherwise `user.admin`.
|
|
4
|
+
*/
|
|
5
|
+
export const isAdminPageForbiddenError = (error: unknown): boolean => {
|
|
6
|
+
if (!error || typeof error !== "object") {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const withStatus = error as {originalStatus?: unknown; status?: unknown};
|
|
10
|
+
if (withStatus.status === 403 || withStatus.originalStatus === 403) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
const nested = withStatus.status;
|
|
14
|
+
if (nested && typeof nested === "object" && "status" in nested) {
|
|
15
|
+
return (nested as {status?: unknown}).status === 403;
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
};
|
package/src/index.tsx
CHANGED
|
@@ -28,6 +28,7 @@ export {AdminScriptRunModal} from "./AdminScriptRunModal";
|
|
|
28
28
|
export {AdminShell, type AdminShellProps, type AdminShellSidebarVariant} from "./AdminShell";
|
|
29
29
|
export {AdminShellLayout, type AdminShellLayoutProps} from "./AdminShellLayout";
|
|
30
30
|
export {AdminVersionConfig} from "./AdminVersionConfig";
|
|
31
|
+
export {isAdminPageForbiddenError} from "./adminPageAccess";
|
|
31
32
|
export {groupAdminModelsByGroup} from "./adminShellNav";
|
|
32
33
|
export {CheckboxListEditor} from "./CheckboxListEditor";
|
|
33
34
|
export {ConfigurationScreen} from "./ConfigurationScreen";
|
package/src/types.ts
CHANGED
|
@@ -29,6 +29,11 @@ export type AdminApi = Api<
|
|
|
29
29
|
*/
|
|
30
30
|
export type AdminFieldValue = unknown;
|
|
31
31
|
|
|
32
|
+
export interface AdminRecordCapabilities {
|
|
33
|
+
delete: boolean;
|
|
34
|
+
update: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
32
37
|
/**
|
|
33
38
|
* RTK Query's `build` argument from `api.injectEndpoints({ endpoints: (build) => ... })`.
|
|
34
39
|
*
|
|
@@ -151,6 +156,15 @@ export interface AdminConfigResponse {
|
|
|
151
156
|
customScreens?: AdminCustomScreen[];
|
|
152
157
|
home?: AdminHome;
|
|
153
158
|
models: AdminModelConfig[];
|
|
159
|
+
/** Server-authorized visibility for built-in Platform sidebar tools. */
|
|
160
|
+
platformTools?: {
|
|
161
|
+
configuration: boolean;
|
|
162
|
+
roles: boolean;
|
|
163
|
+
runScripts?: boolean;
|
|
164
|
+
scripts: boolean;
|
|
165
|
+
version: boolean;
|
|
166
|
+
viewScripts?: boolean;
|
|
167
|
+
};
|
|
154
168
|
schemaVersion?: number;
|
|
155
169
|
scripts: AdminScriptConfig[];
|
|
156
170
|
/** Plugin home widget ids merged from admin contributions (informational). */
|