Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*): add subdomain management for multiworkpsace #8680

Open
wants to merge 15 commits into
base: feat/allow-to-select-auth-methods-by-workspace
Choose a base branch
from

Conversation

AMoreaux
Copy link
Contributor

Blocked by #8579

Reorganized the sign-in-up code for better readability and maintainability. Introduced validation for workspace invitations and moved some utility functions to dedicated methods. Added a migration for new auth provider columns in the workspace entity.
Removed unused `EnvironmentService` and redundant `code` property in exceptions for clarity. Simplified email validation in `workspace-invitation.service.ts`. Refactored tests to remove unnecessary `targetWorkspaceSubdomain` property.
Removed redundant services from auth and core modules to streamline the code. Added debug log statements in sign-in-up service and skipped certain test cases temporarily for evaluation.
Eliminate debug logging for workspace invite tokens and validation. This cleanup improves readability and reduces console clutter during runtime.
…ninup

# Conflicts:
#	packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts
…facto/improve-signinup

# Conflicts:
#	packages/twenty-server/src/engine/core-modules/auth/dto/available-workspaces.output.ts
#	packages/twenty-server/src/engine/core-modules/auth/services/auth.service.ts
#	packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts
#	packages/twenty-server/src/engine/core-modules/user/services/user.service.ts
#	packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts
Removed the redundant `code` property from WorkspaceInvitationException. Adjusted the import order in WorkspaceResolver to comply with coding standards.
Replace user object with user ID in saveDefaultWorkspace method call to fix incorrect parameter type. This ensures that the function operates as expected and prevents potential errors in default workspace assignment.
Deleted outdated user and workspace validation logic, and removed legacy verify-auth controller. Updated Microsoft and Google auth controllers to handle errors correctly and redirect. Added subdomain handling in various components to support multi-workspace setup.
Copy link

github-actions bot commented Nov 22, 2024

TODOs/FIXMEs:

  • // TODO: improve error management: packages/twenty-server/src/engine/core-modules/auth/controllers/sso-auth.controller.ts
  • // TODO: improve error management: packages/twenty-server/src/engine/core-modules/auth/controllers/sso-auth.controller.ts
  • // TODO AMOREAUX: change that with subdomains: packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts

Generated by 🚫 dangerJS against dab3129

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR implements comprehensive subdomain management for multi-workspace functionality across the Twenty application. Here's a focused summary of the key changes:

  • Added subdomain field to Workspace entity with unique constraint and migration scripts to populate existing workspaces with unique subdomains
  • Implemented workspace-specific authentication flows that validate and route based on subdomains, including SSO, Google, and Microsoft OAuth
  • Modified token management to support workspace-specific cookies and validation through new validateTokenByRequest method
  • Added domain settings page in workspace settings UI with subdomain configuration capabilities
  • Added workspace switching functionality that redirects users to appropriate workspace subdomains with proper authentication state

The changes appear well-structured but there are some critical issues to address:

  • Missing workspaceValidator imports in several auth controllers could cause runtime errors
  • Potential security concerns in OriginHeader decorator due to lack of validation
  • Duplicate workspace route in SettingsRoutes.tsx needs to be resolved

82 file(s) reviewed, 73 comment(s)
Edit PR Review Bot Settings | Greptile

packages/twenty-front/src/modules/auth/hooks/useAuth.ts Outdated Show resolved Hide resolved
defaultValue: null,
effects: [
cookieStorageEffect('lastAuthenticateWorkspace', {
domain: `.${getWorkspaceMainDomain()}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Leading dot in domain could prevent cookie from being set in some environments. Consider making it optional based on environment.

effects: [
cookieStorageEffect('lastAuthenticateWorkspace', {
domain: `.${getWorkspaceMainDomain()}`,
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365), // 1 year
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Cookie expiration calculated at state creation time rather than when cookie is set. Move calculation into effect.

});
it('should build workspace URL with workspace', () => {
const baseUrl = 'https://example.com';
const workspace = { subdomain: 'subdomain' } as unknown as Workspace;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Type casting to Workspace without proper mock data could hide type-related issues

Comment on lines 16 to 18
if (!domain) {
return false;
throw new Error('Invalid email format');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Redundant check - if fields[1] exists but is empty, it would have been caught by the length check on line 10

Comment on lines +24 to +28
try {
return !emailProvidersSet.has(getDomainNameByEmail(email));
} catch (err) {
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Silently catching all errors masks potential issues - consider logging errors in development

@@ -58,7 +58,7 @@ This uses the prebuilt images found on [docker hub](https://hub.docker.com/r/twe

- Is set in respective tf-files
- See docs [Setup Environment Variables](https://twenty.com/developers/section/self-hosting/self-hosting-var) for usage
- After deployment you could can set `IS_SIGN_UP_DISABLED=true` (and run `terraform plan/apply` again) to disable new workspaces from being created
- After deployment you could can set `IS_MULTIWORKSPACE_ENABLED=false` (and run `terraform plan/apply` again) to disable new workspaces from being created
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo in text: 'you could can' should be either 'you could' or 'you can'

Comment on lines 138 to 140

Upgrade your Twenty instance to use v0.33.0 image
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: heading format is inconsistent with other version headings - others use ## while this uses #

…ce' into feat/add-subdomain-management-for-multiworpsace
Replace manual URL handling with UrlManagerService across authentication and other core modules. This centralization improves maintainability and consistency by consolidating URL construction logic into a dedicated service.
@AMoreaux AMoreaux force-pushed the feat/add-subdomain-management-for-multiworpsace branch from dd5817d to c782207 Compare November 25, 2024 15:04
…at/add-subdomain-management-for-multiworpsace

# Conflicts:
#	packages/twenty-server/src/database/commands/upgrade-version/0-33/0-33-upgrade-version.module.ts
#	packages/twenty-server/src/database/typeorm-seeds/core/workspaces.ts
#	packages/twenty-server/src/engine/core-modules/billing/stripe/stripe.service.ts
#	packages/twenty-website/src/content/developers/self-hosting/cloud-providers.mdx
#	packages/twenty-website/src/content/developers/self-hosting/upgrade-guide.mdx
Removed the entire workspace-url.helper.ts file to consolidate URL management in a dedicated URL manager. Updated references throughout the project to use the new URL manager hooks and state, ensuring improved consistency and modularity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant