fix(backup-restore): snippet name (#4700)

This commit is contained in:
ChengLei Shao 2024-06-19 07:51:46 +08:00 committed by GitHub
parent ed89dd04c8
commit 6522720a96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 91 additions and 1 deletions

View File

@ -19,7 +19,7 @@ export class PluginBackupRestoreClient extends Plugin {
title: this.t('Backup & Restore'),
icon: 'CloudServerOutlined',
Component: BackupAndRestoreList,
aclSnippet: 'pm.backup.restore',
aclSnippet: 'pm.backup-restore',
});
}
}

View File

@ -0,0 +1,50 @@
/**
* 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 { Database, MigrationContext } from '@nocobase/database';
import updateSnippetName from '../../migrations/20240618103927-update-snippet-name';
import { createMockServer, MockServer } from '@nocobase/test';
describe('migration snippet name test', () => {
let app: MockServer;
let db: Database;
beforeEach(async () => {
app = await createMockServer({
plugins: ['nocobase'],
});
db = app.db;
});
afterEach(async () => {
await app.destroy();
});
it('should change snippet name', async () => {
await db.getRepository('roles').create({
values: {
name: 'test',
snippets: ['pm.backup.restore'],
},
});
const migration = new updateSnippetName({ db } as MigrationContext);
migration.context.app = app;
await migration.up();
const role = await db.getRepository('roles').findOne({
filter: {
name: 'test',
},
});
expect(role.get('snippets')).toEqual(['pm.backup-restore']);
});
});

View File

@ -0,0 +1,40 @@
/**
* 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 { Migration } from '@nocobase/server';
export default class extends Migration {
on = 'afterLoad'; // 'beforeLoad' or 'afterLoad'
appVersion = '<1.2.4-alpha';
async up() {
const roles = await this.db.getRepository('roles').find();
for (const role of roles) {
const snippets = await role.get('snippets');
let roleNeedsUpdate = false;
for (let i = 0; i < snippets.length; i++) {
if (snippets[i].includes('pm.backup.restore')) {
snippets[i] = snippets[i].replace('pm.backup.restore', 'pm.backup-restore');
roleNeedsUpdate = true;
}
}
if (roleNeedsUpdate) {
await this.db.getRepository('roles').update({
filter: {
name: role.get('name'),
},
values: {
snippets: JSON.parse(JSON.stringify(snippets)),
},
});
}
}
}
}