feat: revokeResource when roleResource deleted

This commit is contained in:
Chareice 2022-02-11 22:00:11 +08:00 committed by chenos
parent e92a21f072
commit a2037d90c9
2 changed files with 78 additions and 0 deletions

View File

@ -207,6 +207,69 @@ describe('acl', () => {
).toBeNull(); ).toBeNull();
}); });
it('should revoke resource when collection destroy', async () => {
const role = await db.getRepository('roles').create({
values: {
name: 'admin',
title: 'Admin User',
allowConfigure: true,
},
});
await db.getRepository('collections').create({
values: {
name: 'posts',
},
});
await db.getRepository('fields').create({
values: {
collectionName: 'posts',
type: 'string',
name: 'title',
},
});
await app
.agent()
.resource('roles.resources')
.create({
associatedIndex: role.get('name') as string,
values: {
name: 'posts',
usingActionsConfig: true,
actions: [
{
name: 'view',
fields: ['title'],
},
],
},
});
expect(
acl.can({
role: 'admin',
resource: 'posts',
action: 'view',
}),
).not.toBeNull();
await db.getRepository('collections').destroy({
filter: {
name: 'posts',
},
});
expect(
acl.can({
role: 'admin',
resource: 'posts',
action: 'view',
}),
).toBeNull();
});
it('should revoke actions when not using actions config', async () => { it('should revoke actions when not using actions config', async () => {
await db.getRepository('roles').create({ await db.getRepository('roles').create({
values: { values: {

View File

@ -169,6 +169,21 @@ export class PluginACL extends Plugin {
await this.writeResourceToACL(resource, transaction); await this.writeResourceToACL(resource, transaction);
}); });
this.app.db.on('rolesResources.afterDestroy', async (model, options) => {
const role = this.acl.getRole(model.get('roleName'));
role.revokeResource(model.get('name'));
});
this.app.db.on('collections.afterDestroy', async (model, options) => {
const { transaction } = options;
await this.app.db.getRepository('rolesResources').destroy({
filter: {
name: model.get('name'),
},
transaction,
});
});
this.app.db.on('fields.afterCreate', async (model, options) => { this.app.db.on('fields.afterCreate', async (model, options) => {
const { transaction } = options; const { transaction } = options;