fix: sync role snippets (#5371)

This commit is contained in:
ChengLei Shao 2024-10-08 21:20:35 +08:00 committed by GitHub
parent 26e921e46c
commit 0e39579ae6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { createMockCluster, MockCluster, sleep } from '@nocobase/test';
describe('cluster', () => {
let cluster: MockCluster;
beforeEach(async () => {
cluster = await createMockCluster({
plugins: ['acl', 'field-sort', 'error-handler', 'data-source-main', 'data-source-manager', 'ui-schema-storage'],
acl: false,
});
});
afterEach(async () => {
await cluster.destroy();
});
it('should sync roles when snippet update', async () => {
const [app1, app2] = cluster.nodes;
await app1.db.getRepository('roles').create({
values: {
name: 'role1',
snippets: ['test1'],
},
});
const role1 = app1.acl.getRole('role1');
expect(role1.snippets.has('test1')).toBeTruthy();
// update snippet
await app1.db.getRepository('roles').update({
filter: {
name: 'role1',
},
values: {
snippets: ['test2'],
},
});
await sleep(2000);
expect(role1.snippets.has('test2')).toBeTruthy();
const role1InApp2 = app2.acl.getRole('role1');
expect(role1InApp2.snippets.has('test2')).toBeTruthy();
});
});

View File

@ -45,6 +45,26 @@ export class PluginACLServer extends Plugin {
}); });
} }
async handleSyncMessage(message) {
const { type } = message;
if (type === 'syncRole') {
const { roleName } = message;
const role = await this.app.db.getRepository('roles').findOne({
filter: {
name: roleName,
},
});
await this.writeRoleToACL(role, {
withOutResources: true,
});
await this.app.emitAsync('acl:writeResources', {
roleName: role.get('name'),
});
}
}
async writeRolesToACL(options) { async writeRolesToACL(options) {
const roles = (await this.app.db.getRepository('roles').find({ const roles = (await this.app.db.getRepository('roles').find({
appends: ['resources', 'resources.actions'], appends: ['resources', 'resources.actions'],
@ -213,6 +233,16 @@ export class PluginACLServer extends Plugin {
transaction, transaction,
}); });
} }
this.sendSyncMessage(
{
type: 'syncRole',
roleName: model.get('name'),
},
{
transaction: options.transaction,
},
);
}); });
this.app.db.on('roles.afterDestroy', (model) => { this.app.db.on('roles.afterDestroy', (model) => {