From f5d6dd7d09d87ce37ee0a584225b4b9722bdd52a Mon Sep 17 00:00:00 2001 From: YANG QIA <2013xile@gmail.com> Date: Tue, 16 May 2023 20:33:05 +0800 Subject: [PATCH] fix(acl): issue with repeated createdById field (#1871) --- packages/core/acl/src/__tests__/acl.test.ts | 23 +++++++++++++++++++++ packages/core/acl/src/acl.ts | 6 +++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/core/acl/src/__tests__/acl.test.ts b/packages/core/acl/src/__tests__/acl.test.ts index 2c71a9307..3e5bae0d6 100644 --- a/packages/core/acl/src/__tests__/acl.test.ts +++ b/packages/core/acl/src/__tests__/acl.test.ts @@ -1,3 +1,4 @@ +import { Context } from '@nocobase/actions'; import { ACL } from '..'; describe('acl', () => { @@ -354,4 +355,26 @@ describe('acl', () => { }, }); }); + + it('should clone can result deeply', () => { + jest.spyOn(acl, 'can').mockReturnValue({ + role: 'root', + resource: 'Test', + action: 'test', + params: { + fields: [], + }, + }); + const newConext = () => ({ + state: {}, + action: {}, + throw: () => {}, + }); + const ctx1 = newConext() as Context; + const ctx2 = newConext() as Context; + acl.getActionParams(ctx1); + acl.getActionParams(ctx2); + ctx1.permission.can.params.fields.push('createdById'); + expect(ctx2.permission.can.params.fields).toEqual([]); + }); }); diff --git a/packages/core/acl/src/acl.ts b/packages/core/acl/src/acl.ts index c93ad663b..9d682cbe0 100644 --- a/packages/core/acl/src/acl.ts +++ b/packages/core/acl/src/acl.ts @@ -376,7 +376,11 @@ export class ACL extends EventEmitter { const { resourceName, actionName } = ctx.action; ctx.can = (options: Omit) => { - return this.can({ role: roleName, ...options }); + const can = this.can({ role: roleName, ...options }); + if (!can) { + return null; + } + return lodash.cloneDeep(can); }; ctx.permission = {