Skip to content

Commit caf62ea

Browse files
committed
wip
1 parent 0248869 commit caf62ea

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common';
2+
import { AuthGuard } from '@nestjs/passport';
3+
import { AccessLevel } from '@prisma/client';
4+
5+
import { canActivate } from './utils';
6+
7+
@Injectable()
8+
export class DistributionCenterGuard extends AuthGuard('jwt') {
9+
constructor() {
10+
super();
11+
}
12+
13+
async canActivate(context: ExecutionContext): Promise<boolean> {
14+
await super.canActivate(context);
15+
const ok = await canActivate(context, [
16+
AccessLevel.Staff,
17+
AccessLevel.DistributionCenter,
18+
]);
19+
if (ok) return true;
20+
21+
throw new HttpException('Acesso não autorizado', 401);
22+
}
23+
}

src/shelter-supply/shelter-supply.controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {
77
Param,
88
Post,
99
Put,
10+
UseGuards,
1011
} from '@nestjs/common';
1112
import { ApiTags } from '@nestjs/swagger';
1213

1314
import { ShelterSupplyService } from './shelter-supply.service';
1415
import { ServerResponse } from '../utils';
16+
import { DistributionCenterGuard } from '@/guards/distribution-center.guard';
1517

1618
@ApiTags('Suprimento de abrigos')
1719
@Controller('shelter/supplies')
@@ -69,6 +71,7 @@ export class ShelterSupplyController {
6971
}
7072

7173
@Put(':shelterId/supplies/many')
74+
@UseGuards(DistributionCenterGuard)
7275
async updateMany(@Body() body, @Param('shelterId') shelterId: string) {
7376
try {
7477
const data = await this.shelterSupplyService.updateMany({

0 commit comments

Comments
 (0)