diff --git a/packages/plugin-acl/src/__tests__/role-resource.test.ts b/packages/plugin-acl/src/__tests__/role-resource.test.ts index b48b908b0..0c05e7298 100644 --- a/packages/plugin-acl/src/__tests__/role-resource.test.ts +++ b/packages/plugin-acl/src/__tests__/role-resource.test.ts @@ -7,6 +7,7 @@ describe('role resource api', () => { let app: MockServer; let db: Database; let role: Model; + afterEach(async () => { await app.destroy(); }); @@ -30,6 +31,46 @@ describe('role resource api', () => { }); }); + it('should grant resource by createRepository', async () => { + const collectionManager = db.getRepository('collections') as CollectionRepository; + await collectionManager.create({ + values: { + name: 'c1', + title: 'table1', + }, + context: {}, + }); + + await collectionManager.create({ + values: { + name: 'c2', + title: 'table2', + }, + context: {}, + }); + + await db.getRepository('roles').create({ + values: { + name: 'testRole', + resources: [ + { + name: 'c1', + actions: [ + { + name: 'create', + }, + ], + }, + ], + }, + }); + + const acl = app.acl; + const testRole = acl.getRole('testRole'); + const resource = testRole.getResource('c1'); + expect(resource).toBeDefined(); + }); + it('should grant resource action', async () => { const collectionManager = db.getRepository('collections') as CollectionRepository; diff --git a/packages/plugin-acl/src/server.ts b/packages/plugin-acl/src/server.ts index 7ba340783..1900f361b 100644 --- a/packages/plugin-acl/src/server.ts +++ b/packages/plugin-acl/src/server.ts @@ -127,13 +127,17 @@ export class PluginACL extends Plugin { this.app.resourcer.registerActionHandler('roles:check', checkAction); - this.app.db.on('roles.afterSave', async (model, options) => { + this.app.db.on('roles.afterSaveWithAssociations', async (model, options) => { const { transaction } = options; model.writeToAcl({ acl: this.acl, }); + for (const resource of (await model.getResources({ transaction })) as RoleResourceModel[]) { + await this.writeResourceToACL(resource, transaction); + } + // model is default if (model.get('default')) { await this.app.db.getRepository('roles').update({