Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit fb51638

Browse files
committed
feat: Add AdminGuard for role-based access control in UserController, restricting access to admin users
1 parent afc6c55 commit fb51638

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/auth/guards/admin.guard.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
2+
import { CanActivate } from '@nestjs/common';
3+
import { UserRole } from '../../user/user.types';
4+
5+
@Injectable()
6+
export class AdminGuard implements CanActivate {
7+
canActivate(context: ExecutionContext): boolean {
8+
const request = context.switchToHttp().getRequest();
9+
const user = request.user;
10+
11+
// Если пользователь не найден, выбрасываем 401
12+
if (!user) {
13+
throw new UnauthorizedException('Не авторизован');
14+
}
15+
16+
// Если пользователь не админ, выбрасываем 401
17+
if (user.role !== UserRole.ADMIN) {
18+
throw new UnauthorizedException('Доступ разрешен только администраторам');
19+
}
20+
21+
return true;
22+
}
23+
}
24+

src/user/user.controller.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { UpdateUserV2Dto, updateUserV2Schema, UpdateUserV2DtoClass } from './dto
1818
import { ChangePasswordDto, changePasswordSchema, ChangePasswordDtoClass } from './dto/change-password.dto';
1919
import { ZodValidation } from '../common/decorators/zod-validation.decorator';
2020
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
21+
import { AdminGuard } from '../auth/guards/admin.guard';
2122
import { CurrentUser } from '../auth/decorators/current-user.decorator';
2223

2324
@ApiTags('Пользователи')
@@ -37,11 +38,11 @@ export class UserController {
3738
}
3839

3940
@Get()
40-
@UseGuards(JwtAuthGuard)
41+
@UseGuards(JwtAuthGuard, AdminGuard)
4142
@ApiBearerAuth()
4243
@ApiOperation({ summary: 'Получить всех пользователей' })
4344
@ApiResponse({ status: 200, description: 'Список пользователей' })
44-
@ApiResponse({ status: 401, description: 'Не авторизован' })
45+
@ApiResponse({ status: 401, description: 'Не авторизован или недостаточно прав' })
4546
findAll() {
4647
return this.userService.findAll();
4748
}
@@ -63,12 +64,12 @@ export class UserController {
6364
}
6465

6566
@Get(':id')
66-
@UseGuards(JwtAuthGuard)
67+
@UseGuards(JwtAuthGuard, AdminGuard)
6768
@ApiBearerAuth()
6869
@ApiOperation({ summary: 'Получить пользователя по ID' })
6970
@ApiResponse({ status: 200, description: 'Пользователь найден' })
7071
@ApiResponse({ status: 404, description: 'Пользователь не найден' })
71-
@ApiResponse({ status: 401, description: 'Не авторизован' })
72+
@ApiResponse({ status: 401, description: 'Не авторизован или недостаточно прав' })
7273
findOne(@Param('id', ParseIntPipe) id: number) {
7374
return this.userService.findOne(id);
7475
}
@@ -104,12 +105,12 @@ export class UserController {
104105
}
105106

106107
@Delete(':id')
107-
@UseGuards(JwtAuthGuard)
108+
@UseGuards(JwtAuthGuard, AdminGuard)
108109
@ApiBearerAuth()
109110
@ApiOperation({ summary: 'Удалить пользователя' })
110111
@ApiResponse({ status: 200, description: 'Пользователь удален' })
111112
@ApiResponse({ status: 404, description: 'Пользователь не найден' })
112-
@ApiResponse({ status: 401, description: 'Не авторизован' })
113+
@ApiResponse({ status: 401, description: 'Не авторизован или недостаточно прав' })
113114
remove(@Param('id', ParseIntPipe) id: number) {
114115
return this.userService.remove(id);
115116
}

0 commit comments

Comments
 (0)