From f9a136ab6de8340347bd8fe2eaa7e2fe81ba2dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bosi?= <71827178+bosiraphael@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:32:41 +0100 Subject: [PATCH] Update CanAccessMessageThreadService (#8388) Trying to fix #7830 --- .../can-access-message-thread.service.ts | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/packages/twenty-server/src/modules/messaging/common/query-hooks/message/can-access-message-thread.service.ts b/packages/twenty-server/src/modules/messaging/common/query-hooks/message/can-access-message-thread.service.ts index 0e30225843..19fdb153f1 100644 --- a/packages/twenty-server/src/modules/messaging/common/query-hooks/message/can-access-message-thread.service.ts +++ b/packages/twenty-server/src/modules/messaging/common/query-hooks/message/can-access-message-thread.service.ts @@ -1,12 +1,12 @@ import { ForbiddenException } from '@nestjs/common'; -import groupBy from 'lodash.groupby'; -import { Any } from 'typeorm'; +import { In } from 'typeorm'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity'; -import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity'; +import { MessageChannelMessageAssociationWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel-message-association.workspace-entity'; +import { MessageChannelVisibility } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity'; import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository'; import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity'; @@ -20,32 +20,12 @@ export class CanAccessMessageThreadService { public async canAccessMessageThread( userId: string, workspaceId: string, - messageChannelMessageAssociations: any[], + messageChannelMessageAssociations: MessageChannelMessageAssociationWorkspaceEntity[], ) { - const messageChannelRepository = - await this.twentyORMManager.getRepository( - 'messageChannel', - ); - const messageChannels = await messageChannelRepository.find({ - select: ['id', 'visibility'], - where: { - id: Any( - messageChannelMessageAssociations.map( - (association) => association.messageChannelId, - ), - ), - }, - }); - - const messageChannelsGroupByVisibility = groupBy( - messageChannels, - (channel) => channel.visibility, + const messageChannelIds = messageChannelMessageAssociations.map( + (association) => association.messageChannelId, ); - if (messageChannelsGroupByVisibility.SHARE_EVERYTHING) { - return; - } - const currentWorkspaceMember = await this.workspaceMemberRepository.getByIdOrFail(userId, workspaceId); @@ -55,17 +35,28 @@ export class CanAccessMessageThreadService { ); const connectedAccounts = await connectedAccountRepository.find({ - select: ['id'], - where: { - messageChannels: Any(messageChannels.map((channel) => channel.id)), - accountOwnerId: currentWorkspaceMember.id, + select: { + id: true, }, + where: [ + { + messageChannels: { + id: In(messageChannelIds), + visibility: MessageChannelVisibility.SHARE_EVERYTHING, + }, + }, + { + messageChannels: { + id: In(messageChannelIds), + }, + accountOwnerId: currentWorkspaceMember.id, + }, + ], + take: 1, }); - if (connectedAccounts.length > 0) { - return; + if (connectedAccounts.length === 0) { + throw new ForbiddenException(); } - - throw new ForbiddenException(); } }