Add WorkspaceAuthContextMiddleware (#17487)

## Context
Introduces a middleware that automatically sets the workspace auth
context in AsyncLocalStorage for HTTP requests, making it available
throughout the request lifecycle without explicit parameter passing.

The motivation behind this change is to reduce boilerplate and simplify
the developer experience when working with workspace data in HTTP
request handlers.

The Problem (Before)
Every HTTP request handler that needed to access workspace data had to:
- Extract auth-related info from decorators (@AuthWorkspace(),
@AuthUserWorkspaceId(), etc.) in controller/resolver and pass down to
services
- Build or pass the authContext explicitly (sometimes with type
assertion which was flaky)
Then call executeInWorkspaceContext(authContext, async () => { ... })

## Changes
- Add WorkspaceAuthContextMiddleware that extracts auth context from the
request and stores it in AsyncLocalStorage
- Register middleware for GraphQL, metadata, and REST routes (runs after
hydration middlewares)
- Simplify executeInWorkspaceContext signature: fn is now the first
parameter, authContext is optional second
- If authContext is not provided, it's automatically retrieved from the
storage (set by middleware)
- Update all callers (~120 files) to use the new parameter order


- Fixes a bug in search where system auth context was used, bypassing
RLS feature.
This commit is contained in:
Weiko
2026-01-27 18:24:51 +01:00
committed by GitHub
parent dd98146c99
commit 2daebc6d0f
151 changed files with 3743 additions and 4002 deletions
@@ -44,7 +44,6 @@ export class DashboardDuplicationService {
const workspaceId = workspace.id;
return this.globalWorkspaceOrmManager.executeInWorkspaceContext(
authContext as WorkspaceAuthContext,
async () => {
const dashboardRepository =
await this.globalWorkspaceOrmManager.getRepository<DashboardWorkspaceEntity>(
@@ -108,6 +107,7 @@ export class DashboardDuplicationService {
throw error;
}
},
authContext as WorkspaceAuthContext,
);
}
@@ -52,32 +52,29 @@ export class DashboardToPageLayoutSyncService {
}): Promise<void> {
const authContext = buildSystemAuthContext(workspaceId);
await this.globalWorkspaceOrmManager.executeInWorkspaceContext(
authContext,
async () => {
const dashboardRepository =
await this.globalWorkspaceOrmManager.getRepository<DashboardWorkspaceEntity>(
workspaceId,
'dashboard',
{ shouldBypassPermissionChecks: true },
);
const dashboards = await dashboardRepository.find({
where: {
id: In(dashboardIds),
},
withDeleted: true,
});
const pageLayoutIds = dashboards
.map((dashboard) => dashboard.pageLayoutId)
.filter(isDefined);
await this.pageLayoutService.destroyMany({
ids: pageLayoutIds,
await this.globalWorkspaceOrmManager.executeInWorkspaceContext(async () => {
const dashboardRepository =
await this.globalWorkspaceOrmManager.getRepository<DashboardWorkspaceEntity>(
workspaceId,
});
},
);
'dashboard',
{ shouldBypassPermissionChecks: true },
);
const dashboards = await dashboardRepository.find({
where: {
id: In(dashboardIds),
},
withDeleted: true,
});
const pageLayoutIds = dashboards
.map((dashboard) => dashboard.pageLayoutId)
.filter(isDefined);
await this.pageLayoutService.destroyMany({
ids: pageLayoutIds,
workspaceId,
});
}, authContext);
}
}
@@ -206,27 +206,24 @@ const createDashboardRecord = async (
): Promise<string> => {
const authContext = buildSystemAuthContext(context.workspaceId);
return deps.globalWorkspaceOrmManager.executeInWorkspaceContext(
authContext,
async () => {
const dashboardRepository =
await deps.globalWorkspaceOrmManager.getRepository(
context.workspaceId,
'dashboard',
{ shouldBypassPermissionChecks: true },
);
return deps.globalWorkspaceOrmManager.executeInWorkspaceContext(async () => {
const dashboardRepository =
await deps.globalWorkspaceOrmManager.getRepository(
context.workspaceId,
'dashboard',
{ shouldBypassPermissionChecks: true },
);
const position = await deps.recordPositionService.buildRecordPosition({
value: 'first',
objectMetadata: { isCustom: false, nameSingular: 'dashboard' },
workspaceId: context.workspaceId,
});
const position = await deps.recordPositionService.buildRecordPosition({
value: 'first',
objectMetadata: { isCustom: false, nameSingular: 'dashboard' },
workspaceId: context.workspaceId,
});
const dashboard = { id: uuidv4(), title, pageLayoutId, position };
const dashboard = { id: uuidv4(), title, pageLayoutId, position };
await dashboardRepository.insert(dashboard);
await dashboardRepository.insert(dashboard);
return dashboard.id;
},
);
return dashboard.id;
}, authContext);
};
@@ -27,7 +27,6 @@ export const createGetDashboardTool = (
const dashboard =
await deps.globalWorkspaceOrmManager.executeInWorkspaceContext(
authContext,
async () => {
const repo = await deps.globalWorkspaceOrmManager.getRepository(
context.workspaceId,
@@ -37,6 +36,7 @@ export const createGetDashboardTool = (
return repo.findOne({ where: { id: parameters.dashboardId } });
},
authContext,
);
if (!isDefined(dashboard)) {
@@ -30,7 +30,6 @@ export const createListDashboardsTool = (
const dashboards =
await deps.globalWorkspaceOrmManager.executeInWorkspaceContext(
authContext,
async () => {
const repo = await deps.globalWorkspaceOrmManager.getRepository(
context.workspaceId,
@@ -40,6 +39,7 @@ export const createListDashboardsTool = (
return repo.find({ take: limit, order: { position: 'ASC' } });
},
authContext,
);
const dashboardList = dashboards.map((d) => ({