Skip to content

Commit 9613b9d

Browse files
authored
chore: IsUserInOrd guard (#15579)
1 parent ec755b1 commit 9613b9d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
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 IsUserInOrg 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 orgId: string = request.params.orgId;
14+
const userId: string = request.params.userId;
15+
16+
if (!userId) {
17+
throw new ForbiddenException("No user id found in request params.");
18+
}
19+
20+
if (!orgId) {
21+
throw new ForbiddenException("No org id found in request params.");
22+
}
23+
24+
const user = await this.organizationsRepository.findOrgUser(Number(orgId), Number(userId));
25+
26+
if (user) {
27+
request.user = user;
28+
return true;
29+
}
30+
31+
return false;
32+
}
33+
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,13 @@ export class OrganizationsRepository {
8080
},
8181
});
8282
}
83+
84+
async findOrgUser(organizationId: number, userId: number) {
85+
return this.dbRead.prisma.user.findUnique({
86+
where: {
87+
id: userId,
88+
organizationId,
89+
},
90+
});
91+
}
8392
}

0 commit comments

Comments
 (0)