test: harden business boundary conditions

This commit is contained in:
2026-07-15 00:03:55 +08:00
parent 17a5046ea0
commit b1f35f9d1a
65 changed files with 2311 additions and 293 deletions

View File

@@ -301,7 +301,11 @@ export class RbacService {
}
}
if (role.code !== preset.code || role.name !== preset.name || role.description !== preset.description) {
if (
role.code !== preset.code ||
role.name !== preset.name ||
role.description !== preset.description
) {
role.code = preset.code;
role.name = preset.name;
role.description = preset.description;
@@ -368,6 +372,28 @@ export class RbacService {
return this.roleRepo.findOneOrFail({ where: { id }, relations: ['permissions'] });
}
private async resolvePermissions(permissionIds: number[]): Promise<Permission[]> {
const uniqueIds = [...new Set(permissionIds)];
const permissions = uniqueIds.length > 0 ? await this.permRepo.findByIds(uniqueIds) : [];
if (permissions.length !== uniqueIds.length) {
const foundIds = new Set(permissions.map((permission) => permission.id));
const missingIds = uniqueIds.filter((id) => !foundIds.has(id));
throw new Error(`权限不存在: ${missingIds.join(',')}`);
}
return permissions;
}
private async resolveRoles(roleIds: number[]): Promise<Role[]> {
const uniqueIds = [...new Set(roleIds)];
const roles = uniqueIds.length > 0 ? await this.roleRepo.findByIds(uniqueIds) : [];
if (roles.length !== uniqueIds.length) {
const foundIds = new Set(roles.map((role) => role.id));
const missingIds = uniqueIds.filter((id) => !foundIds.has(id));
throw new Error(`角色不存在: ${missingIds.join(',')}`);
}
return roles;
}
async createRole(dto: {
name: string;
description?: string;
@@ -375,7 +401,7 @@ export class RbacService {
}): Promise<Role> {
const role = this.roleRepo.create({ name: dto.name, description: dto.description });
if (dto.permissionIds && dto.permissionIds.length > 0) {
role.permissions = await this.permRepo.findByIds(dto.permissionIds);
role.permissions = await this.resolvePermissions(dto.permissionIds);
}
return this.roleRepo.save(role);
}
@@ -392,7 +418,7 @@ export class RbacService {
if (dto.description !== undefined) role.description = dto.description;
if (dto.permissionIds !== undefined) {
role.permissions =
dto.permissionIds.length > 0 ? await this.permRepo.findByIds(dto.permissionIds) : [];
dto.permissionIds.length > 0 ? await this.resolvePermissions(dto.permissionIds) : [];
}
return this.roleRepo.save(role);
}
@@ -472,7 +498,7 @@ export class RbacService {
name: dto.name,
});
if (dto.roleIds && dto.roleIds.length > 0) {
user.roles = await this.roleRepo.findByIds(dto.roleIds);
user.roles = await this.resolveRoles(dto.roleIds);
}
await this.userRepo.save(user);
return { message: '用户创建成功' };
@@ -492,7 +518,7 @@ export class RbacService {
if (dto.name !== undefined) user.name = dto.name;
if (dto.isActive !== undefined) user.isActive = dto.isActive;
if (dto.roleIds !== undefined) {
user.roles = dto.roleIds.length > 0 ? await this.roleRepo.findByIds(dto.roleIds) : [];
user.roles = dto.roleIds.length > 0 ? await this.resolveRoles(dto.roleIds) : [];
}
await this.userRepo.save(user);
return { message: '更新成功' };