feat: recalculate permission scope and add permission matrix UI
- Gate app list procedures (getPaginated/all/search) behind app-modify-all - Filter integration list procedures by per-integration access - Bundle create permissions with their modify/full parents on group save - Align manage nav, home stat cards and page gates with the new scope - Add permission matrix UI with presets and effective-permission preview - Show scoped empty states and delegated-admin notices on the integrations page
This commit is contained in:
23 files changed
+982
-275
No files matched your search
@@ -117,3 +117,44 @@ export const groupPermissionKeys = objectKeys(groupPermissions).reduce((acc, key
|
||||
}
|
||||
return acc;
|
||||
}, [] as GroupPermissionKey[]);
|
||||
|
||||
/**
|
||||
* Ordered access levels per category used by the group permission matrix UI.
|
||||
* Each level entry is a full {@link GroupPermissionKey}; higher levels imply the lower ones
|
||||
* through {@link groupPermissionParents}.
|
||||
*/
|
||||
export const permissionMatrix = {
|
||||
board: { levels: ["board-view-all", "board-modify-all", "board-full-all"] },
|
||||
app: { levels: ["app-use-all", "app-modify-all", "app-full-all"] },
|
||||
integration: { levels: ["integration-use-all", "integration-interact-all", "integration-full-all"] },
|
||||
"search-engine": { levels: ["search-engine-modify-all", "search-engine-full-all"] },
|
||||
media: { levels: ["media-view-all", "media-full-all"] },
|
||||
other: { levels: ["other-view-logs"] },
|
||||
admin: { levels: ["admin"] },
|
||||
} as const satisfies Record<string, { levels: readonly GroupPermissionKey[] }>;
|
||||
|
||||
export type PermissionMatrixCategory = keyof typeof permissionMatrix;
|
||||
|
||||
export type PermissionMatrixState = Record<PermissionMatrixCategory, { level: number }>;
|
||||
|
||||
export const permissionsToMatrixState = (permissions: GroupPermissionKey[]): PermissionMatrixState => {
|
||||
const permissionSet = new Set(permissions);
|
||||
|
||||
return objectKeys(permissionMatrix).reduce((acc, category) => {
|
||||
let level = 0;
|
||||
permissionMatrix[category].levels.forEach((key, index) => {
|
||||
if (permissionSet.has(key)) {
|
||||
level = index + 1;
|
||||
}
|
||||
});
|
||||
acc[category] = { level };
|
||||
return acc;
|
||||
}, {} as PermissionMatrixState);
|
||||
};
|
||||
|
||||
export const matrixStateToPermissions = (state: PermissionMatrixState): GroupPermissionKey[] => {
|
||||
return objectKeys(permissionMatrix).reduce((acc, category) => {
|
||||
acc.push(...permissionMatrix[category].levels.slice(0, state[category].level));
|
||||
return acc;
|
||||
}, [] as GroupPermissionKey[]);
|
||||
};
|
||||
@@ -1,7 +1,12 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import type { GroupPermissionKey } from "../permissions";
|
||||
import { getPermissionsWithChildren, getPermissionsWithParents } from "../permissions";
|
||||
import {
|
||||
getPermissionsWithChildren,
|
||||
getPermissionsWithParents,
|
||||
matrixStateToPermissions,
|
||||
permissionsToMatrixState,
|
||||
} from "../permissions";
|
||||
|
||||
describe("getPermissionsWithParents should return the correct permissions", () => {
|
||||
test.each([
|
||||
@@ -45,3 +50,70 @@ describe("getPermissionsWithChildren should return the correct permissions", ()
|
||||
expect(getPermissionsWithChildren(input)).toEqual(expect.arrayContaining(expectedOutput));
|
||||
});
|
||||
});
|
||||
|
||||
describe("permissionsToMatrixState should resolve the highest present level per category", () => {
|
||||
test("resolves the highest present level key", () => {
|
||||
const state = permissionsToMatrixState(["board-view-all", "board-modify-all"]);
|
||||
expect(state.board.level).toBe(2);
|
||||
});
|
||||
|
||||
test("resolves the admin category", () => {
|
||||
expect(permissionsToMatrixState(["admin"]).admin.level).toBe(1);
|
||||
});
|
||||
|
||||
test("resolves an empty selection to all zero", () => {
|
||||
const state = permissionsToMatrixState([]);
|
||||
expect(Object.values(state).every(({ level }) => level === 0)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("matrixStateToPermissions should expand levels into permission keys", () => {
|
||||
test("expands a level into the ordered level keys", () => {
|
||||
const permissions = matrixStateToPermissions({
|
||||
board: { level: 2 },
|
||||
app: { level: 0 },
|
||||
integration: { level: 0 },
|
||||
"search-engine": { level: 0 },
|
||||
media: { level: 0 },
|
||||
other: { level: 0 },
|
||||
admin: { level: 0 },
|
||||
});
|
||||
expect(permissions).toEqual(["board-view-all", "board-modify-all"]);
|
||||
});
|
||||
|
||||
test("expands the admin category into the admin key", () => {
|
||||
const permissions = matrixStateToPermissions({
|
||||
board: { level: 0 },
|
||||
app: { level: 0 },
|
||||
integration: { level: 0 },
|
||||
"search-engine": { level: 0 },
|
||||
media: { level: 0 },
|
||||
other: { level: 0 },
|
||||
admin: { level: 1 },
|
||||
});
|
||||
expect(permissions).toEqual(["admin"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("matrix helpers should round-trip stably", () => {
|
||||
test.each([
|
||||
[["board-view-all", "board-modify-all"]],
|
||||
[["integration-use-all", "integration-interact-all", "integration-full-all"]],
|
||||
[["admin"]],
|
||||
[[]],
|
||||
[
|
||||
[
|
||||
"app-use-all",
|
||||
"app-modify-all",
|
||||
"search-engine-modify-all",
|
||||
"search-engine-full-all",
|
||||
"media-view-all",
|
||||
"other-view-logs",
|
||||
],
|
||||
],
|
||||
] satisfies [GroupPermissionKey[]][])("expect %s to round-trip", (permissions) => {
|
||||
const roundTripped = matrixStateToPermissions(permissionsToMatrixState(permissions));
|
||||
expect(roundTripped).toEqual(expect.arrayContaining(permissions));
|
||||
expect(roundTripped).toHaveLength(permissions.length);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user