diff --git a/packages/module-file/src/server/actions/attachments.ts b/packages/module-file/src/server/actions/attachments.ts index cfd917af8a..4257b749e1 100644 --- a/packages/module-file/src/server/actions/attachments.ts +++ b/packages/module-file/src/server/actions/attachments.ts @@ -31,6 +31,11 @@ function getFileData(ctx: Context) { const filename = path.basename(name); const extname = path.extname(filename); const urlPath = storage.path ? storage.path.replace(/^([^/])/, '/$1') : ''; + let create_user_id = ''; + if (storage.type === 'local' && storage.create_user_id) { + storage.create_user_id = storage.create_user_id.replace(/^([^/])/, '/$1'); + create_user_id = `/${storage.create_user_id}`; + } return { title: Buffer.from(file.originalname, 'latin1').toString('utf8').replace(extname, ''), @@ -40,7 +45,7 @@ function getFileData(ctx: Context) { path: storage.path, size: file.size, // 直接缓存起来 - url: `${storage.baseUrl}${urlPath}/${filename}`, + url: `${storage.baseUrl}${create_user_id}${urlPath}/${filename}`, mimetype: file.mimetype, // @ts-ignore meta: ctx.request.body, diff --git a/packages/module-file/src/server/collections/storages.ts b/packages/module-file/src/server/collections/storages.ts index 7677ce56b1..8ffb962106 100644 --- a/packages/module-file/src/server/collections/storages.ts +++ b/packages/module-file/src/server/collections/storages.ts @@ -60,5 +60,11 @@ export default defineCollection({ name: 'paranoid', defaultValue: false, }, + { + // 主应用创建者的用户id + type: 'bigInt', + name: 'create_user_id', + defaultValue: false, + }, ], }); diff --git a/packages/module-file/src/server/migrations/20250401175040-move-file.ts b/packages/module-file/src/server/migrations/20250401175040-move-file.ts new file mode 100644 index 0000000000..557dfb28f1 --- /dev/null +++ b/packages/module-file/src/server/migrations/20250401175040-move-file.ts @@ -0,0 +1,13 @@ +import { Migration } from '@tachybase/server'; + +export default class extends Migration { + on = 'afterLoad'; // 'beforeLoad' or 'afterLoad' + appVersion = '<1.0.4'; + + async up() { + // coding + if (this.app.name === 'main') { + return; + } + } +} diff --git a/packages/module-multi-app/src/server/collections/originalInfo.ts b/packages/module-multi-app/src/server/collections/originalInfo.ts new file mode 100644 index 0000000000..9b6d9c3bef --- /dev/null +++ b/packages/module-multi-app/src/server/collections/originalInfo.ts @@ -0,0 +1,21 @@ +import { defineCollection } from '@tachybase/database'; + +export default defineCollection({ + dumpRules: { + group: 'third-party', + }, + name: 'originalInfo', + autoGenId: false, + createdBy: true, + updatedBy: true, + fields: [ + { + type: 'bigInt', + name: 'create_user_id', // 主应用的用户id,即使是爷孙关系,也应该是爷爷的用户id + }, + { + type: 'string', + name: 'parent_id', + }, + ], +}); diff --git a/packages/module-multi-app/src/server/server.ts b/packages/module-multi-app/src/server/server.ts index c37f01fc19..6d4f63d6a8 100644 --- a/packages/module-multi-app/src/server/server.ts +++ b/packages/module-multi-app/src/server/server.ts @@ -1,4 +1,5 @@ import path from 'path'; +import { Context } from '@tachybase/actions'; import { Database, IDatabaseOptions, Transactionable } from '@tachybase/database'; import Application, { AppSupervisor, Gateway, Plugin } from '@tachybase/server'; @@ -59,7 +60,7 @@ const defaultSubAppUpgradeHandle: SubAppUpgradeHandler = async (mainApp: Applica } }; -const defaultDbCreator = async (app: Application) => { +const defaultDbCreator = async (app: Application, { context }: { context: Context }) => { const databaseOptions = app.options.database as any; const { host, port, username, password, dialect, database } = databaseOptions; const tmpl = app.options?.tmpl; @@ -121,6 +122,29 @@ const defaultDbCreator = async (app: Application) => { } else { await client.query(`CREATE DATABASE "${database}"`); } + + // 新建一个client, 修改数据库里的原始用户id和parent_id + const newClient = new Client({ + host, + port, + user: username, + password, + database, + }); + await newClient.connect(); + // originalInfo插入一条数据create_user_id是当前用户id, parent_id是当前应用的name + let create_user_id; + if (app.name === 'main') { + create_user_id = context.state.currentUser.id; + } else { + const originalInfo = await context.db.getRepository('originalInfo').findOne(); + create_user_id = originalInfo.create_user_id; + } + // TODO: 非模板创建的应用怎么办 + await newClient.query( + `INSERT INTO originalInfo (create_user_id, parent_id) VALUES (${create_user_id}, ${app.name})`, + ); + await newClient.end(); } catch (e) { app.logger.error(JSON.stringify(e)); AppSupervisor.getInstance().setAppError(database, e); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index df947ccf2c..c30561de03 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4343,37 +4343,6 @@ importers: specifier: ^5.1.6 version: 5.1.6 - packages/plugin-test-temp: - dependencies: - '@tachybase/client': - specifier: workspace:* - version: link:../client - '@tachybase/data-source': - specifier: workspace:* - version: link:../data-source - '@tachybase/database': - specifier: workspace:* - version: link:../database - '@tachybase/resourcer': - specifier: workspace:* - version: link:../resourcer - '@tachybase/server': - specifier: workspace:* - version: link:../server - '@tachybase/test': - specifier: workspace:* - version: link:../test - '@tachybase/utils': - specifier: workspace:* - version: link:../utils - devDependencies: - '@sgarciac/bombadil': - specifier: ^2.3.0 - version: 2.3.0 - lodash: - specifier: 4.17.21 - version: 4.17.21 - packages/plugin-theme-editor: dependencies: '@tachybase/actions': @@ -9208,9 +9177,6 @@ packages: '@scarf/scarf@1.4.0': resolution: {integrity: sha512-xxeapPiUXdZAE3che6f3xogoJPeZgig6omHEy1rIY5WVsB3H2BHNnZH+gHG6x91SCWyQCzWGsuL2Hh3ClO5/qQ==} - '@sgarciac/bombadil@2.3.0': - resolution: {integrity: sha512-jet+J1Dr2NPgVrILO43B6OIFp5v1uyBLGS7pWfQ97YL/zQafayFVCVtSgrRr/gJcyU2Qo3mcqPs4qvvBOydApA==} - '@sinclair/typebox@0.27.8': resolution: {integrity: sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==} @@ -10914,9 +10880,6 @@ packages: resolution: {integrity: sha512-g0J0q/O6mW8z5zxQ3A8E8J1hUgp4SMOvEoW/x84OwyHKe/Zccz83PVT4y5Crcr530FV6NgmKI1qvGTKVl9XXVw==} engines: {node: '>= 6'} - chevrotain@4.8.1: - resolution: {integrity: sha512-Dd2oHFdscdm8MSmG0ejTXdMlqKR5ryeKuwLpsf1ZOs3l8HD/jPfIY0MYs9Eh4X3gm9h0CR155zpVYFK+uAI3tA==} - china-division@2.7.0: resolution: {integrity: sha512-4uUPAT+1WfqDh5jytq7omdCmHNk3j+k76zEG/2IqaGcYB90c2SwcixttcypdsZ3T/9tN1TTpBDoeZn+Yw/qBEA==} @@ -13823,9 +13786,6 @@ packages: lodash.escaperegexp@4.1.2: resolution: {integrity: sha512-TM9YBvyC84ZxE3rgfefxUWiQKLilstD6k7PTGt6wfbtXF8ixIJLOL3VYyV/z+ZiPLsVxAsKAFVwWlWeb2Y8Yyw==} - lodash.every@4.6.0: - resolution: {integrity: sha512-isF82d+65/sNvQ3aaQAW7LLHnnTxSN/2fm4rhYyuufLzA4VtHz6y6S5vFwe6PQVr2xdqUOyxBbTNKDpnmeu50w==} - lodash.flatten@4.4.0: resolution: {integrity: sha512-C5N2Z3DgnnKr0LOpv/hKCgKdb7ZZwafIrsesve6lmzvZIRZRGaZ/l6Q8+2W7NaT+ZwO3fFlSCzCzrDCFdJfZ4g==} @@ -13866,9 +13826,6 @@ packages: lodash.isundefined@3.0.1: resolution: {integrity: sha512-MXB1is3s899/cD8jheYYE2V9qTHwKvt+npCwpD+1Sxm3Q3cECXCiYHjeHWXNwr6Q0SOBPrYUDxendrO6goVTEA==} - lodash.last@3.0.0: - resolution: {integrity: sha512-14mq7rSkCxG4XMy9lF2FbIOqqgF0aH0NfPuQ3LPR3vIh0kHnUvIYP70dqa1Hf47zyXfQ8FzAg0MYOQeSuE1R7A==} - lodash.merge@4.6.2: resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==} @@ -16195,9 +16152,6 @@ packages: regenerator-transform@0.15.2: resolution: {integrity: sha512-hfMp2BoF0qOk3uc5V20ALGDS2ddjQaLrdl7xrGXvAIow7qeWRM2VA2HuCHkUKk9slq3VwEwLNK3DFBqDfPGYtg==} - regexp-to-ast@0.4.0: - resolution: {integrity: sha512-4qf/7IsIKfSNHQXSwial1IFmfM1Cc/whNBQqRwe0V2stPe7KmN1U0tWQiIx6JiirgSrisjE0eECdNf7Tav1Ntw==} - regexp.prototype.flags@1.5.2: resolution: {integrity: sha512-NcDiDkTLuPR+++OCKB0nWafEmhg/Da8aUPLPMQbK+bxKKCm1/S5he+AqYa4PlMCVBalb4/yxIRub6qkEx5yJbw==} engines: {node: '>= 0.4'} @@ -23299,14 +23253,6 @@ snapshots: '@scarf/scarf@1.4.0': {} - '@sgarciac/bombadil@2.3.0': - dependencies: - chevrotain: 4.8.1 - lodash.every: 4.6.0 - lodash.includes: 4.3.0 - lodash.last: 3.0.0 - moment: 2.30.1 - '@sinclair/typebox@0.27.8': {} '@sindresorhus/is@0.14.0': {} @@ -25655,10 +25601,6 @@ snapshots: parse5-htmlparser2-tree-adapter: 6.0.1 tslib: 2.8.1 - chevrotain@4.8.1: - dependencies: - regexp-to-ast: 0.4.0 - china-division@2.7.0: {} chokidar@3.5.1: @@ -29045,8 +28987,6 @@ snapshots: lodash.escaperegexp@4.1.2: {} - lodash.every@4.6.0: {} - lodash.flatten@4.4.0: {} lodash.groupby@4.6.0: {} @@ -29073,8 +29013,6 @@ snapshots: lodash.isundefined@3.0.1: {} - lodash.last@3.0.0: {} - lodash.merge@4.6.2: {} lodash.once@4.1.1: {} @@ -31849,8 +31787,6 @@ snapshots: dependencies: '@babel/runtime': 7.26.9 - regexp-to-ast@0.4.0: {} - regexp.prototype.flags@1.5.2: dependencies: call-bind: 1.0.7