fix: (pbac) edit sheet readonly to all to readonly (#22672)

* fix readonly to all to readonly

* fix sheet not showing correct all,readonly,none state until refresh

* update test to include the correct roleId

---------

Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
sean-brydonandAlex van Andel authored and GitHub committed 2025-07-22 14:46:16 +00:00
1 parent dbaa0727ae
commit fa47456d5f
4 files changed
+110 -2

No files matched your search

@@ -0,0 +1,48 @@
import { describe, it, expect } from "vitest";
import { usePermissions } from "../usePermissions";
describe("usePermissions", () => {
const { getResourcePermissionLevel } = usePermissions();
describe("getResourcePermissionLevel", () => {
it("should return 'all' for any resource when *.* permission is present", () => {
const permissions = ["*.*", "eventType.create", "eventType.read"];
expect(getResourcePermissionLevel("eventType", permissions)).toBe("all");
expect(getResourcePermissionLevel("booking", permissions)).toBe("all");
expect(getResourcePermissionLevel("team", permissions)).toBe("all");
});
it("should return 'all' for resource with all individual permissions", () => {
const permissions = ["eventType.create", "eventType.read", "eventType.update", "eventType.delete"];
expect(getResourcePermissionLevel("eventType", permissions)).toBe("all");
});
it("should return 'read' for resource with only read permission", () => {
const permissions = ["eventType.read"];
expect(getResourcePermissionLevel("eventType", permissions)).toBe("read");
});
it("should return 'none' for resource with no permissions", () => {
const permissions = ["booking.create"];
expect(getResourcePermissionLevel("eventType", permissions)).toBe("none");
});
it("should handle * resource correctly", () => {
const permissionsWithAll = ["*.*"];
const permissionsWithoutAll = ["eventType.read"];
expect(getResourcePermissionLevel("*", permissionsWithAll)).toBe("all");
expect(getResourcePermissionLevel("*", permissionsWithoutAll)).toBe("none");
});
it("should prioritize *.* over individual permissions", () => {
const permissions = ["*.*", "eventType.read"]; // Has global all but only read for eventType individually
expect(getResourcePermissionLevel("eventType", permissions)).toBe("all");
});
});
});
@@ -46,6 +46,11 @@ export function usePermissions(): UsePermissionsReturn {
const resourceConfig = PERMISSION_REGISTRY[resource as keyof typeof PERMISSION_REGISTRY];
if (!resourceConfig) return "none";
// Check if global all permissions (*.*) is present
if (permissions.includes("*.*")) {
return "all";
}
// Filter out internal keys like _resource when checking permissions
const allResourcePerms = Object.keys(resourceConfig)
.filter((action) => !action.startsWith("_"))