Skip to content

Commit f1416e5

Browse files
kelvinsbHelioDantas
authored andcommitted
feat: only show contact on shelter public routes on authorized roles
1 parent 1210f82 commit f1416e5

File tree

6 files changed

+62
-19
lines changed

6 files changed

+62
-19
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { UserDecorator } from './user.decorator';
2+
3+
export { UserDecorator };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
2+
3+
export const UserDecorator = createParamDecorator(
4+
(data: unknown, ctx: ExecutionContext) => {
5+
const request = ctx.switchToHttp().getRequest();
6+
return request?.user;
7+
},
8+
);

src/guards/apply-user.guard.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { AuthGuard } from '@nestjs/passport';
3+
4+
@Injectable()
5+
export class ApplyUser extends AuthGuard('jwt') {
6+
handleRequest(err: any, user: any) {
7+
if (user) return user;
8+
return null;
9+
}
10+
}

src/guards/utils.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,34 @@ async function canActivate(context: ExecutionContext, allowed: AccessLevel[]) {
1111
if (request.user) {
1212
const { userId, sessionId } = request.user;
1313

14-
const session = await service.session.findUnique({
15-
where: { id: sessionId, active: true, user: { id: userId } },
16-
include: {
17-
user: true,
18-
},
19-
});
20-
21-
if (
22-
session &&
23-
allowed.some((permission) => permission === session.user.accessLevel)
24-
) {
25-
return true;
26-
}
14+
return isRightSessionRole(allowed, sessionId, userId);
2715
}
2816

2917
return false;
3018
}
3119

32-
export { canActivate };
20+
async function isRightSessionRole(
21+
allowed: AccessLevel[],
22+
sessionId?: string,
23+
userId?: string,
24+
) {
25+
if (!sessionId) return false;
26+
if (!userId) return false;
27+
28+
const session = await service.session.findUnique({
29+
where: { id: sessionId, active: true, user: { id: userId } },
30+
include: {
31+
user: true,
32+
},
33+
});
34+
35+
if (
36+
session &&
37+
allowed.some((permission) => permission === session.user.accessLevel)
38+
) {
39+
return true;
40+
}
41+
return false;
42+
}
43+
44+
export { canActivate, isRightSessionRole };

src/shelter/shelter.controller.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { ApiTags } from '@nestjs/swagger';
1515
import { ShelterService } from './shelter.service';
1616
import { ServerResponse } from '../utils';
1717
import { StaffGuard } from '@/guards/staff.guard';
18+
import { ApplyUser } from '@/guards/apply-user.guard';
19+
import { UserDecorator } from '@/decorators/UserDecorator';
1820

1921
@ApiTags('Abrigos')
2022
@Controller('shelters')
@@ -35,9 +37,10 @@ export class ShelterController {
3537
}
3638

3739
@Get(':id')
38-
async show(@Param('id') id: string) {
40+
@UseGuards(ApplyUser)
41+
async show(@UserDecorator() user: any, @Param('id') id: string) {
3942
try {
40-
const data = await this.shelterService.show(id);
43+
const data = await this.shelterService.show(id, user);
4144
return new ServerResponse(200, 'Successfully get shelter', data);
4245
} catch (err: any) {
4346
this.logger.error(`Failed to get shelter: ${err}`);

src/shelter/shelter.service.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { z } from 'zod';
22
import { Injectable } from '@nestjs/common';
33
import * as qs from 'qs';
4-
import { Prisma } from '@prisma/client';
4+
import { Prisma, AccessLevel } from '@prisma/client';
55
import { DefaultArgs } from '@prisma/client/runtime/library';
66

77
import { PrismaService } from '../prisma/prisma.service';
@@ -14,6 +14,7 @@ import { SearchSchema } from '../types';
1414
import { ShelterSearch, parseTagResponse } from './ShelterSearch';
1515
import { SupplyPriority } from '../supply/types';
1616
import { IFilterFormProps } from './types/search.types';
17+
import { isRightSessionRole } from '@/guards/utils';
1718

1819
@Injectable()
1920
export class ShelterService {
@@ -60,7 +61,13 @@ export class ShelterService {
6061
});
6162
}
6263

63-
async show(id: string) {
64+
async show(id: string, user: any) {
65+
const isLogged = await isRightSessionRole(
66+
[AccessLevel.User, AccessLevel.Staff],
67+
user?.sessionId,
68+
user?.userId,
69+
);
70+
6471
const data = await this.prismaService.shelter.findFirst({
6572
where: {
6673
id,
@@ -72,7 +79,7 @@ export class ShelterService {
7279
pix: true,
7380
shelteredPeople: true,
7481
capacity: true,
75-
contact: true,
82+
contact: isLogged,
7683
petFriendly: true,
7784
prioritySum: true,
7885
latitude: true,

0 commit comments

Comments
 (0)