From 31dbe5a0ebfc6c8ba6149c2c25aae8924871f7b6 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Sat, 16 Jul 2022 12:04:58 +0800 Subject: [PATCH] fix: permission logic of super administrator Signed-off-by: Ryan Wang --- src/utils/__tests__/permission.spec.ts | 9 +++++++-- src/utils/permission.ts | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/utils/__tests__/permission.spec.ts b/src/utils/__tests__/permission.spec.ts index 6ba3e517..fcd6040d 100644 --- a/src/utils/__tests__/permission.spec.ts +++ b/src/utils/__tests__/permission.spec.ts @@ -4,8 +4,7 @@ import { hasPermission } from "../permission"; describe("hasPermission", () => { it("should return true if user has permission", () => { const uiPermissions = ["system:post:manage", "system:post:view"]; - expect(hasPermission(uiPermissions, ["*"], false)).toBe(true); - expect(hasPermission(uiPermissions, ["*"], true)).toBe(true); + expect(hasPermission(uiPermissions, ["system:post:manage"], false)).toBe( false ); @@ -27,5 +26,11 @@ describe("hasPermission", () => { false ) ).toBe(true); + + // super admin has all permissions + expect(hasPermission(["*"], ["system:post:manage"], false)).toBe(true); + expect( + hasPermission(["*"], ["system:post:manage", "system:links:manage"], true) + ).toBe(true); }); }); diff --git a/src/utils/permission.ts b/src/utils/permission.ts index a12ed43d..8183bbd1 100644 --- a/src/utils/permission.ts +++ b/src/utils/permission.ts @@ -5,12 +5,12 @@ export function hasPermission( targetPermissions: Array, any: boolean ): boolean { - if (!targetPermissions || !targetPermissions.length) { + // super admin has all permissions + if (uiPermissions.includes("*")) { return true; } - // super admin has all permissions - if (targetPermissions.includes("*")) { + if (!targetPermissions || !targetPermissions.length) { return true; }