The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Refuse sign ups that have nowhere to land (#24824)
Félix Malfaitbbaee82bd91c
diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts b/packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.tsindex c0c406ec516..f096a1f7d28 100644--- a/packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts+++ b/packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts@@ -37,6 +37,8 @@ import {hashPassword,} from 'src/engine/core-modules/auth/auth.util';import { MAX_WORKSPACES_WITHOUT_ENTERPRISE_KEY } from 'src/engine/core-modules/auth/constants/max-workspaces-without-enterprise-key.constants';+import { getSignUpWithoutWorkspaceDecision } from 'src/engine/core-modules/auth/utils/get-sign-up-without-workspace-decision.util';+import { hasProvisionedSignUpDestination } from 'src/engine/core-modules/auth/utils/has-provisioned-sign-up-destination.util';import { DEFAULT_DPA_REGION } from 'src/engine/core-modules/dpa/config/dpa-region-config.constant';import { DpaAgreementEntity } from 'src/engine/core-modules/dpa/entities/dpa-agreement.entity';import { DpaAgreementType } from 'src/engine/core-modules/dpa/enums/dpa-agreement-type.enum';@@ -482,6 +484,55 @@ export class SignInUpService {}}+ // Enforced here rather than at workspace creation so a restricted instance+ // stops accumulating verified users that can never reach a workspace.+ private async assertSignUpWithoutWorkspaceAllowed(+ email: string,+ ): Promise<void> {+ const decision = getSignUpWithoutWorkspaceDecision({+ isMultiWorkspaceEnabled: this.twentyConfigService.get(+ 'IS_MULTIWORKSPACE_ENABLED',+ ),+ isWorkspaceCreationLimitedToServerAdmins: this.twentyConfigService.get(+ 'IS_WORKSPACE_CREATION_LIMITED_TO_SERVER_ADMINS',+ ),+ workspaceCount: await this.workspaceRepository.count(),+ });++ if (decision === 'allowed') {+ return;+ }++ if (+ decision === 'requiresDestination' &&+ (await this.hasProvisionedDestination(email))+ ) {+ return;+ }++ throw new AuthException(+ 'New workspace setup is disabled',+ AuthExceptionCode.SIGNUP_DISABLED,+ );+ }++ private async hasProvisionedDestination(email: string): Promise<boolean> {+ // Invitations are read directly instead of through the sign-in picker,+ // which hides HIDDEN workspaces: that is a listing rule, not a statement+ // that the invitee has nowhere to land.+ const invitations =+ await this.workspaceInvitationService.findInvitationsByEmail(email);++ if (hasProvisionedSignUpDestination(invitations)) {+ return true;+ }++ const { availableWorkspacesForSignUp } =+ await this.userWorkspaceService.findAvailableWorkspacesByEmail(email);++ return hasProvisionedSignUpDestination(availableWorkspacesForSignUp);+ }+private async hasServerAdmin(): Promise<boolean> {const adminCount = await this.userRepository.count({where: { canAccessFullAdminPanel: true },@@ -771,7 +822,7 @@ export class SignInUpService {);}- await this.assertSignUpEnabled();+ await this.assertSignUpWithoutWorkspaceAllowed(newUserParams.email);const shouldGrantServerAdmin = !(await this.hasServerAdmin());diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/get-sign-up-without-workspace-decision.util.spec.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/get-sign-up-without-workspace-decision.util.spec.tsnew file mode 100644index 00000000000..08db0a01136--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/get-sign-up-without-workspace-decision.util.spec.ts@@ -0,0 +1,43 @@+import { getSignUpWithoutWorkspaceDecision } from 'src/engine/core-modules/auth/utils/get-sign-up-without-workspace-decision.util';++describe('getSignUpWithoutWorkspaceDecision', () => {+ it('should allow the first sign up of a fresh instance whatever the restrictions', () => {+ expect(+ getSignUpWithoutWorkspaceDecision({+ isMultiWorkspaceEnabled: false,+ isWorkspaceCreationLimitedToServerAdmins: true,+ workspaceCount: 0,+ }),+ ).toBe('allowed');+ });++ it('should refuse a sign up on a single-workspace instance that already has one', () => {+ expect(+ getSignUpWithoutWorkspaceDecision({+ isMultiWorkspaceEnabled: false,+ isWorkspaceCreationLimitedToServerAdmins: false,+ workspaceCount: 1,+ }),+ ).toBe('refused');+ });++ it('should allow a sign up without a destination when workspace creation is unrestricted', () => {+ expect(+ getSignUpWithoutWorkspaceDecision({+ isMultiWorkspaceEnabled: true,+ isWorkspaceCreationLimitedToServerAdmins: false,+ workspaceCount: 1,+ }),+ ).toBe('allowed');+ });++ it('should require a destination when workspace creation is restricted to server admins', () => {+ expect(+ getSignUpWithoutWorkspaceDecision({+ isMultiWorkspaceEnabled: true,+ isWorkspaceCreationLimitedToServerAdmins: true,+ workspaceCount: 1,+ }),+ ).toBe('requiresDestination');+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/has-provisioned-sign-up-destination.util.spec.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/has-provisioned-sign-up-destination.util.spec.tsnew file mode 100644index 00000000000..fb21ca3e05f--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/auth/utils/__tests__/has-provisioned-sign-up-destination.util.spec.ts@@ -0,0 +1,69 @@+import { WorkspaceActivationStatus } from 'twenty-shared/workspace';++import { WorkspaceDiscoverability } from 'src/engine/core-modules/workspace/types/workspace-discoverability.type';++import { hasProvisionedSignUpDestination } from 'src/engine/core-modules/auth/utils/has-provisioned-sign-up-destination.util';++describe('hasProvisionedSignUpDestination', () => {+ it('should not find a destination when nothing is available', () => {+ expect(hasProvisionedSignUpDestination([])).toBe(false);+ });++ it('should find a destination in an active workspace', () => {+ expect(+ hasProvisionedSignUpDestination([+ { workspace: { activationStatus: WorkspaceActivationStatus.ACTIVE } },+ ]),+ ).toBe(true);+ });++ // Suspension is temporary and membership survives it, unlike a workspace+ // that was never provisioned.+ it('should find a destination in a suspended workspace', () => {+ expect(+ hasProvisionedSignUpDestination([+ {+ workspace: { activationStatus: WorkspaceActivationStatus.SUSPENDED },+ },+ ]),+ ).toBe(true);+ });++ it.each([+ WorkspaceActivationStatus.PENDING_CREATION,+ WorkspaceActivationStatus.ONGOING_CREATION,+ WorkspaceActivationStatus.INACTIVE,+ ])('should not find a destination in a %s workspace', (activationStatus) => {+ expect(+ hasProvisionedSignUpDestination([{ workspace: { activationStatus } }]),+ ).toBe(false);+ });++ // Callers pass invitations to hidden workspaces straight in, so nothing here+ // may key off discoverability.+ it('should find a destination regardless of workspace discoverability', () => {+ const hiddenWorkspaceInvitation = {+ workspace: {+ activationStatus: WorkspaceActivationStatus.ACTIVE,+ workspaceDiscoverability: WorkspaceDiscoverability.HIDDEN,+ },+ };++ expect(hasProvisionedSignUpDestination([hiddenWorkspaceInvitation])).toBe(+ true,+ );+ });++ it('should find a destination when only one of several candidates is provisioned', () => {+ expect(+ hasProvisionedSignUpDestination([+ {+ workspace: {+ activationStatus: WorkspaceActivationStatus.PENDING_CREATION,+ },+ },+ { workspace: { activationStatus: WorkspaceActivationStatus.ACTIVE } },+ ]),+ ).toBe(true);+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/get-sign-up-without-workspace-decision.util.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/get-sign-up-without-workspace-decision.util.tsnew file mode 100644index 00000000000..b03b4e73ff4--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/auth/utils/get-sign-up-without-workspace-decision.util.ts@@ -0,0 +1,30 @@+export type SignUpWithoutWorkspaceDecision =+ | 'allowed'+ | 'refused'+ | 'requiresDestination';++// Tri-state rather than a boolean so callers only pay for the destination+// lookup on instances that restrict workspace creation.+export const getSignUpWithoutWorkspaceDecision = ({+ isMultiWorkspaceEnabled,+ isWorkspaceCreationLimitedToServerAdmins,+ workspaceCount,+}: {+ isMultiWorkspaceEnabled: boolean;+ isWorkspaceCreationLimitedToServerAdmins: boolean;+ workspaceCount: number;+}): SignUpWithoutWorkspaceDecision => {+ if (workspaceCount === 0) {+ return 'allowed';+ }++ if (!isMultiWorkspaceEnabled) {+ return 'refused';+ }++ if (!isWorkspaceCreationLimitedToServerAdmins) {+ return 'allowed';+ }++ return 'requiresDestination';+};diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/has-provisioned-sign-up-destination.util.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/has-provisioned-sign-up-destination.util.tsnew file mode 100644index 00000000000..853749db6d0--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/auth/utils/has-provisioned-sign-up-destination.util.ts@@ -0,0 +1,13 @@+import {+ isWorkspaceProvisioned,+ type WorkspaceActivationStatus,+} from 'twenty-shared/workspace';++export const hasProvisionedSignUpDestination = (+ availableWorkspacesForSignUp: {+ workspace: { activationStatus: WorkspaceActivationStatus };+ }[],+): boolean =>+ availableWorkspacesForSignUp.some(({ workspace }) =>+ isWorkspaceProvisioned(workspace),+ );diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.tsindex 3da500f3bd3..af384bb8944 100644--- a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts@@ -539,7 +539,7 @@ export class ConfigVariables {@ConfigVariablesMetadata({group: ConfigVariablesGroup.ADVANCED_SETTINGS,description:- 'When enabled, only server admins can create new workspaces. Ignored during initial setup when no workspace exists.',+ 'When enabled, only server admins can create new workspaces, and signing up without a pending invitation or an approved access domain is refused. Ignored during initial setup when no workspace exists.',type: ConfigVariableType.BOOLEAN,})@IsOptional()