From c502fd86d7dd3fa1bbe1a78e75544c8e4b7587ee Mon Sep 17 00:00:00 2001 From: Mahdi Date: Mon, 25 May 2026 20:51:19 +0330 Subject: [PATCH] add password in admin --- .../users/dto/admin-set-user-password.dto.ts | 16 +++++++++++++ src/modules/users/users.controller.ts | 9 ++++++++ src/modules/users/users.service.ts | 23 ++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/modules/users/dto/admin-set-user-password.dto.ts diff --git a/src/modules/users/dto/admin-set-user-password.dto.ts b/src/modules/users/dto/admin-set-user-password.dto.ts new file mode 100644 index 0000000..55c7fb8 --- /dev/null +++ b/src/modules/users/dto/admin-set-user-password.dto.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, MaxLength, MinLength } from 'class-validator'; + +export class AdminSetUserPasswordDto { + @ApiProperty({ example: 'mySecurePass123', minLength: 8 }) + @IsString() + @MinLength(8) + @MaxLength(72) + password!: string; + + @ApiProperty({ example: 'mySecurePass123' }) + @IsString() + @MinLength(8) + @MaxLength(72) + confirmPassword!: string; +} diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index f638445..698e444 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -11,6 +11,7 @@ import { UserRole } from '../../entities/user.entity'; import { Roles } from '../auth/decorators/roles.decorator'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { RolesGuard } from '../auth/guards/roles.guard'; +import { AdminSetUserPasswordDto } from './dto/admin-set-user-password.dto'; import { UpdateUserDto } from './dto/update-user.dto'; import { UsersService } from './users.service'; @@ -30,6 +31,14 @@ export class UsersController { return this.service.findOne(id); } + @Patch(':id/password') + setPassword( + @Param('id') id: string, + @Body() payload: AdminSetUserPasswordDto, + ): Promise { + return this.service.setPassword(id, payload); + } + @Patch(':id') update( @Param('id') id: string, diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 2b26718..d50d997 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -1,8 +1,10 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; +import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; +import { hashPassword } from '../../common/utils/password.util'; import { PublicUser, serializeUser } from '../../common/utils/user-serialize.util'; import { UserEntity } from '../../entities/user.entity'; +import { AdminSetUserPasswordDto } from './dto/admin-set-user-password.dto'; import { UpdateUserDto } from './dto/update-user.dto'; @Injectable() @@ -56,4 +58,23 @@ export class UsersService { const saved = await this.repo.save(user); return serializeUser(saved); } + + async setPassword(id: string, dto: AdminSetUserPasswordDto): Promise { + if (dto.password !== dto.confirmPassword) { + throw new BadRequestException('تکرار رمز با رمز جدید یکسان نیست'); + } + + const user = await this.repo + .createQueryBuilder('user') + .addSelect('user.passwordHash') + .where('user.id = :id', { id }) + .getOne(); + if (!user) { + throw new NotFoundException('User not found'); + } + + user.passwordHash = await hashPassword(dto.password); + const saved = await this.repo.save(user); + return serializeUser(saved); + } }