From 103552d79c1f714aea9ef0497f05f33deec2be66 Mon Sep 17 00:00:00 2001 From: Chareice Date: Fri, 26 Jan 2024 17:19:45 +0800 Subject: [PATCH] chore: create roles resources --- .../__tests__/data-source-with-acl.test.ts | 68 +++++++------------ ...> data-sources-roles-resources-actions.ts} | 7 +- ...=> data-sources-roles-resources-scopes.ts} | 7 +- .../data-sources-roles-resources.ts | 3 +- .../src/server/collections/data-sources.ts | 4 +- .../src/server/plugin.ts | 2 +- ...resources.ts => data-sources-resources.ts} | 26 +++---- 7 files changed, 49 insertions(+), 68 deletions(-) rename packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/{connections-roles-resources-actions.ts => data-sources-roles-resources-actions.ts} (75%) rename packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/{connections-roles-resources-scopes.ts => data-sources-roles-resources-scopes.ts} (74%) rename packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/{connection-resources.ts => data-sources-resources.ts} (68%) diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-source-with-acl.test.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-source-with-acl.test.ts index f9b9fba994..0e2ef538c9 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-source-with-acl.test.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-source-with-acl.test.ts @@ -139,31 +139,12 @@ describe('data source with acl', () => { }); it('should create resources', async () => { - class MockDataSource extends DataSource { - async load(): Promise {} - - createCollectionManager(options?: any): any { - return undefined; - } - } - - app.dataSourceManager.factory.register('mock', MockDataSource); - const adminUser = await app.db.getRepository('users').create({ values: { roles: ['root'], }, }); - const adminAgent: any = app.agent().login(adminUser); - - await app.db.getRepository('dataSources').create({ - values: { - key: 'test', - type: 'mock', - }, - }); - await app.db.getRepository('roles').create({ values: { name: 'testRole', @@ -177,37 +158,36 @@ describe('data source with acl', () => { }, }); - const testUserAgent = app.agent().login(testUser); + const adminAgent: any = app.agent().login(adminUser); - const createResourceResp = await adminAgent - .resource('databaseConnections.connectionsRolesResourcesScopes', 'test') - .create({ - values: { - name: 'articles title starts with test', - resourceName: 'Articles', - scope: { - title: { - $startsWith: 'test', - }, + // should get permission error + const testUserAgent = getDataSourceAgent(app.agent().login(testUser), 'mockInstance1'); + + const createResourceResp = await adminAgent.resource('dataSources.rolesResourcesScopes', 'mockInstance1').create({ + values: { + name: 'posts title starts with test', + resourceName: 'posts', + scope: { + title: { + $startsWith: 'test', }, }, - }); + }, + }); expect(createResourceResp.status).toBe(200); // list scopes - const listScopesResp = await adminAgent - .resource('databaseConnections.connectionsRolesResourcesScopes', 'test') - .list({}); + const listScopesResp = await adminAgent.resource('dataSources.rolesResourcesScopes', 'mockInstance1').list({}); expect(listScopesResp.status).toBe(200); - const scope = listScopesResp.body.data.find((item) => item.name === 'articles title starts with test'); + const scope = listScopesResp.body.data.find((item) => item.name === 'posts title starts with test'); // create user resource permission - const createConnectionResourceResp = await adminAgent.resource('roles.connectionResources', 'testRole').create({ + const createConnectionResourceResp = await adminAgent.resource('roles.dataSourceResources', 'testRole').create({ values: { - connectionName: 'test', + dataSourceKey: 'mockInstance1', usingActionsConfig: true, actions: [ { @@ -218,7 +198,7 @@ describe('data source with acl', () => { }, }, ], - name: 'Articles', + name: 'posts', }, }); @@ -227,10 +207,10 @@ describe('data source with acl', () => { const data = createConnectionResourceResp.body.data; // update scope to null - const updateScopeResp = await adminAgent.resource('roles.connectionResources', 'testRole').update({ + const updateScopeResp = await adminAgent.resource('roles.dataSourceResources', 'testRole').update({ filter: { - connectionName: 'test', - name: 'Articles', + dataSourceKey: 'mockInstance1', + name: 'posts', }, values: { actions: data.actions.map((action) => { @@ -246,10 +226,10 @@ describe('data source with acl', () => { expect(updateScopeResp.status).toBe(200); // get resourcers - const getResourceResp = await adminAgent.resource('roles.connectionResources', 'testRole').get({ + const getResourceResp = await adminAgent.resource('roles.dataSourceResources', 'testRole').get({ filter: { - connectionName: 'test', - name: 'Articles', + dataSourceKey: 'mockInstance1', + name: 'posts', }, appends: ['actions.scope'], }); diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/connections-roles-resources-actions.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources-actions.ts similarity index 75% rename from packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/connections-roles-resources-actions.ts rename to packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources-actions.ts index 8fd454adc5..0479863a41 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/connections-roles-resources-actions.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources-actions.ts @@ -2,14 +2,14 @@ import { defineCollection } from '@nocobase/database'; export default defineCollection({ dumpRules: 'required', - name: 'connectionsRolesResourcesActions', + name: 'dataSourcesRolesResourcesActions', model: 'ConnectionsRolesResourcesActionModel', fields: [ { type: 'belongsTo', name: 'resource', foreignKey: 'rolesResourceId', - target: 'connectionRolesResources', + target: 'dataSourcesRolesResources', }, { type: 'string', @@ -23,8 +23,9 @@ export default defineCollection({ { type: 'belongsTo', name: 'scope', - target: 'connectionsRolesResourcesScopes', + target: 'dataSourcesRolesResourcesScopes', onDelete: 'RESTRICT', + foreignKey: 'scopeId', }, ], }); diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/connections-roles-resources-scopes.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources-scopes.ts similarity index 74% rename from packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/connections-roles-resources-scopes.ts rename to packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources-scopes.ts index 0c0a3fdc6c..3b6536e5b4 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/connections-roles-resources-scopes.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources-scopes.ts @@ -2,7 +2,7 @@ import { defineCollection } from '@nocobase/database'; export default defineCollection({ dumpRules: 'required', - name: 'connectionsRolesResourcesScopes', + name: 'dataSourcesRolesResourcesScopes', fields: [ { type: 'uid', @@ -10,9 +10,8 @@ export default defineCollection({ }, { type: 'belongsTo', - name: 'connection', - target: 'databaseConnections', - foreignKey: 'connectionName', + name: 'dataSources', + foreignKey: 'dataSourceKey', onDelete: 'CASCADE', }, { diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources.ts index 2ee0cb1168..2849f0c23b 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources-roles-resources.ts @@ -29,7 +29,8 @@ export default defineCollection({ { type: 'hasMany', name: 'actions', - target: 'connectionsRolesResourcesActions', + target: 'dataSourcesRolesResourcesActions', + foreignKey: 'rolesResourceId', }, ], }); diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources.ts index 77c7a60b40..1f0a335fd3 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources.ts @@ -26,8 +26,8 @@ export default defineCollection({ }, { type: 'hasMany', - name: 'connectionsRolesResourcesScopes', - target: 'connectionsRolesResourcesScopes', + name: 'rolesResourcesScopes', + target: 'dataSourcesRolesResourcesScopes', foreignKey: 'dataSourceKey', }, ], diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts index 217d580e02..8a1f35854e 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts @@ -8,7 +8,7 @@ import { RemoteCollectionModel } from './models/remote-collection-model'; import { RemoteFieldModel } from './models/remote-field-model'; import { rolesRemoteCollectionsResourcer } from './resourcers/roles-remote-collections'; import databaseConnectionsRolesResourcer from './resourcers/data-sources-roles'; -import rolesConnectionResourcesResourcer from './resourcers/connection-resources'; +import rolesConnectionResourcesResourcer from './resourcers/data-sources-resources'; import { ConnectionsRolesModel } from './models/connections-roles-model'; import { ConnectionsRolesResourcesModel } from './models/connections-roles-resources'; diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/connection-resources.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/data-sources-resources.ts similarity index 68% rename from packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/connection-resources.ts rename to packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/data-sources-resources.ts index cfda34b3ff..0ebe3acddb 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/connection-resources.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/data-sources-resources.ts @@ -1,5 +1,5 @@ export default { - name: 'roles.connectionResources', + name: 'roles.dataSourceResources', actions: { async create(ctx, next) { const { associatedIndex: roleName } = ctx.action.params; @@ -8,31 +8,31 @@ export default { const transaction = await db.sequelize.transaction(); - const connectionName = ctx.action.params.values.connectionName; + const dataSourceKey = ctx.action.params.values.dataSourceKey; - if (!connectionName) { - throw new Error('connectionName is required'); + if (!dataSourceKey) { + throw new Error('dataSourceKey is required'); } - const connectionRole = await db.getRepository('connectionsRoles').findOne({ + const connectionRole = await db.getRepository('dataSourcesRoles').findOne({ filter: { roleName, - connectionName, + dataSourceKey, }, transaction, }); if (!connectionRole) { - await db.getRepository('connectionsRoles').create({ + await db.getRepository('dataSourcesRoles').create({ values: { roleName, - connectionName, + dataSourceKey, }, transaction, }); } - const record = await db.getRepository('connectionsRolesResources').create({ + const record = await db.getRepository('dataSourcesRolesResources').create({ values: { roleName, ...ctx.action.params.values, @@ -52,11 +52,11 @@ export default { ctx.body = await ctx.app .getDb() - .getRepository('connectionsRolesResources') + .getRepository('dataSourcesRolesResources') .update({ filter: { roleName, - connectionName: ctx.action.params.filter.connectionName, + dataSourceKey: ctx.action.params.filter.dataSourceKey, name: ctx.action.params.filter.name, }, values: ctx.action.params.values, @@ -71,11 +71,11 @@ export default { const record = await ctx.app .getDb() - .getRepository('connectionsRolesResources') + .getRepository('dataSourcesRolesResources') .findOne({ filter: { roleName, - connectionName: ctx.action.params.filter.connectionName, + dataSourceKey: ctx.action.params.filter.dataSourceKey, name: ctx.action.params.filter.name, }, appends: ctx.action.params.appends,