Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 071db1d9d5 fix: override queryRunner.connection in createQueryRunnerForEntityPersistExecutor
https://sonarly.com/issue/35752?type=bug

When updating workspace members with relation fields (like `accountOwnerForCompanies`), TypeORM's internal `RelationIdLoader` calls `createQueryBuilder` directly on the queryRunner's connection, bypassing the permission check override and throwing a "Method not allowed" error.

Fix: The fix adds `connection: dataSourceWithOverridenCreateQueryBuilder` to the `Object.assign` call in both branches of `createQueryRunnerForEntityPersistExecutor()`.

**Problem:** When TypeORM's `EntityPersistExecutor` saves entities with relation fields, it uses `RelationIdLoader` to load existing relation IDs. `RelationIdLoader` calls `this.connection.createQueryBuilder(this.queryRunner)` where `this.connection` is `queryRunner.connection`. The original code only assigned `queryRunner.manager` with the overridden datasource, leaving `queryRunner.connection` pointing to the original `GlobalWorkspaceDataSource` that enforces the `calledByWorkspaceEntityManager === true` check.

**Fix:** Override both `queryRunner.manager` AND `queryRunner.connection` to point to `dataSourceWithOverridenCreateQueryBuilder`. This ensures all internal TypeORM code paths (including `RelationIdLoader`) that access the connection directly will use the overridden `createQueryBuilder` that automatically passes `calledByWorkspaceEntityManager: true`.

The fix is applied to both code paths in the method:
1. The cached path (when `this.dataSourceWithOverridenCreateQueryBuilder` exists)
2. The initial path (when creating `dataSourceWithOverridenCreateQueryBuilder` for the first time)
2026-05-07 17:58:29 +00:00
@@ -131,7 +131,10 @@ export class GlobalWorkspaceDataSource extends DataSource {
queryRunner,
);
Object.assign(queryRunner, { manager: manager });
Object.assign(queryRunner, {
manager: manager,
connection: this.dataSourceWithOverridenCreateQueryBuilder,
});
return queryRunner;
}
@@ -167,7 +170,10 @@ export class GlobalWorkspaceDataSource extends DataSource {
queryRunner,
);
Object.assign(queryRunner, { manager: manager });
Object.assign(queryRunner, {
manager: manager,
connection: dataSourceWithOverridenCreateQueryBuilder,
});
return queryRunner;
}