feat: 实现登录鉴权 RBAC 与用户管理模块
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsEnum, IsNotEmpty, IsOptional, IsString, MaxLength, MinLength } from 'class-validator';
|
||||
import { RoleType } from '@artveris/shared';
|
||||
|
||||
export class CreateUserDto {
|
||||
@ApiProperty({ description: '用户名' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@MaxLength(50)
|
||||
username!: string;
|
||||
|
||||
@ApiProperty({ description: '密码', minLength: 6 })
|
||||
@IsString()
|
||||
@MinLength(6)
|
||||
@MaxLength(64)
|
||||
password!: string;
|
||||
|
||||
@ApiProperty({ description: '姓名', required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(50)
|
||||
realName?: string;
|
||||
|
||||
@ApiProperty({ description: '角色', enum: RoleType })
|
||||
@IsEnum(RoleType)
|
||||
role!: RoleType;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsEnum, IsOptional, IsString } from 'class-validator';
|
||||
import { RoleType, UserStatus } from '@artveris/shared';
|
||||
import { PaginationDto } from '../../common/dto/pagination.dto';
|
||||
|
||||
export class QueryUserDto extends PaginationDto {
|
||||
@ApiPropertyOptional({ description: '关键词(用户名/姓名)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
keyword?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '角色', enum: RoleType })
|
||||
@IsOptional()
|
||||
@IsEnum(RoleType)
|
||||
role?: RoleType;
|
||||
|
||||
@ApiPropertyOptional({ description: '状态', enum: UserStatus })
|
||||
@IsOptional()
|
||||
@IsEnum(UserStatus)
|
||||
status?: UserStatus;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsEnum, IsOptional, IsString, MaxLength, MinLength } from 'class-validator';
|
||||
import { RoleType, UserStatus } from '@artveris/shared';
|
||||
|
||||
export class UpdateUserDto {
|
||||
@ApiPropertyOptional({ description: '姓名' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(50)
|
||||
realName?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '角色', enum: RoleType })
|
||||
@IsOptional()
|
||||
@IsEnum(RoleType)
|
||||
role?: RoleType;
|
||||
|
||||
@ApiPropertyOptional({ description: '状态', enum: UserStatus })
|
||||
@IsOptional()
|
||||
@IsEnum(UserStatus)
|
||||
status?: UserStatus;
|
||||
}
|
||||
|
||||
export class ResetPasswordDto {
|
||||
@ApiPropertyOptional({ description: '新密码', minLength: 6 })
|
||||
@IsString()
|
||||
@MinLength(6)
|
||||
@MaxLength(64)
|
||||
newPassword!: string;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { RoleType } from '@artveris/shared';
|
||||
import { UserService } from './user.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto, ResetPasswordDto } from './dto/update-user.dto';
|
||||
import { QueryUserDto } from './dto/query-user.dto';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
import { CurrentUser, AuthUser } from '../common/decorators/current-user.decorator';
|
||||
|
||||
@ApiTags('用户管理')
|
||||
@ApiBearerAuth()
|
||||
@Roles(RoleType.SUPER_ADMIN)
|
||||
@Controller('users')
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: '用户列表' })
|
||||
list(@Query() query: QueryUserDto) {
|
||||
return this.userService.list(query);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: '创建用户' })
|
||||
create(@Body() dto: CreateUserDto) {
|
||||
return this.userService.create(dto);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
@ApiOperation({ summary: '更新用户' })
|
||||
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateUserDto) {
|
||||
return this.userService.update(id, dto);
|
||||
}
|
||||
|
||||
@Put(':id/password')
|
||||
@ApiOperation({ summary: '重置用户密码' })
|
||||
resetPassword(@Param('id', ParseIntPipe) id: number, @Body() dto: ResetPasswordDto) {
|
||||
return this.userService.resetPassword(id, dto.newPassword);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: '删除用户' })
|
||||
remove(@Param('id', ParseIntPipe) id: number, @CurrentUser() user: AuthUser) {
|
||||
return this.userService.remove(id, user.id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { UserController } from './user.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
exports: [UserService],
|
||||
})
|
||||
export class UserModule {}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { ErrorCode } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { buildPage } from '../common/utils/pagination.util';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { QueryUserDto } from './dto/query-user.dto';
|
||||
|
||||
const userSelect = {
|
||||
id: true,
|
||||
username: true,
|
||||
realName: true,
|
||||
role: true,
|
||||
status: true,
|
||||
lastLoginAt: true,
|
||||
createdAt: true,
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async list(query: QueryUserDto) {
|
||||
const where: Prisma.SysUserWhereInput = {};
|
||||
if (query.keyword) {
|
||||
where.OR = [
|
||||
{ username: { contains: query.keyword } },
|
||||
{ realName: { contains: query.keyword } },
|
||||
];
|
||||
}
|
||||
if (query.role) where.role = query.role;
|
||||
if (query.status) where.status = query.status;
|
||||
|
||||
const [list, total] = await this.prisma.$transaction([
|
||||
this.prisma.sysUser.findMany({
|
||||
where,
|
||||
select: userSelect,
|
||||
orderBy: { id: 'desc' },
|
||||
skip: query.skip,
|
||||
take: query.take,
|
||||
}),
|
||||
this.prisma.sysUser.count({ where }),
|
||||
]);
|
||||
return buildPage(list, total, query.page, query.pageSize);
|
||||
}
|
||||
|
||||
async create(dto: CreateUserDto) {
|
||||
const exists = await this.prisma.sysUser.findUnique({ where: { username: dto.username } });
|
||||
if (exists) {
|
||||
throw new BusinessException(ErrorCode.USERNAME_EXISTS, '用户名已存在');
|
||||
}
|
||||
const passwordHash = await bcrypt.hash(dto.password, 10);
|
||||
return this.prisma.sysUser.create({
|
||||
data: {
|
||||
username: dto.username,
|
||||
passwordHash,
|
||||
realName: dto.realName,
|
||||
role: dto.role,
|
||||
},
|
||||
select: userSelect,
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateUserDto) {
|
||||
await this.ensureExists(id);
|
||||
return this.prisma.sysUser.update({
|
||||
where: { id },
|
||||
data: { realName: dto.realName, role: dto.role, status: dto.status },
|
||||
select: userSelect,
|
||||
});
|
||||
}
|
||||
|
||||
async resetPassword(id: number, newPassword: string) {
|
||||
await this.ensureExists(id);
|
||||
const passwordHash = await bcrypt.hash(newPassword, 10);
|
||||
await this.prisma.sysUser.update({ where: { id }, data: { passwordHash } });
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async remove(id: number, currentUserId: number) {
|
||||
if (id === currentUserId) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '不能删除当前登录账号');
|
||||
}
|
||||
await this.ensureExists(id);
|
||||
await this.prisma.sysUser.delete({ where: { id } });
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
private async ensureExists(id: number) {
|
||||
const user = await this.prisma.sysUser.findUnique({ where: { id } });
|
||||
if (!user) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND, '用户不存在', 404);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user