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:
@@ -210,36 +210,32 @@ export class MatchParticipantService<
|
||||
}: MatchParticipantsForWorkspaceMembersArgs) {
|
||||
const authContext = buildSystemAuthContext(workspaceId);
|
||||
|
||||
await this.globalWorkspaceOrmManager.executeInWorkspaceContext(
|
||||
authContext,
|
||||
async () => {
|
||||
const participantRepository = await this.getParticipantRepository(
|
||||
workspaceId,
|
||||
objectMetadataName,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.executeInWorkspaceContext(async () => {
|
||||
const participantRepository = await this.getParticipantRepository(
|
||||
workspaceId,
|
||||
objectMetadataName,
|
||||
);
|
||||
|
||||
const participants = await participantRepository.find({
|
||||
where: {
|
||||
workspaceMemberId: In(participantMatching.workspaceMemberIds),
|
||||
},
|
||||
});
|
||||
const participants = await participantRepository.find({
|
||||
where: {
|
||||
workspaceMemberId: In(participantMatching.workspaceMemberIds),
|
||||
},
|
||||
});
|
||||
|
||||
const tobeRematchedParticipants = participants.map((participant) => {
|
||||
return {
|
||||
...participant,
|
||||
workspaceMemberId: null,
|
||||
};
|
||||
});
|
||||
const tobeRematchedParticipants = participants.map((participant) => {
|
||||
return {
|
||||
...participant,
|
||||
workspaceMemberId: null,
|
||||
};
|
||||
});
|
||||
|
||||
await this.matchParticipants({
|
||||
matchWith: 'workspaceMemberOnly',
|
||||
participants:
|
||||
tobeRematchedParticipants as ParticipantWorkspaceEntity[],
|
||||
objectMetadataName,
|
||||
workspaceId,
|
||||
});
|
||||
},
|
||||
);
|
||||
await this.matchParticipants({
|
||||
matchWith: 'workspaceMemberOnly',
|
||||
participants: tobeRematchedParticipants as ParticipantWorkspaceEntity[],
|
||||
objectMetadataName,
|
||||
workspaceId,
|
||||
});
|
||||
}, authContext);
|
||||
}
|
||||
|
||||
public async matchParticipantsForPeople({
|
||||
@@ -249,56 +245,53 @@ export class MatchParticipantService<
|
||||
}: MatchParticipantsForPeopleArgs) {
|
||||
const authContext = buildSystemAuthContext(workspaceId);
|
||||
|
||||
await this.globalWorkspaceOrmManager.executeInWorkspaceContext(
|
||||
authContext,
|
||||
async () => {
|
||||
const participantRepository = await this.getParticipantRepository(
|
||||
workspaceId,
|
||||
objectMetadataName,
|
||||
);
|
||||
await this.globalWorkspaceOrmManager.executeInWorkspaceContext(async () => {
|
||||
const participantRepository = await this.getParticipantRepository(
|
||||
workspaceId,
|
||||
objectMetadataName,
|
||||
);
|
||||
|
||||
let participantsMatchingPersonEmails: ParticipantWorkspaceEntity[] = [];
|
||||
let participantsMatchingPersonId: ParticipantWorkspaceEntity[] = [];
|
||||
let participantsMatchingPersonEmails: ParticipantWorkspaceEntity[] = [];
|
||||
let participantsMatchingPersonId: ParticipantWorkspaceEntity[] = [];
|
||||
|
||||
if (participantMatching.personIds.length > 0) {
|
||||
participantsMatchingPersonId = (await participantRepository.find({
|
||||
where: {
|
||||
personId: In(participantMatching.personIds),
|
||||
},
|
||||
})) as ParticipantWorkspaceEntity[];
|
||||
}
|
||||
|
||||
if (participantMatching.personEmails.length > 0) {
|
||||
participantsMatchingPersonEmails = (await participantRepository.find({
|
||||
where: {
|
||||
handle: In(participantMatching.personEmails),
|
||||
},
|
||||
})) as ParticipantWorkspaceEntity[];
|
||||
}
|
||||
|
||||
const uniqueParticipants = [
|
||||
...new Set([
|
||||
...participantsMatchingPersonId,
|
||||
...participantsMatchingPersonEmails,
|
||||
]),
|
||||
];
|
||||
|
||||
const tobeRematchedParticipants = uniqueParticipants.map(
|
||||
(participant) => {
|
||||
return {
|
||||
...participant,
|
||||
personId: null,
|
||||
};
|
||||
if (participantMatching.personIds.length > 0) {
|
||||
participantsMatchingPersonId = (await participantRepository.find({
|
||||
where: {
|
||||
personId: In(participantMatching.personIds),
|
||||
},
|
||||
);
|
||||
})) as ParticipantWorkspaceEntity[];
|
||||
}
|
||||
|
||||
await this.matchParticipants({
|
||||
matchWith: 'personOnly',
|
||||
participants: tobeRematchedParticipants,
|
||||
objectMetadataName,
|
||||
workspaceId,
|
||||
});
|
||||
},
|
||||
);
|
||||
if (participantMatching.personEmails.length > 0) {
|
||||
participantsMatchingPersonEmails = (await participantRepository.find({
|
||||
where: {
|
||||
handle: In(participantMatching.personEmails),
|
||||
},
|
||||
})) as ParticipantWorkspaceEntity[];
|
||||
}
|
||||
|
||||
const uniqueParticipants = [
|
||||
...new Set([
|
||||
...participantsMatchingPersonId,
|
||||
...participantsMatchingPersonEmails,
|
||||
]),
|
||||
];
|
||||
|
||||
const tobeRematchedParticipants = uniqueParticipants.map(
|
||||
(participant) => {
|
||||
return {
|
||||
...participant,
|
||||
personId: null,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
await this.matchParticipants({
|
||||
matchWith: 'personOnly',
|
||||
participants: tobeRematchedParticipants,
|
||||
objectMetadataName,
|
||||
workspaceId,
|
||||
});
|
||||
}, authContext);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user