add password in admin

This commit is contained in:
2026-05-25 20:51:19 +03:30
parent f9bce4d872
commit c502fd86d7
3 changed files with 47 additions and 1 deletions
@@ -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;
}
+9
View File
@@ -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<PublicUser> {
return this.service.setPassword(id, payload);
}
@Patch(':id')
update(
@Param('id') id: string,
+22 -1
View File
@@ -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<PublicUser> {
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);
}
}