-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: IsTeamInOrg guard and decorator apiv2 (#15567)
- Loading branch information
1 parent
d431607
commit ec755b1
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
apps/api/v2/src/modules/auth/decorators/get-team/get-team.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ExecutionContext } from "@nestjs/common"; | ||
import { createParamDecorator } from "@nestjs/common"; | ||
|
||
import { Team } from "@calcom/prisma/client"; | ||
|
||
export type GetTeamReturnType = Team; | ||
|
||
export const GetTeam = createParamDecorator< | ||
keyof GetTeamReturnType | (keyof GetTeamReturnType)[], | ||
ExecutionContext | ||
>((data, ctx) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
const team = request.team as GetTeamReturnType; | ||
|
||
if (!team) { | ||
throw new Error("GetTeam decorator : Team not found"); | ||
} | ||
|
||
if (Array.isArray(data)) { | ||
return data.reduce((prev, curr) => { | ||
return { | ||
...prev, | ||
[curr]: team[curr], | ||
}; | ||
}, {}); | ||
} | ||
|
||
if (data) { | ||
return team[data]; | ||
} | ||
|
||
return team; | ||
}); |
33 changes: 33 additions & 0 deletions
33
apps/api/v2/src/modules/auth/guards/teams/is-team-in-org.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { OrganizationsRepository } from "@/modules/organizations/organizations.repository"; | ||
import { Injectable, CanActivate, ExecutionContext, ForbiddenException } from "@nestjs/common"; | ||
import { Request } from "express"; | ||
|
||
import { Team } from "@calcom/prisma/client"; | ||
|
||
@Injectable() | ||
export class IsTeamInOrg implements CanActivate { | ||
constructor(private organizationsRepository: OrganizationsRepository) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest<Request & { team: Team }>(); | ||
const teamId: string = request.params.teamId; | ||
const orgId: string = request.params.orgId; | ||
|
||
if (!orgId) { | ||
throw new ForbiddenException("No org id found in request params."); | ||
} | ||
|
||
if (!teamId) { | ||
throw new ForbiddenException("No team id found in request params."); | ||
} | ||
|
||
const team = await this.organizationsRepository.findOrgTeam(Number(orgId), Number(teamId)); | ||
|
||
if (team && !team.isOrganization && team.parentId === Number(orgId)) { | ||
request.team = team; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,10 @@ describe("Organizations Team Endpoints", () => { | |
|
||
let userRepositoryFixture: UserRepositoryFixture; | ||
let organizationsRepositoryFixture: TeamRepositoryFixture; | ||
let teamsRepositoryFixture: TeamRepositoryFixture; | ||
|
||
let org: Team; | ||
let team: Team; | ||
|
||
const userEmail = "[email protected]"; | ||
let user: User; | ||
|
@@ -37,6 +40,7 @@ describe("Organizations Team Endpoints", () => { | |
|
||
userRepositoryFixture = new UserRepositoryFixture(moduleRef); | ||
organizationsRepositoryFixture = new TeamRepositoryFixture(moduleRef); | ||
teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef); | ||
|
||
user = await userRepositoryFixture.create({ | ||
email: userEmail, | ||
|
@@ -48,6 +52,12 @@ describe("Organizations Team Endpoints", () => { | |
isOrganization: true, | ||
}); | ||
|
||
team = await teamsRepositoryFixture.create({ | ||
name: "Test org team", | ||
isOrganization: false, | ||
parent: { connect: { id: org.id } }, | ||
}); | ||
|
||
app = moduleRef.createNestApplication(); | ||
bootstrap(app as NestExpressApplication); | ||
|
||
|
@@ -72,6 +82,26 @@ describe("Organizations Team Endpoints", () => { | |
}); | ||
}); | ||
|
||
it("should fail if org does not exist", async () => { | ||
return request(app.getHttpServer()).get(`/v2/organizations/120494059/teams`).expect(403); | ||
}); | ||
|
||
it("should get the team of the org", async () => { | ||
return request(app.getHttpServer()) | ||
.get(`/v2/organizations/${org.id}/teams/${team.id}`) | ||
.expect(200) | ||
.then((response) => { | ||
const responseBody: ApiSuccessResponse<Team> = response.body; | ||
expect(responseBody.status).toEqual(SUCCESS_STATUS); | ||
expect(responseBody.data.id).toEqual(team.id); | ||
expect(responseBody.data.parentId).toEqual(team.parentId); | ||
}); | ||
}); | ||
|
||
it("should fail if the team does not exist", async () => { | ||
return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams/123132145`).expect(403); | ||
}); | ||
|
||
afterAll(async () => { | ||
await userRepositoryFixture.deleteByEmail(user.email); | ||
await organizationsRepositoryFixture.delete(org.id); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters