From 914600209a51e9386f452beb3a9a21a0776dc4bd Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 4 May 2022 10:16:53 +0800 Subject: [PATCH] fix: empty resource acl error (#357) --- packages/core/acl/src/acl.ts | 2 + .../plugins/acl/src/__tests__/acl.test.ts | 50 +++++++++++++++++++ .../acl/src/model/RoleResourceModel.ts | 9 +++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/packages/core/acl/src/acl.ts b/packages/core/acl/src/acl.ts index 2f31704d3..1bbd5949e 100644 --- a/packages/core/acl/src/acl.ts +++ b/packages/core/acl/src/acl.ts @@ -175,6 +175,8 @@ export class ACL extends EventEmitter { action, params: actionParams, }; + } else { + return null; } } diff --git a/packages/plugins/acl/src/__tests__/acl.test.ts b/packages/plugins/acl/src/__tests__/acl.test.ts index e14d324df..ccd597bde 100644 --- a/packages/plugins/acl/src/__tests__/acl.test.ts +++ b/packages/plugins/acl/src/__tests__/acl.test.ts @@ -72,6 +72,56 @@ describe('acl', () => { }); }); + it('should deny when resource action has no resource', async () => { + const role = await db.getRepository('roles').create({ + values: { + name: 'admin', + title: 'Admin User', + allowConfigure: true, + strategy: { + actions: ['update:own', 'destroy:own', 'create', 'view'], + }, + }, + }); + + changeMockRole('admin'); + + // create c1 collection + await db.getRepository('collections').create({ + values: { + name: 'c1', + title: 'table1', + }, + }); + + // create c2 collection + await db.getRepository('collections').create({ + values: { + name: 'c2', + title: 'table2', + }, + }); + + await app + .agent() + .resource('roles.resources', 'admin') + .create({ + values: { + name: 'c1', + usingActionsConfig: true, + actions: [], + }, + }); + + expect( + acl.can({ + role: 'admin', + resource: 'c1', + action: 'list', + }), + ).toBeNull(); + }); + it('should works with resources actions', async () => { const role = await db.getRepository('roles').create({ values: { diff --git a/packages/plugins/acl/src/model/RoleResourceModel.ts b/packages/plugins/acl/src/model/RoleResourceModel.ts index 8800349b2..bfd259184 100644 --- a/packages/plugins/acl/src/model/RoleResourceModel.ts +++ b/packages/plugins/acl/src/model/RoleResourceModel.ts @@ -1,5 +1,5 @@ import { Database, Model } from '@nocobase/database'; -import { ACL, ACLRole } from '@nocobase/acl'; +import { ACL, ACLResource, ACLRole } from '@nocobase/acl'; import { RoleResourceActionModel } from './RoleResourceActionModel'; import { AssociationFieldsActions, GrantHelper } from '../server'; @@ -43,6 +43,13 @@ export class RoleResourceModel extends Model { return; } + const resource = new ACLResource({ + role, + name: resourceName, + }); + + role.resources.set(resourceName, resource); + // @ts-ignore const actions: RoleResourceActionModel[] = await this.getActions({ transaction: options.transaction,