完成用户权限管理
This commit is contained in:
@@ -9,6 +9,7 @@ import { RoleModule } from 'src/role/role.module';
|
||||
import { AdminRoleController } from './controller/admin-role.controller';
|
||||
import { AdminPermissionController } from './controller/admin-permission.controller';
|
||||
import { AdminRolePermissionController } from './controller/admin-role-permission.controller';
|
||||
import { AdminUserRoleController } from './controller/admin-user-role.controller';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -24,6 +25,7 @@ import { AdminRolePermissionController } from './controller/admin-role-permissio
|
||||
AdminRoleController,
|
||||
AdminPermissionController,
|
||||
AdminRolePermissionController,
|
||||
AdminUserRoleController,
|
||||
],
|
||||
providers: [
|
||||
AdminUserService,
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseUUIDPipe, Post } from "@nestjs/common";
|
||||
import { RoleService } from "src/role/services/role.service";
|
||||
import { UserRoleService } from "src/role/services/user-role.service";
|
||||
import { CreateUserRoleDto } from "../dto/admin-user-role/create-user-role.dto";
|
||||
import { DeleteUserRoleDto } from "../dto/admin-user-role/delete-user-role.dto";
|
||||
|
||||
@Controller('admin/users/:userId/role')
|
||||
export class AdminUserRoleController {
|
||||
|
||||
constructor(
|
||||
private readonly userRoleService: UserRoleService,
|
||||
private readonly roleService: RoleService,
|
||||
) { }
|
||||
|
||||
@Get()
|
||||
async getUserRoles(
|
||||
@Param('userId', new ParseUUIDPipe({ version: '4' })) userId: string,
|
||||
) {
|
||||
const userRoleIds = await this.userRoleService.findRoleIdsByUserId(userId);
|
||||
return await this.roleService.findRolesByRoleIds(userRoleIds);
|
||||
}
|
||||
|
||||
@Post()
|
||||
async setUserRoles(
|
||||
@Param('userId', new ParseUUIDPipe({ version: '4' })) userId: string,
|
||||
@Body() dto: CreateUserRoleDto,
|
||||
) {
|
||||
return this.userRoleService.addUserRole({
|
||||
userId,
|
||||
roleId: dto.roleId,
|
||||
isEnabled: dto.isEnabled,
|
||||
expiredAt: dto.expiredAt,
|
||||
});
|
||||
}
|
||||
|
||||
@Delete()
|
||||
async deleteUserRoles(
|
||||
@Param('userId', new ParseUUIDPipe({ version: '4' })) userId: string,
|
||||
@Body() dto: DeleteUserRoleDto,
|
||||
) {
|
||||
return this.userRoleService.deleteUserRole(userId, dto.roleId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IsBoolean, IsDateString, IsOptional, IsUUID } from "class-validator";
|
||||
|
||||
export class CreateUserRoleDto {
|
||||
@IsUUID('4')
|
||||
roleId: string;
|
||||
|
||||
@IsBoolean()
|
||||
isEnabled: boolean;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
expiredAt?: Date;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { IsUUID } from "class-validator";
|
||||
|
||||
export class DeleteUserRoleDto {
|
||||
@IsUUID('4')
|
||||
roleId: string;
|
||||
}
|
||||
@@ -10,6 +10,16 @@ export class UserRoleService {
|
||||
private readonly userRoleRepository: Repository<UserRole>,
|
||||
) { }
|
||||
|
||||
async findRoleIdsByUserId(userId: string): Promise<string[]> {
|
||||
const userRoles = await this.userRoleRepository.find({
|
||||
where: {
|
||||
userId,
|
||||
}
|
||||
});
|
||||
|
||||
return userRoles.map(ur => ur.roleId);
|
||||
}
|
||||
|
||||
async findValidRoleIdsByUserId(userId: string): Promise<string[]> {
|
||||
return (await this.findValidRolesByUserId(userId)).map(ur => ur.roleId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user