Skip to content

Commit ec755b1

Browse files
chore: IsTeamInOrg guard and decorator apiv2 (#15567)
1 parent d431607 commit ec755b1

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ExecutionContext } from "@nestjs/common";
2+
import { createParamDecorator } from "@nestjs/common";
3+
4+
import { Team } from "@calcom/prisma/client";
5+
6+
export type GetTeamReturnType = Team;
7+
8+
export const GetTeam = createParamDecorator<
9+
keyof GetTeamReturnType | (keyof GetTeamReturnType)[],
10+
ExecutionContext
11+
>((data, ctx) => {
12+
const request = ctx.switchToHttp().getRequest();
13+
const team = request.team as GetTeamReturnType;
14+
15+
if (!team) {
16+
throw new Error("GetTeam decorator : Team not found");
17+
}
18+
19+
if (Array.isArray(data)) {
20+
return data.reduce((prev, curr) => {
21+
return {
22+
...prev,
23+
[curr]: team[curr],
24+
};
25+
}, {});
26+
}
27+
28+
if (data) {
29+
return team[data];
30+
}
31+
32+
return team;
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { OrganizationsRepository } from "@/modules/organizations/organizations.repository";
2+
import { Injectable, CanActivate, ExecutionContext, ForbiddenException } from "@nestjs/common";
3+
import { Request } from "express";
4+
5+
import { Team } from "@calcom/prisma/client";
6+
7+
@Injectable()
8+
export class IsTeamInOrg implements CanActivate {
9+
constructor(private organizationsRepository: OrganizationsRepository) {}
10+
11+
async canActivate(context: ExecutionContext): Promise<boolean> {
12+
const request = context.switchToHttp().getRequest<Request & { team: Team }>();
13+
const teamId: string = request.params.teamId;
14+
const orgId: string = request.params.orgId;
15+
16+
if (!orgId) {
17+
throw new ForbiddenException("No org id found in request params.");
18+
}
19+
20+
if (!teamId) {
21+
throw new ForbiddenException("No team id found in request params.");
22+
}
23+
24+
const team = await this.organizationsRepository.findOrgTeam(Number(orgId), Number(teamId));
25+
26+
if (team && !team.isOrganization && team.parentId === Number(orgId)) {
27+
request.team = team;
28+
return true;
29+
}
30+
31+
return false;
32+
}
33+
}

apps/api/v2/src/modules/organizations/controllers/organizations-teams.controller.e2e-spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ describe("Organizations Team Endpoints", () => {
2222

2323
let userRepositoryFixture: UserRepositoryFixture;
2424
let organizationsRepositoryFixture: TeamRepositoryFixture;
25+
let teamsRepositoryFixture: TeamRepositoryFixture;
26+
2527
let org: Team;
28+
let team: Team;
2629

2730
const userEmail = "org-teams-controller-e2e@api.com";
2831
let user: User;
@@ -37,6 +40,7 @@ describe("Organizations Team Endpoints", () => {
3740

3841
userRepositoryFixture = new UserRepositoryFixture(moduleRef);
3942
organizationsRepositoryFixture = new TeamRepositoryFixture(moduleRef);
43+
teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef);
4044

4145
user = await userRepositoryFixture.create({
4246
email: userEmail,
@@ -48,6 +52,12 @@ describe("Organizations Team Endpoints", () => {
4852
isOrganization: true,
4953
});
5054

55+
team = await teamsRepositoryFixture.create({
56+
name: "Test org team",
57+
isOrganization: false,
58+
parent: { connect: { id: org.id } },
59+
});
60+
5161
app = moduleRef.createNestApplication();
5262
bootstrap(app as NestExpressApplication);
5363

@@ -72,6 +82,26 @@ describe("Organizations Team Endpoints", () => {
7282
});
7383
});
7484

85+
it("should fail if org does not exist", async () => {
86+
return request(app.getHttpServer()).get(`/v2/organizations/120494059/teams`).expect(403);
87+
});
88+
89+
it("should get the team of the org", async () => {
90+
return request(app.getHttpServer())
91+
.get(`/v2/organizations/${org.id}/teams/${team.id}`)
92+
.expect(200)
93+
.then((response) => {
94+
const responseBody: ApiSuccessResponse<Team> = response.body;
95+
expect(responseBody.status).toEqual(SUCCESS_STATUS);
96+
expect(responseBody.data.id).toEqual(team.id);
97+
expect(responseBody.data.parentId).toEqual(team.parentId);
98+
});
99+
});
100+
101+
it("should fail if the team does not exist", async () => {
102+
return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams/123132145`).expect(403);
103+
});
104+
75105
afterAll(async () => {
76106
await userRepositoryFixture.deleteByEmail(user.email);
77107
await organizationsRepositoryFixture.delete(org.id);

apps/api/v2/src/modules/organizations/controllers/organizations-teams.controller.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { API_VERSIONS_VALUES } from "@/lib/api-versions";
22
import { GetOrg } from "@/modules/auth/decorators/get-org/get-org.decorator";
3+
import { GetTeam } from "@/modules/auth/decorators/get-team/get-team.decorator";
34
import { ApiAuthGuard } from "@/modules/auth/guards/api-auth/api-auth.guard";
45
import { IsOrgGuard } from "@/modules/auth/guards/organizations/is-org.guard";
6+
import { IsTeamInOrg } from "@/modules/auth/guards/teams/is-team-in-org.guard";
57
import { Controller, UseGuards, Get, Param, ParseIntPipe } from "@nestjs/common";
68
import { ApiTags as DocsTags } from "@nestjs/swagger";
79

@@ -28,4 +30,20 @@ export class OrganizationsTeamsController {
2830
data: [],
2931
};
3032
}
33+
34+
@UseGuards(IsTeamInOrg)
35+
@Get("/:teamId")
36+
async getTeam(
37+
@Param("orgId", ParseIntPipe) orgId: number,
38+
@Param("teamId", ParseIntPipe) teamId: number,
39+
@GetOrg() organization: Team,
40+
@GetTeam() team: Team,
41+
@GetOrg("name") orgName: string
42+
): Promise<ApiResponse<Team>> {
43+
console.log(teamId, orgId, organization, team, orgName);
44+
return {
45+
status: SUCCESS_STATUS,
46+
data: team,
47+
};
48+
}
3149
}

apps/api/v2/src/modules/organizations/organizations.repository.ts

+10
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,14 @@ export class OrganizationsRepository {
7070
},
7171
});
7272
}
73+
74+
async findOrgTeam(organizationId: number, teamId: number) {
75+
return this.dbRead.prisma.team.findUnique({
76+
where: {
77+
id: teamId,
78+
isOrganization: false,
79+
parentId: organizationId,
80+
},
81+
});
82+
}
7383
}

0 commit comments

Comments
 (0)