Skip to content

The open alternative to Salesforce, designed for AI.

Remove viaApplication from the user auth context (#29527)

Charles Bochet2364c64669be
diff --git a/packages/twenty-server/src/engine/core-modules/auth/types/workspace-auth-context.type.ts b/packages/twenty-server/src/engine/core-modules/auth/types/workspace-auth-context.type.ts
index 26c285141d3..7ea886ad2b1 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/types/workspace-auth-context.type.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/types/workspace-auth-context.type.ts
@@ -30,7 +30,6 @@ export interface UserWorkspaceAuthContext extends BaseWorkspaceAuthContext {
workspaceMemberId: NonNullable<RawAuthContext['workspaceMemberId']>;
workspaceMember: NonNullable<RawAuthContext['workspaceMember']>;
application?: NonNullable<RawAuthContext['application']>;
- viaApplication?: NonNullable<RawAuthContext['application']>;
agentId?: string;
// Set when an AI run narrows the caller to an inline scope
permissionScope?: PermissionScope;
diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/build-user-auth-context.util.spec.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/build-user-auth-context.util.spec.ts
index 23217f06967..5c303a2ae49 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/build-user-auth-context.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/build-user-auth-context.util.spec.ts
@@ -17,28 +17,16 @@ const application = {
} as NonNullable<RawAuthContext['application']>;
describe('buildUserAuthContext', () => {
- it('builds a user context without either application field by default', () => {
+ it('builds a user context without an application by default', () => {
const context = buildUserAuthContext(baseInput);
expect(context.type).toBe('user');
expect('application' in context).toBe(false);
- expect('viaApplication' in context).toBe(false);
});
it('carries a user-bound application on the application field', () => {
const context = buildUserAuthContext({ ...baseInput, application });
expect(context.application).toBe(application);
- expect('viaApplication' in context).toBe(false);
- });
-
- it('carries run-as provenance on viaApplication without touching application', () => {
- const context = buildUserAuthContext({
- ...baseInput,
- viaApplication: application,
- });
-
- expect(context.viaApplication).toBe(application);
- expect('application' in context).toBe(false);
});
});
diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/get-auth-context-application.util.spec.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/get-auth-context-application.util.spec.ts
index bded49cad1d..ca3c0afe5b4 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/get-auth-context-application.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/get-auth-context-application.util.spec.ts
@@ -44,22 +44,11 @@ describe('getAuthContextApplication', () => {
);
it('should return the application a member acts through', () => {
- expect(
- getAuthContextApplication({
- type: 'user',
- workspace,
- viaApplication: standardApplication,
- } as WorkspaceAuthContext),
- ).toBe(standardApplication);
- });
-
- it('should prefer application over viaApplication for a member', () => {
expect(
getAuthContextApplication({
type: 'user',
workspace,
application: installedApplication,
- viaApplication: standardApplication,
} as WorkspaceAuthContext),
).toBe(installedApplication);
});
diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/build-user-auth-context.util.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/build-user-auth-context.util.ts
index 3c8d51a3f54..25600168951 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/utils/build-user-auth-context.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/utils/build-user-auth-context.util.ts
@@ -10,7 +10,6 @@ type UserAuthContextInput = {
workspaceMemberId: NonNullable<RawAuthContext['workspaceMemberId']>;
workspaceMember: NonNullable<RawAuthContext['workspaceMember']>;
application?: RawAuthContext['application'];
- viaApplication?: RawAuthContext['application'];
agentId?: RawAuthContext['agentId'];
};
@@ -25,9 +24,6 @@ export const buildUserAuthContext = (
workspaceMemberId: input.workspaceMemberId,
workspaceMember: input.workspaceMember,
...(isDefined(input.application) ? { application: input.application } : {}),
- ...(isDefined(input.viaApplication)
- ? { viaApplication: input.viaApplication }
- : {}),
...(isDefined(input.agentId) ? { agentId: input.agentId } : {}),
};
};
diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/get-auth-context-application.util.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/get-auth-context-application.util.ts
index 558d7f3b309..5cedd547ff3 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/utils/get-auth-context-application.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/utils/get-auth-context-application.util.ts
@@ -17,7 +17,7 @@ export const getAuthContextApplication = (
}
if (isUserAuthContext(authContext)) {
- return authContext.application ?? authContext.viaApplication;
+ return authContext.application;
}
return undefined;
diff --git a/packages/twenty-server/src/engine/core-modules/record-export/services/record-export.workspace-service.ts b/packages/twenty-server/src/engine/core-modules/record-export/services/record-export.workspace-service.ts
index b96abba3037..b7b60849805 100644
--- a/packages/twenty-server/src/engine/core-modules/record-export/services/record-export.workspace-service.ts
+++ b/packages/twenty-server/src/engine/core-modules/record-export/services/record-export.workspace-service.ts
@@ -494,7 +494,7 @@ export class RecordExportWorkspaceService {
): Promise<RecordExportAuthContext> {
if (
!isApiKeyAuthContext(authContext) &&
- (!isUserAuthContext(authContext) || isDefined(authContext.viaApplication))
+ (!isUserAuthContext(authContext) || isDefined(authContext.agentId))
) {
throw new ForbiddenException(t`Sign in to export records.`);
}
@@ -517,9 +517,7 @@ export class RecordExportWorkspaceService {
applicationId: isUserAuthContext(authContext)
? authContext.application?.id
: undefined,
- agentId: isUserAuthContext(authContext)
- ? authContext.agentId
- : undefined,
+ agentId: undefined,
setting: PermissionFlagType.EXPORT_CSV,
}))
) {
diff --git a/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/__tests__/code-interpreter-tool.sender.spec.ts b/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/__tests__/code-interpreter-tool.sender.spec.ts
index eef723baf34..e19c9116f88 100644
--- a/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/__tests__/code-interpreter-tool.sender.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/__tests__/code-interpreter-tool.sender.spec.ts
@@ -143,7 +143,7 @@ describe('Code interpreter sandbox token', () => {
],
[
'an application running its agent as the person',
- buildUserContext({ viaApplication: { id: CALLING_APPLICATION_ID } }),
+ buildUserContext({ application: { id: CALLING_APPLICATION_ID } }),
CALLING_APPLICATION_ID,
],
[
@@ -231,7 +231,7 @@ describe('Code interpreter sandbox token', () => {
it.each([
[
'the person through the application',
- buildUserContext({ viaApplication: { id: CALLING_APPLICATION_ID } }),
+ buildUserContext({ application: { id: CALLING_APPLICATION_ID } }),
{ userId: USER_ID, userWorkspaceId: USER_WORKSPACE_ID },
],
[
@@ -269,7 +269,7 @@ describe('Code interpreter sandbox token', () => {
const { sandboxContext: directRun } =
await runInterpreter(buildUserContext());
const { sandboxContext: applicationRun } = await runInterpreter(
- buildUserContext({ viaApplication: { id: CALLING_APPLICATION_ID } }),
+ buildUserContext({ application: { id: CALLING_APPLICATION_ID } }),
);
expect(directRun.sessionId).toBe(applicationRun.sessionId);
diff --git a/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/code-interpreter-tool.ts b/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/code-interpreter-tool.ts
index 7c94eb763c9..a7eb22ea998 100644
--- a/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/code-interpreter-tool.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/code-interpreter-tool.ts
@@ -347,7 +347,6 @@ export class CodeInterpreterTool implements Tool {
const applicationId =
authContext.application?.id ??
- authContext.viaApplication?.id ??
(await this.findWorkspaceCustomApplicationId(workspaceId));
return {
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-speed-buckets.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-speed-buckets.util.spec.ts
index e668724099a..f010002ecf2 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-speed-buckets.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-speed-buckets.util.spec.ts
@@ -89,7 +89,7 @@ const agentRunContext = {
type: 'user',
workspace,
userWorkspaceId: 'user-workspace-1',
- viaApplication: { id: 'app-1', universalIdentifier: 'app-uid' },
+ application: { id: 'app-1', universalIdentifier: 'app-uid' },
} as WorkspaceAuthContext;
const systemContext = { type: 'system', workspace } as WorkspaceAuthContext;
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-spenders-from-auth-context.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-spenders-from-auth-context.util.spec.ts
index 2d31fb9f6f5..8bdc809af32 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-spenders-from-auth-context.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-spenders-from-auth-context.util.spec.ts
@@ -48,7 +48,7 @@ describe('buildSpendersFromAuthContext', () => {
type: 'user',
workspace,
userWorkspaceId: 'user-workspace-1',
- viaApplication: { id: 'app-1' },
+ application: { id: 'app-1' },
} as WorkspaceAuthContext;
expect(buildSpendersFromAuthContext(authContext)).toContainEqual({
diff --git a/packages/twenty-server/src/engine/core-modules/usage/utils/build-usage-spenders-from-auth-context.util.ts b/packages/twenty-server/src/engine/core-modules/usage/utils/build-usage-spenders-from-auth-context.util.ts
index 955bf291f82..5d1904d5872 100644
--- a/packages/twenty-server/src/engine/core-modules/usage/utils/build-usage-spenders-from-auth-context.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage/utils/build-usage-spenders-from-auth-context.util.ts
@@ -20,8 +20,7 @@ export const buildUsageSpendersFromAuthContext = (
if (isUserAuthContext(authContext)) {
return {
userWorkspaceId: authContext.userWorkspaceId,
- applicationId:
- authContext.application?.id ?? authContext.viaApplication?.id,
+ applicationId: authContext.application?.id,
};
}
diff --git a/packages/twenty-server/src/engine/core-modules/user-workspace/services/user-workspace-auth-context.service.ts b/packages/twenty-server/src/engine/core-modules/user-workspace/services/user-workspace-auth-context.service.ts
index e08ad4184f0..69f0d932355 100644
--- a/packages/twenty-server/src/engine/core-modules/user-workspace/services/user-workspace-auth-context.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/user-workspace/services/user-workspace-auth-context.service.ts
@@ -173,7 +173,6 @@ export class UserWorkspaceAuthContextService {
return buildUserAuthContext({
workspace,
application,
- viaApplication: matchingUserContext?.viaApplication,
userWorkspaceId,
user: fromUserEntityToFlat(userWorkspace.user),
workspaceMemberId,
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-actor-context.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-actor-context.service.ts
index ea62eeb8fe7..2c6dd3ed15e 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-actor-context.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-actor-context.service.ts
@@ -165,11 +165,9 @@ export class AgentActorContextService {
async buildRunAsWorkspaceMemberContext({
workspaceMemberId,
workspaceId,
- viaApplication,
}: {
workspaceMemberId: string;
workspaceId: string;
- viaApplication?: FlatApplication;
}): Promise<RunAsWorkspaceMemberContext> {
const workspaceMember = await this.userWorkspaceService.getWorkspaceMember({
workspaceMemberId,
@@ -215,7 +213,6 @@ export class AgentActorContextService {
user: fromUserEntityToFlat(userWorkspace.user),
workspaceMemberId: workspaceMember.id,
workspaceMember,
- viaApplication,
}),
roleId,
};
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-context.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-context.service.ts
index f9205d7f9ae..24f0c5bd925 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-context.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-context.service.ts
@@ -93,7 +93,6 @@ export class AgentRunCallerContextService {
await this.agentActorContextService.buildRunAsWorkspaceMemberContext({
workspaceMemberId: callerRef.callerWorkspaceMemberId,
workspaceId: workspace.id,
- viaApplication: applicationAuthContext?.application,
});
return { authContext, actorContext };
diff --git a/packages/twenty-server/src/engine/permissions/principal/utils/__tests__/resolve-role-ids-from-auth-context.util.spec.ts b/packages/twenty-server/src/engine/permissions/principal/utils/__tests__/resolve-role-ids-from-auth-context.util.spec.ts
index 4602c3e7d55..cb2aa533bc6 100644
--- a/packages/twenty-server/src/engine/permissions/principal/utils/__tests__/resolve-role-ids-from-auth-context.util.spec.ts
+++ b/packages/twenty-server/src/engine/permissions/principal/utils/__tests__/resolve-role-ids-from-auth-context.util.spec.ts
@@ -53,15 +53,6 @@ describe('resolveRoleIdsFromAuthContext', () => {
]);
});
- it('should ignore viaApplication: run-as provenance never narrows permissions', () => {
- const runAsContext = {
- ...buildUserContext(),
- viaApplication: { defaultRoleId: APPLICATION_ROLE_ID },
- } as WorkspaceAuthContext;
-
- expect(resolve(runAsContext)).toEqual([USER_ROLE_ID]);
- });
-
it('should resolve nothing when the user has no role, even with an application', () => {
const contextWithUnknownUserWorkspace = {
...buildUserContext({ defaultRoleId: APPLICATION_ROLE_ID }),
diff --git a/packages/twenty-server/test/integration/graphql/suites/user-session/utils/capture-code-interpreter-sandbox-tokens.util.ts b/packages/twenty-server/test/integration/graphql/suites/user-session/utils/capture-code-interpreter-sandbox-tokens.util.ts
index 8e81c275847..5d1a5104852 100644
--- a/packages/twenty-server/test/integration/graphql/suites/user-session/utils/capture-code-interpreter-sandbox-tokens.util.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/user-session/utils/capture-code-interpreter-sandbox-tokens.util.ts
@@ -38,14 +38,17 @@ export const captureCodeInterpreterSandboxTokens = async ({
throw new Error(`Application ${applicationId} is not in the cache`);
}
- const { authContext: runAsJaneContext } =
+ const { authContext: janeMemberContext } =
await getAppProviderByClassName<AgentActorContextService>(
'AgentActorContextService',
).buildRunAsWorkspaceMemberContext({
workspaceMemberId: WORKSPACE_MEMBER_DATA_SEED_IDS.JANE,
workspaceId: SEED_APPLE_WORKSPACE_ID,
- viaApplication: flatApplication,
});
+ const runAsJaneContext = {
+ ...janeMemberContext,
+ application: flatApplication,
+ };
const janeContext =
await getAppProviderByClassName<UserWorkspaceAuthContextService>(