fix(acl): issue with repeated createdById field (#1871)

This commit is contained in:
YANG QIA 2023-05-16 20:33:05 +08:00 committed by GitHub
parent fbbd90ca3c
commit f5d6dd7d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -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([]);
});
});

View File

@ -376,7 +376,11 @@ export class ACL extends EventEmitter {
const { resourceName, actionName } = ctx.action;
ctx.can = (options: Omit<CanArgs, 'role'>) => {
return this.can({ role: roleName, ...options });
const can = this.can({ role: roleName, ...options });
if (!can) {
return null;
}
return lodash.cloneDeep(can);
};
ctx.permission = {