fix tenant

This commit is contained in:
COOL 2025-02-26 12:57:33 +08:00
parent 2edbd2ef6d
commit fb46db45d3
2 changed files with 17 additions and 13 deletions

View File

@ -29,7 +29,7 @@
"mysql2": "^3.12.0", "mysql2": "^3.12.0",
"svg-captcha": "^1.4.0", "svg-captcha": "^1.4.0",
"tslib": "^2.8.1", "tslib": "^2.8.1",
"typeorm": "npm:@cool-midway/typeorm@8.0.0", "typeorm": "npm:@cool-midway/typeorm@0.3.20",
"uuid": "^11.0.5", "uuid": "^11.0.5",
"ws": "^8.18.0" "ws": "^8.18.0"
}, },

View File

@ -15,7 +15,6 @@ import {
Config, Config,
IMidwayApplication, IMidwayApplication,
IMidwayContext, IMidwayContext,
Init,
Inject, Inject,
} from '@midwayjs/core'; } from '@midwayjs/core';
import { Utils } from '../../../comm/utils'; import { Utils } from '../../../comm/utils';
@ -67,7 +66,7 @@ export class TenantSubscriber implements EntitySubscriberInterface<any> {
]; ];
// 不进行租户过滤的用户 // 不进行租户过滤的用户
ignoreUsername = ['admin']; ignoreUsername = [];
@Inject() @Inject()
utils: Utils; utils: Utils;
@ -163,7 +162,7 @@ export class TenantSubscriber implements EntitySubscriberInterface<any> {
if (!this.tenant?.enable) return; if (!this.tenant?.enable) return;
const tenantId = this.getTenantId(); const tenantId = this.getTenantId();
if (tenantId) { if (tenantId) {
queryBuilder.where( queryBuilder.andWhere(
`${ `${
queryBuilder.alias ? queryBuilder.alias + '.' : '' queryBuilder.alias ? queryBuilder.alias + '.' : ''
}tenantId = ${tenantId}` }tenantId = ${tenantId}`
@ -175,11 +174,16 @@ export class TenantSubscriber implements EntitySubscriberInterface<any> {
* ID * ID
* @param queryBuilder * @param queryBuilder
*/ */
async afterInsertQueryBuilder(queryBuilder: InsertQueryBuilder<any>) { afterInsertQueryBuilder(queryBuilder: InsertQueryBuilder<any>) {
if (!this.tenant?.enable) return; if (!this.tenant?.enable) return;
const tenantId = await this.getTenantId(); const tenantId = this.getTenantId();
if (tenantId) { if (tenantId) {
queryBuilder.setParameter('tenantId', tenantId); const values = queryBuilder.expressionMap.valuesSet;
if (Array.isArray(values)) {
queryBuilder.values(values.map(item => ({ ...item, tenantId })));
} else if (typeof values === 'object') {
queryBuilder.values({ ...values, tenantId });
}
} }
} }
@ -187,12 +191,12 @@ export class TenantSubscriber implements EntitySubscriberInterface<any> {
* ID和条件 * ID和条件
* @param queryBuilder * @param queryBuilder
*/ */
async afterUpdateQueryBuilder(queryBuilder: UpdateQueryBuilder<any>) { afterUpdateQueryBuilder(queryBuilder: UpdateQueryBuilder<any>) {
if (!this.tenant?.enable) return; if (!this.tenant?.enable) return;
const tenantId = await this.getTenantId(); const tenantId = this.getTenantId();
if (tenantId) { if (tenantId) {
queryBuilder.set({ tenantId }); queryBuilder.set({ tenantId });
queryBuilder.where(`tenantId = ${tenantId}`); queryBuilder.andWhere(`tenantId = ${tenantId}`);
} }
} }
@ -200,11 +204,11 @@ export class TenantSubscriber implements EntitySubscriberInterface<any> {
* ID和条件 * ID和条件
* @param queryBuilder * @param queryBuilder
*/ */
async afterDeleteQueryBuilder(queryBuilder: DeleteQueryBuilder<any>) { afterDeleteQueryBuilder(queryBuilder: DeleteQueryBuilder<any>) {
if (!this.tenant?.enable) return; if (!this.tenant?.enable) return;
const tenantId = await this.getTenantId(); const tenantId = this.getTenantId();
if (tenantId) { if (tenantId) {
queryBuilder.where(`tenantId = ${tenantId}`); queryBuilder.andWhere(`tenantId = ${tenantId}`);
} }
} }
} }