fix: acl write (#280)

This commit is contained in:
ChengLei Shao 2022-04-11 10:09:55 +08:00 committed by GitHub
parent 571b077840
commit 2f6aeacbd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -7,6 +7,7 @@ describe('role resource api', () => {
let app: MockServer; let app: MockServer;
let db: Database; let db: Database;
let role: Model; let role: Model;
afterEach(async () => { afterEach(async () => {
await app.destroy(); 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 () => { it('should grant resource action', async () => {
const collectionManager = db.getRepository('collections') as CollectionRepository; const collectionManager = db.getRepository('collections') as CollectionRepository;

View File

@ -127,13 +127,17 @@ export class PluginACL extends Plugin {
this.app.resourcer.registerActionHandler('roles:check', checkAction); 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; const { transaction } = options;
model.writeToAcl({ model.writeToAcl({
acl: this.acl, acl: this.acl,
}); });
for (const resource of (await model.getResources({ transaction })) as RoleResourceModel[]) {
await this.writeResourceToACL(resource, transaction);
}
// model is default // model is default
if (model.get('default')) { if (model.get('default')) {
await this.app.db.getRepository('roles').update({ await this.app.db.getRepository('roles').update({