add password in admin
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user