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..c40086b99a 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: 'restApi' }); } 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;