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

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

Fix https://github.com/twentyhq/twenty/issues/9033#event-15714863042
This commit is contained in:
Antoine Moreaux
2024-12-24 12:47:41 +01:00
committed by GitHub
parent fe6948ba0b
commit cd2946b670
78 changed files with 1150 additions and 1244 deletions

View File

@ -0,0 +1,11 @@
import { CustomException } from 'src/utils/custom-exception';
export class DomainManagerException extends CustomException {
constructor(message: string, code: DomainManagerExceptionCode) {
super(message, code);
}
}
export enum DomainManagerExceptionCode {
SUBDOMAIN_REQUIRED = 'SUBDOMAIN_REQUIRED',
}

View File

@ -117,6 +117,14 @@ export class DomainManagerService {
return subdomain;
};
async getWorkspaceBySubdomainOrDefaultWorkspace(subdomain?: string) {
return subdomain
? await this.workspaceRepository.findOne({
where: { subdomain },
})
: await this.getDefaultWorkspace();
}
isDefaultSubdomain(subdomain: string) {
return subdomain === this.environmentService.get('DEFAULT_SUBDOMAIN');
}
@ -137,7 +145,7 @@ export class DomainManagerService {
return url.toString();
}
async getDefaultWorkspace() {
private async getDefaultWorkspace() {
if (!this.environmentService.get('IS_MULTIWORKSPACE_ENABLED')) {
const workspaces = await this.workspaceRepository.find({
order: {
@ -147,7 +155,6 @@ export class DomainManagerService {
});
if (workspaces.length > 1) {
// TODO AMOREAUX: this logger is trigger twice and the second time the message is undefined for an unknown reason
Logger.warn(
`In single-workspace mode, there should be only one workspace. Today there are ${workspaces.length} workspaces`,
);
@ -161,7 +168,7 @@ export class DomainManagerService {
);
}
async getWorkspaceByOrigin(origin: string) {
async getWorkspaceByOriginOrDefaultWorkspace(origin: string) {
try {
if (!this.environmentService.get('IS_MULTIWORKSPACE_ENABLED')) {
return this.getDefaultWorkspace();
@ -171,12 +178,10 @@ export class DomainManagerService {
if (!isDefined(subdomain)) return;
const workspace = await this.workspaceRepository.findOne({
return await this.workspaceRepository.findOne({
where: { subdomain },
relations: ['workspaceSSOIdentityProviders'],
});
return workspace;
} catch (e) {
throw new WorkspaceException(
'Workspace not found',