From d694a7b4833d571eef6cf8b114dd9670699cacbd Mon Sep 17 00:00:00 2001 From: Chareice Date: Wed, 31 Jan 2024 22:17:11 +0800 Subject: [PATCH 1/2] fix: middleware position --- .../data-source-manager/src/data-source.ts | 1 + packages/core/resourcer/src/resourcer.ts | 11 ++--------- packages/core/server/src/helper.ts | 4 ++-- .../__tests__/data-source-with-acl.test.ts | 19 +++++++++++++++++++ .../src/server/plugin.ts | 3 +++ 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/core/data-source-manager/src/data-source.ts b/packages/core/data-source-manager/src/data-source.ts index 4aafd808d1..0add540ea6 100644 --- a/packages/core/data-source-manager/src/data-source.ts +++ b/packages/core/data-source-manager/src/data-source.ts @@ -77,6 +77,7 @@ export abstract class DataSource { const { resourceName, resourceOf } = ctx.action; return this.collectionManager.getRepository(resourceName, resourceOf); }; + return compose([this.collectionToResourceMiddleware(), this.resourceManager.restApiMiddleware()])(ctx, next); }; } diff --git a/packages/core/resourcer/src/resourcer.ts b/packages/core/resourcer/src/resourcer.ts index 444ecf6d13..c223ffd87b 100644 --- a/packages/core/resourcer/src/resourcer.ts +++ b/packages/core/resourcer/src/resourcer.ts @@ -269,18 +269,16 @@ export class Resourcer { if (skipIfDataSourceExists) { const dataSource = ctx.get('x-data-source'); if (dataSource) { - await next(); - return; + return next(); } } + ctx.resourcer = this; - const connectionName = ctx.get('x-connection'); let params = parseRequest( { path: ctx.request.path, method: ctx.request.method, - namespace: connectionName, }, { prefix: this.options.prefix || prefix, @@ -302,7 +300,6 @@ export class Resourcer { path: ctx.request.path, method: ctx.request.method, type: resource.options.type, - namespace: connectionName, }, { prefix: this.options.prefix || prefix, @@ -318,10 +315,6 @@ export class Resourcer { // action 需要 clone 之后再赋给 ctx ctx.action = this.getAction(getNameByParams(params), params.actionName).clone(); - if (params && params.resourceName && params.resourceName.includes('@')) { - params.resourceName = params.resourceName.replace(`${connectionName}@`, ''); - } - ctx.action.setContext(ctx); ctx.action.actionName = params.actionName; ctx.action.resourceOf = params.associatedIndex; diff --git a/packages/core/server/src/helper.ts b/packages/core/server/src/helper.ts index b456eecbc9..de7c7d6dde 100644 --- a/packages/core/server/src/helper.ts +++ b/packages/core/server/src/helper.ts @@ -9,7 +9,7 @@ import fs from 'fs'; import i18next from 'i18next'; import bodyParser from 'koa-bodyparser'; import { resolve } from 'path'; -import { RecordableHistogram, createHistogram } from 'perf_hooks'; +import { createHistogram, RecordableHistogram } from 'perf_hooks'; import Application, { ApplicationOptions } from './application'; import { parseVariables } from './middlewares'; import { dateTemplate } from './middlewares/data-template'; @@ -98,7 +98,7 @@ export function registerMiddlewares(app: Application, options: ApplicationOption app.use(db2resource, { tag: 'db2resource', after: 'dataWrapping' }); app.use(app.resourcer.restApiMiddleware({ skipIfDataSourceExists: true }), { tag: 'restApi', after: 'db2resource' }); - app.use(app.dataSourceManager.middleware()); + app.use(app.dataSourceManager.middleware(), { tag: 'dataSource', after: 'dataWrapping' }); } export const createAppProxy = (app: Application) => { 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 1109cfc5c7..32e863a3d5 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 @@ -94,6 +94,25 @@ describe('data source with acl', () => { await app.destroy(); }); + it('should call application middleware', async () => { + const middlewareFn = vi.fn(); + app.use(async (ctx, next) => { + middlewareFn(); + await next(); + }); + + const adminUser = await app.db.getRepository('users').create({ + values: { + roles: ['root'], + }, + }); + + const adminAgent: any = app.agent().login(adminUser).set('x-data-source', 'mockInstance1'); + const listRes = await adminAgent.resource('api/posts').list(); + expect(listRes.status).toBe(200); + expect(middlewareFn).toBeCalledTimes(1); + }); + it('should allow root user', async () => { const adminUser = await app.db.getRepository('users').create({ values: { 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 53bf4ba524..9ee21ffa59 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 @@ -79,6 +79,9 @@ export class PluginDataSourceManagerServer extends Plugin { this.app.use(async (ctx, next) => { await next(); + if (!ctx.action) { + return; + } const { actionName, resourceName, params } = ctx.action; From c6fc1b65b82bdc5ef4c4741bf6eb8bd3dcc5514d Mon Sep 17 00:00:00 2001 From: Chareice Date: Wed, 31 Jan 2024 22:21:28 +0800 Subject: [PATCH 2/2] fix: middleware position --- packages/core/server/src/helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/server/src/helper.ts b/packages/core/server/src/helper.ts index de7c7d6dde..c40086b99a 100644 --- a/packages/core/server/src/helper.ts +++ b/packages/core/server/src/helper.ts @@ -98,7 +98,7 @@ export function registerMiddlewares(app: Application, options: ApplicationOption app.use(db2resource, { tag: 'db2resource', after: 'dataWrapping' }); app.use(app.resourcer.restApiMiddleware({ skipIfDataSourceExists: true }), { tag: 'restApi', after: 'db2resource' }); - app.use(app.dataSourceManager.middleware(), { tag: 'dataSource', after: 'dataWrapping' }); + app.use(app.dataSourceManager.middleware(), { tag: 'dataSource', after: 'restApi' }); } export const createAppProxy = (app: Application) => {