chore: v2 dont allow mixing platform teams/users with non-platform (#20296)
* chore: v2 dont allow mixing platform teams/users with non-platform * chore: finish main merge * test: e2e * refactor: simplify can add user to team logic * refactor: fetch only needed oauth data * refactor: cleanup
This commit is contained in:
1 parent
d65c0753ef
commit
a2d0cbfe5b
8 files changed
+562
-7
No files matched your search
@@ -1,15 +1,24 @@
|
||||
import { OAuthClientRepository } from "@/modules/oauth-clients/oauth-client.repository";
|
||||
import { CreateTeamMembershipInput } from "@/modules/teams/memberships/inputs/create-team-membership.input";
|
||||
import { UpdateTeamMembershipInput } from "@/modules/teams/memberships/inputs/update-team-membership.input";
|
||||
import { TeamsMembershipsRepository } from "@/modules/teams/memberships/teams-memberships.repository";
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||
|
||||
import { TeamService } from "@calcom/platform-libraries";
|
||||
|
||||
export const PLATFORM_USER_BEING_ADDED_TO_REGULAR_TEAM_ERROR = `Can't add user to team - the user is platform managed user but team is not because team probably was not created using OAuth credentials.`;
|
||||
export const REGULAR_USER_BEING_ADDED_TO_PLATFORM_TEAM_ERROR = `Can't add user to team - the user is not platform managed user but team is platform managed. Both have to be created using OAuth credentials.`;
|
||||
export const PLATFORM_USER_AND_PLATFORM_TEAM_CREATED_WITH_DIFFERENT_OAUTH_CLIENTS_ERROR = `Can't add user to team - managed user and team were created using different OAuth clients.`;
|
||||
|
||||
@Injectable()
|
||||
export class TeamsMembershipsService {
|
||||
constructor(private readonly teamsMembershipsRepository: TeamsMembershipsRepository) {}
|
||||
constructor(
|
||||
private readonly teamsMembershipsRepository: TeamsMembershipsRepository,
|
||||
private readonly oAuthClientsRepository: OAuthClientRepository
|
||||
) {}
|
||||
|
||||
async createTeamMembership(teamId: number, data: CreateTeamMembershipInput) {
|
||||
await this.canUserBeAddedToTeam(data.userId, teamId);
|
||||
const teamMembership = await this.teamsMembershipsRepository.createTeamMembership(teamId, data);
|
||||
return teamMembership;
|
||||
}
|
||||
@@ -54,4 +63,29 @@ export class TeamsMembershipsService {
|
||||
|
||||
return teamMembership;
|
||||
}
|
||||
|
||||
async canUserBeAddedToTeam(userId: number, teamId: number) {
|
||||
const [userOAuthClient, teamOAuthClient] = await Promise.all([
|
||||
this.oAuthClientsRepository.getByUserId(userId),
|
||||
this.oAuthClientsRepository.getByTeamId(teamId),
|
||||
]);
|
||||
|
||||
if (!userOAuthClient && !teamOAuthClient) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (userOAuthClient && teamOAuthClient && userOAuthClient.id === teamOAuthClient.id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!teamOAuthClient) {
|
||||
throw new BadRequestException(PLATFORM_USER_BEING_ADDED_TO_REGULAR_TEAM_ERROR);
|
||||
}
|
||||
|
||||
if (!userOAuthClient) {
|
||||
throw new BadRequestException(REGULAR_USER_BEING_ADDED_TO_PLATFORM_TEAM_ERROR);
|
||||
}
|
||||
|
||||
throw new BadRequestException(PLATFORM_USER_AND_PLATFORM_TEAM_CREATED_WITH_DIFFERENT_OAUTH_CLIENTS_ERROR);
|
||||
}
|
||||
}
|
||||
@@ -12,5 +12,6 @@ import { Module } from "@nestjs/common";
|
||||
imports: [PrismaModule, RedisModule, OrganizationsModule, MembershipsModule, TeamsEventTypesModule],
|
||||
providers: [TeamsMembershipsRepository, TeamsMembershipsService],
|
||||
controllers: [TeamsMembershipsController],
|
||||
exports: [TeamsMembershipsService],
|
||||
})
|
||||
export class TeamsMembershipsModule {}
|
||||
Reference in new issue
Block a user