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

refacto(*): remove everything about default workspace #9157

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

AMoreaux
Copy link
Contributor

@AMoreaux AMoreaux commented Dec 19, 2024

Summary

  • Remove defaultWorkspace in user
  • Remove all occurrence of defaultWorkspace and defaultWorkspaceId
  • Improve activate workspace flow
  • Improve security on social login
  • Add ImpersonateGuard
  • Allow to use impersonation with couple User/Workspace
  • Prevent unexpected reload on activate workspace
  • Scope login token with workspaceId

Replaced `Verify` and `CurrentUserOutput` dto usages with direct use of `AuthTokens` and `User` entities, respectively. Simplified related GraphQL queries, mutations, and services to reflect the updated data model. Enhanced code readability and reduced redundancy by removing unnecessary intermediate representations.
@AMoreaux AMoreaux self-assigned this Dec 19, 2024
…hing-about-defaultWorkspaceId

# Conflicts:
#	packages/twenty-server/test/integration/metadata/suites/object-metadata/rename-custom-object.integration-spec.ts
Removed `ActivateWorkspaceOutput` in favor of directly returning `Workspace` for cleaner API responses. Consolidated workspace fetching logic into `getWorkspaceByOriginOrDefaultWorkspace` to reduce duplication and improve maintainability. Updated related queries, mutations, and frontend logic to reflect these changes.
Copy link

github-actions bot commented Dec 20, 2024

TODOs/FIXMEs:

  • // TODO: factorize with UserProviderEffect: packages/twenty-front/src/modules/auth/hooks/useAuth.ts
  • // TODO: factorize with UserProviderEffect: packages/twenty-front/src/modules/auth/hooks/useAuth.ts
  • // TODO AMOREAUX: this logger is trigger twice and the second time the message is undefined for an unknown reason: packages/twenty-server/src/engine/core-modules/domain-manager/service/domain-manager.service.ts

Generated by 🚫 dangerJS against 4400f5e

Unifies impersonation functionality and feature flag management under a single admin panel component. Replaces the standalone impersonation component and integrates updated GraphQL types and APIs, improving handling of login tokens and workspace redirections.
@AMoreaux AMoreaux marked this pull request as ready for review December 20, 2024 15:27
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 a significant architectural change by removing the concept of default workspaces and improving workspace activation flows. The changes span across both frontend and backend codebases.

Key changes:

  • Removes defaultWorkspaceId from User entity and all related references throughout the codebase
  • Introduces new ImpersonateGuard with workspace-specific impersonation requiring both userId and workspaceId
  • Replaces defaultWorkspace with currentWorkspace in user-related components and services
  • Adds domain manager service with improved workspace lookup and validation
  • Simplifies workspace activation flow by removing automatic workspace assignment and login token generation

Potential issues:

  • Migration's down function adds NOT NULL constraint without handling existing data
  • Some impersonation security checks may be incomplete (workspace-level permissions)
  • Removal of OAuthService needs careful testing of authentication flows

72 file(s) reviewed, 29 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +181 to +183
if (!user) {
throw new Error('No current user result');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style: this error should include more context about why the current user result is missing to help with debugging

Comment on lines +352 to +360
if (isMultiWorkspaceEnabled) {
return redirectToWorkspaceDomain(
signUpResult.data.signUp.workspace.subdomain,
AppPath.Verify,
{
loginToken: signUpResult.data.signUp.loginToken.token,
},
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: no error handling for failed workspace domain redirects - should catch and handle redirect failures

Comment on lines +56 to +58
if (isDefined(queryData.currentUser.currentWorkspace)) {
setCurrentWorkspace(queryData.currentUser.currentWorkspace);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style: consider handling the case where currentWorkspace is undefined - may need to select first available workspace or show workspace selector

Comment on lines 75 to 77
if (isDefined(result.errors)) {
throw result.errors ?? new Error('Unknown error');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Error handling logic is incorrect - result.errors check should come before loadCurrentUser() call to prevent executing it when there are errors

Comment on lines +105 to 107
await this.workspaceRepository.update(workspace.id, {
activationStatus: WorkspaceActivationStatus.ONGOING_CREATION,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider wrapping workspace status updates and initialization in a transaction to ensure atomicity

workspace,
loginToken,
};
workspaceValidator.assertIsDefinedOrThrow(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: should pass a WorkspaceException like on line 194 to provide better error handling

Comment on lines +11 to +13
const request = ctx.getContext().req;

return request.user.canImpersonate === true;
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Missing null check on request.user could cause runtime error if authentication fails

const ctx = GqlExecutionContext.create(context);
const request = ctx.getContext().req;

return request.user.canImpersonate === true;
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Should also check workspace.allowImpersonation before allowing impersonation

Comment on lines +41 to +44
exceptionToThrowCustom: AuthException = new AuthException(
`${provider} auth is not enabled for this workspace`,
AuthExceptionCode.OAUTH_ACCESS_DENIED,
),
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Default exception message includes template literal that could expose internal details. Consider using a static message instead.

Renamed `SettingsAdminFeatureFlags` to `SettingsAdminContent` for clarity and updated associated routes. Enhanced impersonation checks by adding a condition to ensure the workspace allows impersonation. This improves code readability and adds stricter validation for impersonation actions.
Moved SettingsAdminContent to a more appropriate module directory for improved project structure and maintainability. Updated all import paths to reflect the new location.
The guard no longer checks for `workspace.allowImpersonation` as it is unnecessary. Impersonation access is now solely determined by the user's permissions. This simplifies the logic and avoids potential misconfigurations.
Added allowImpersonation property to workspace entities in both front-end and back-end. This enables tracking of whether impersonation is allowed for each workspace in user lookup operations.
Enhance login token generation by including workspace ID, ensuring tokens are tied to specific workspaces. Updated `verifyLoginToken` to validate workspace ID, adding stricter access control. Adjusted all relevant controllers and services to support this feature.
Added support for passing the workspace ID when generating login tokens. This enhances token specificity and aligns with multi-workspace authentication requirements. Adjusted controller and token service to handle the additional parameter.
Removed the defaultWorkspaceId column and its associated constraints from database migrations. Additionally, added an import in workspace.entity.ts to address circular dependency issues.
Added an explanatory comment detailing the specific circular dependency path to improve code readability and maintenance. This change does not affect functionality but aims to assist future developers in understanding the issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant