/
andreas300000
/
webpinger-backend
Обзор
Документация
Войти
/
andreas300000
/
webpinger-backend
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/core/groups/groups.controller.ts
47 строк
2 KB
ILYA Loginov
SPRAV: check "admin" for post/patch/delete
02 июн 2025, 23:53
02 июн 2025, 23:53
b309e23
Код
Авторство
О чём код?
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, UseGuards, ForbiddenException, } from '@nestjs/common'; import { GroupsService } from './groups.service'; import { CreateGroupDto } from './dto/create-group.dto'; import { UpdateGroupDto } from './dto/update-group.dto'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { GetUser } from '../auth/get-user.decorator'; import { UserWithRoles } from '../users/entities/user.entity'; @UseGuards(JwtAuthGuard) @Controller('groups') export class GroupsController { constructor(private readonly groupsService: GroupsService) { } @Post() async create(@GetUser() user: UserWithRoles, @Body() createGroupDto: CreateGroupDto) { if (user.roles?.name.toLowerCase() !== 'admin') { throw new ForbiddenException('Permission denied'); } return await this.groupsService.create(createGroupDto); } @Get() async findAll() { return await this.groupsService.findAll(); } @Get(':id') async findOne(@Param('id', ParseIntPipe) id: string) { return await this.groupsService.findOne(+id); } @Patch(':id') async update(@GetUser() user: UserWithRoles, @Param('id', ParseIntPipe) id: string, @Body() updateGroupDto: UpdateGroupDto) { if (user.roles?.name.toLowerCase() !== 'admin') { throw new ForbiddenException('Permission denied'); } return await this.groupsService.update(+id, updateGroupDto); } @Delete(':id') async remove(@GetUser() user: UserWithRoles, @Param('id', ParseIntPipe) id: string) { if (user.roles?.name.toLowerCase() !== 'admin') { throw new ForbiddenException('Permission denied'); } return await this.groupsService.remove(+id); } }