Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adicionando tag a endpoints nao tageados #158

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/guards/abstract.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ExecutionContext, HttpException } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { AccessLevel } from '@prisma/client';
import { canActivate } from './utils';

export abstract class AbstractGuard extends AuthGuard('jwt') {
abstract getAccessLevel(): AccessLevel[];
async canActivate(context: ExecutionContext): Promise<boolean> {
await super.canActivate(context);
const ok = await canActivate(context, this.getAccessLevel());
if (ok) return true;
throw new HttpException('Acesso não autorizado', 401);
}
}
19 changes: 5 additions & 14 deletions src/guards/staff.guard.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { AccessLevel } from '@prisma/client';

import { canActivate } from './utils';
import { AbstractGuard } from './abstract.guard';

@Injectable()
export class StaffGuard extends AuthGuard('jwt') {
export class StaffGuard extends AbstractGuard {
constructor() {
super();
}

async canActivate(context: ExecutionContext): Promise<boolean> {
await super.canActivate(context);
const ok = await canActivate(context, [
AccessLevel.Admin,
AccessLevel.Staff,
]);
if (ok) return true;

throw new HttpException('Acesso não autorizado', 401);
getAccessLevel(): AccessLevel[] {
return [AccessLevel.Staff, AccessLevel.Admin];
}
}
16 changes: 6 additions & 10 deletions src/guards/user.guard.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { AccessLevel } from '@prisma/client';

import { canActivate } from './utils';
import { AbstractGuard } from './abstract.guard';

@Injectable()
export class UserGuard extends AuthGuard('jwt') {
export class UserGuard extends AbstractGuard {
constructor() {
super();
}

async canActivate(context: ExecutionContext): Promise<boolean> {
await super.canActivate(context);
const ok = await canActivate(context, [
getAccessLevel(): AccessLevel[] {
return [
AccessLevel.User,
AccessLevel.Staff,
AccessLevel.DistributionCenter,
AccessLevel.Admin,
]);
if (ok) return true;
throw new HttpException('Acesso não autorizado', 401);
];
}
}
2 changes: 2 additions & 0 deletions src/partners/partners.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
import { PartnersService } from './partners.service';
import { ServerResponse } from '../utils';
import { AdminGuard } from '@/guards/admin.guard';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('Parceiros')
@Controller('partners')
export class PartnersController {
private logger = new Logger(PartnersController.name);
Expand Down
2 changes: 2 additions & 0 deletions src/supporters/supporters.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
import { SupportersService } from './supporters.service';
import { ServerResponse } from '../utils';
import { AdminGuard } from '@/guards/admin.guard';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('Apoiadores')
@Controller('supporters')
export class SupportersController {
private logger = new Logger(SupportersController.name);
Expand Down