Skip to content

Commit f2f112f

Browse files
committed
feat: created admin user role
1 parent caf62ea commit f2f112f

File tree

11 files changed

+42
-15
lines changed

11 files changed

+42
-15
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterEnum
2+
ALTER TYPE "AccessLevel" ADD VALUE 'Admin';

prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum AccessLevel {
1111
User
1212
Staff
1313
DistributionCenter
14+
Admin
1415
}
1516

1617
model User {

src/guards/admin.guard.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 AdminGuard 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, [AccessLevel.Admin]);
16+
if (ok) return true;
17+
18+
throw new HttpException('Acesso não autorizado', 401);
19+
}
20+
}

src/guards/distribution-center.guard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class DistributionCenterGuard extends AuthGuard('jwt') {
1313
async canActivate(context: ExecutionContext): Promise<boolean> {
1414
await super.canActivate(context);
1515
const ok = await canActivate(context, [
16-
AccessLevel.Staff,
16+
AccessLevel.Admin,
1717
AccessLevel.DistributionCenter,
1818
]);
1919
if (ok) return true;

src/guards/staff.guard.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export class StaffGuard extends AuthGuard('jwt') {
1212

1313
async canActivate(context: ExecutionContext): Promise<boolean> {
1414
await super.canActivate(context);
15-
const ok = await canActivate(context, [AccessLevel.Staff]);
15+
const ok = await canActivate(context, [
16+
AccessLevel.Admin,
17+
AccessLevel.Staff,
18+
]);
1619
if (ok) return true;
1720

1821
throw new HttpException('Acesso não autorizado', 401);

src/guards/user.guard.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export class UserGuard extends AuthGuard('jwt') {
1313
async canActivate(context: ExecutionContext): Promise<boolean> {
1414
await super.canActivate(context);
1515
const ok = await canActivate(context, [
16-
AccessLevel.Staff,
1716
AccessLevel.User,
17+
AccessLevel.Staff,
18+
AccessLevel.DistributionCenter,
19+
AccessLevel.Admin,
1820
]);
1921
if (ok) return true;
2022
throw new HttpException('Acesso não autorizado', 401);

src/shelter-managers/shelter-managers.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ApiTags } from '@nestjs/swagger';
1313

1414
import { ShelterManagersService } from './shelter-managers.service';
1515
import { ServerResponse } from '../utils';
16-
import { StaffGuard } from '@/guards/staff.guard';
16+
import { AdminGuard } from '@/guards/admin.guard';
1717

1818
@ApiTags('Admin de Abrigo')
1919
@Controller('shelter/managers')
@@ -25,7 +25,7 @@ export class ShelterManagersController {
2525
) {}
2626

2727
@Post('')
28-
@UseGuards(StaffGuard)
28+
@UseGuards(AdminGuard)
2929
async store(@Body() body) {
3030
try {
3131
await this.shelterManagerServices.store(body);

src/shelter/ShelterSearch.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ class ShelterSearch {
122122
}
123123

124124
get query(): Prisma.ShelterWhereInput {
125-
console.log(this.formProps);
126125
if (Object.keys(this.formProps).length === 0) return {};
127126
const queryData = {
128127
AND: [

src/shelter/shelter.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ApiTags } from '@nestjs/swagger';
1414

1515
import { ShelterService } from './shelter.service';
1616
import { ServerResponse } from '../utils';
17-
import { UserGuard } from '@/guards/user.guard';
17+
import { StaffGuard } from '@/guards/staff.guard';
1818

1919
@ApiTags('Abrigos')
2020
@Controller('shelters')
@@ -46,7 +46,7 @@ export class ShelterController {
4646
}
4747

4848
@Post('')
49-
@UseGuards(UserGuard)
49+
@UseGuards(StaffGuard)
5050
async store(@Body() body) {
5151
try {
5252
const data = await this.shelterService.store(body);
@@ -69,7 +69,7 @@ export class ShelterController {
6969
}
7070

7171
@Put(':id/admin')
72-
@UseGuards(UserGuard)
72+
@UseGuards(StaffGuard)
7373
async fullUpdate(@Param('id') id: string, @Body() body) {
7474
try {
7575
const data = await this.shelterService.fullUpdate(id, body);

src/supply-categories/supply-categories.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ApiTags } from '@nestjs/swagger';
1313

1414
import { SupplyCategoriesService } from './supply-categories.service';
1515
import { ServerResponse } from '../utils';
16-
import { StaffGuard } from '@/guards/staff.guard';
16+
import { AdminGuard } from '@/guards/admin.guard';
1717

1818
@ApiTags('Categoria de Suprimentos')
1919
@Controller('supply-categories')
@@ -40,7 +40,7 @@ export class SupplyCategoriesController {
4040
}
4141

4242
@Post('')
43-
@UseGuards(StaffGuard)
43+
@UseGuards(AdminGuard)
4444
async store(@Body() body) {
4545
try {
4646
const data = await this.supplyCategoryServices.store(body);
@@ -56,7 +56,7 @@ export class SupplyCategoriesController {
5656
}
5757

5858
@Put(':id')
59-
@UseGuards(StaffGuard)
59+
@UseGuards(AdminGuard)
6060
async update(@Param('id') id: string, @Body() body) {
6161
try {
6262
const data = await this.supplyCategoryServices.update(id, body);

0 commit comments

Comments
 (0)