|
|
@ -7,15 +7,36 @@ import { RedisService } from '@common/service/redis/redis.service'; |
|
|
|
import { Snowflake } from '@service/snowflake/snowflake.service'; |
|
|
|
import { Snowflake } from '@service/snowflake/snowflake.service'; |
|
|
|
import { ConfigService } from '@nestjs/config'; |
|
|
|
import { ConfigService } from '@nestjs/config'; |
|
|
|
import { PacInfoType } from '@utils/myType'; |
|
|
|
import { PacInfoType } from '@utils/myType'; |
|
|
|
import { isTrueEnum } from '@utils/boolean.enum'; |
|
|
|
import { isExistKey, isTrueEnum } from '@utils/boolean.enum'; |
|
|
|
import { pacAuthLinkRoleDept, pacAuthLinkRoleMenu, pacAuthRole, pacAuthUser, pacCoreDict } from '@entities/schema'; |
|
|
|
import { pacAuthDept, pacAuthLinkRoleDept, pacAuthLinkRoleMenu, pacAuthRole, pacAuthUser, pacCoreDict, pacCoreMenu } from '@entities/schema'; |
|
|
|
import { and, eq, isNull, sql } from 'drizzle-orm'; |
|
|
|
import { and, asc, desc, eq, isNull, like, or, sql } from 'drizzle-orm'; |
|
|
|
import { GetPacAuthRoleAllDto } from '@app/auth-role/dto/get-auth-role.dto'; |
|
|
|
import { GetPacAuthRoleAllDto } from '@app/auth-role/dto/get-auth-role.dto'; |
|
|
|
|
|
|
|
import { likeQuery } from '@utils/likeQuery'; |
|
|
|
|
|
|
|
import { alias } from 'drizzle-orm/mysql-core'; |
|
|
|
|
|
|
|
|
|
|
|
@Injectable() |
|
|
|
@Injectable() |
|
|
|
export class AuthRoleService { |
|
|
|
export class AuthRoleService { |
|
|
|
// 基本分页信息
|
|
|
|
// 基本分页信息
|
|
|
|
private readonly roleBaseType = {}; |
|
|
|
private readonly rolePageType = { |
|
|
|
|
|
|
|
roleId: pacAuthRole.roleId, |
|
|
|
|
|
|
|
pid: pacAuthRole.pid, |
|
|
|
|
|
|
|
roleName: pacAuthRole.roleName, |
|
|
|
|
|
|
|
roleKey: pacAuthRole.roleKey, |
|
|
|
|
|
|
|
roleDesc: pacAuthRole.roleDesc, |
|
|
|
|
|
|
|
roleType: pacAuthRole.roleType, |
|
|
|
|
|
|
|
roleTypeName: pacCoreDict.dictName, |
|
|
|
|
|
|
|
roleTypeKey: pacCoreDict.dictKey, |
|
|
|
|
|
|
|
deptScope: pacAuthRole.deptScope, |
|
|
|
|
|
|
|
haveChildren: pacAuthRole.haveChildren, |
|
|
|
|
|
|
|
root: pacAuthRole.root, |
|
|
|
|
|
|
|
orderNum: pacAuthRole.orderNum, |
|
|
|
|
|
|
|
status: pacAuthRole.status, |
|
|
|
|
|
|
|
createby: pacAuthRole.createby, |
|
|
|
|
|
|
|
createName: pacAuthUser.nickname, |
|
|
|
|
|
|
|
createtime: pacAuthRole.createtime, |
|
|
|
|
|
|
|
updateby: pacAuthRole.updateby, |
|
|
|
|
|
|
|
updatetime: pacAuthRole.updatetime, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 角色详细信息
|
|
|
|
// 角色详细信息
|
|
|
|
private readonly roleMoreType = { |
|
|
|
private readonly roleMoreType = { |
|
|
@ -90,16 +111,18 @@ export class AuthRoleService { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// ! 增加关联菜单
|
|
|
|
// ! 增加关联菜单
|
|
|
|
await this.createMenuLink(newRoleResult.roleId, createAuthRoleDto.menuList, pacInfo.userId).catch((e) => { |
|
|
|
await this.createMenuLink(newRoleResult.roleId, createAuthRoleDto.menuList, pacInfo.userId as any).catch((e) => { |
|
|
|
this.logger.error(e.message + e.track.toString()); |
|
|
|
this.logger.error(e.message + e.track.toString()); |
|
|
|
throw new HttpException('添加菜单关系出现错误,角色写入错误!', HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
throw new HttpException('添加菜单关系出现错误,角色写入错误!', HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// ! 增加关联部门
|
|
|
|
// ! 增加关联部门
|
|
|
|
await this.createDeptLink(newRoleResult.roleId, createAuthRoleDto.deptList, pacInfo.userId).catch((e) => { |
|
|
|
if (createAuthRoleDto.deptScope == 5) { |
|
|
|
|
|
|
|
await this.createDeptLink(newRoleResult.roleId, createAuthRoleDto.deptList, pacInfo.userId as any).catch((e) => { |
|
|
|
this.logger.error(e.message + e.track.toString()); |
|
|
|
this.logger.error(e.message + e.track.toString()); |
|
|
|
throw new HttpException('添加部门数据范围关系出现错误,角色写入错误!', HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
throw new HttpException('添加部门数据范围关系出现错误,角色写入错误!', HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ! 解锁
|
|
|
|
// ! 解锁
|
|
|
|
lock(); |
|
|
|
lock(); |
|
|
@ -118,20 +141,95 @@ export class AuthRoleService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
findAll(getPacAuthRoleAllDto: GetPacAuthRoleAllDto) { |
|
|
|
/** Service |
|
|
|
return `This action returns all authRole`; |
|
|
|
* NAME: findAll |
|
|
|
|
|
|
|
* DESC: 获取角色信息列表/分页 |
|
|
|
|
|
|
|
* DATE: 2024-06-26 13:53:03 - |
|
|
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
public async findAll(getPacAuthRoleAllDto: GetPacAuthRoleAllDto) { |
|
|
|
|
|
|
|
if (isTrueEnum(getPacAuthRoleAllDto['isList'])) { |
|
|
|
|
|
|
|
return await this.getRoleList(getPacAuthRoleAllDto); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return await this.getRolePage(getPacAuthRoleAllDto); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
findOne(id: number) { |
|
|
|
/** Service |
|
|
|
return `This action returns a #${id} authRole`; |
|
|
|
* NAME: findOne |
|
|
|
|
|
|
|
* DESC: 获取角色详细信息 |
|
|
|
|
|
|
|
* DATE: 2024-06-26 16:15:21 - |
|
|
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
findOne(id: string) { |
|
|
|
|
|
|
|
return this.getRoleMore(id); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
update(id: number, updateAuthRoleDto: UpdateAuthRoleDto) { |
|
|
|
public async update(roleId: string, updateAuthRoleDto: UpdateAuthRoleDto, pacInfo: PacInfoType) { |
|
|
|
return `This action updates a #${id} authRole`; |
|
|
|
// ! 查找角色信息
|
|
|
|
|
|
|
|
const roleData = await this.getRoleForRoleId(roleId); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 当不存在角色信息
|
|
|
|
|
|
|
|
if (roleData.length == 0) { |
|
|
|
|
|
|
|
throw new HttpException('未找到目标角色信息,无法修改!', HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
remove(id: number) { |
|
|
|
// ! 判断是否是root账户,非root账户无法设置为root
|
|
|
|
return `This action removes a #${id} authRole`; |
|
|
|
if (this.config.get('masterId') !== pacInfo.userId && isTrueEnum(roleData[0].root)) { |
|
|
|
|
|
|
|
throw new HttpException('没有权限创建原始角色!', HttpStatus.UNAUTHORIZED); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 加目标锁
|
|
|
|
|
|
|
|
const lock = await this.redisService.distributedLock('DICT' + updateAuthRoleDto.roleKey, updateAuthRoleDto.roleKey); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 存在正在进行写入的角色
|
|
|
|
|
|
|
|
if (!lock) throw new HttpException('服务繁忙,角色标识重复!', HttpStatus.CONFLICT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// @ 核心内容
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
// ! 查重
|
|
|
|
|
|
|
|
const checkRepeat = await this.getRoleForRoleKey(updateAuthRoleDto.roleKey); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 是否存在重复的角色
|
|
|
|
|
|
|
|
if (checkRepeat.length > 0 && checkRepeat[0].roleId != roleId) throw new HttpException('角色标识重复!', HttpStatus.CONFLICT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 修改角色数据
|
|
|
|
|
|
|
|
const result = await this.updateRole(roleId, updateAuthRoleDto, pacInfo.userId as any).catch((e) => { |
|
|
|
|
|
|
|
this.logger.error(e.message + e.track.toString()); |
|
|
|
|
|
|
|
throw new HttpException('角色写入错误!', HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 增加关联菜单
|
|
|
|
|
|
|
|
await this.createMenuLink(roleData[0].roleId, updateAuthRoleDto.menuList, pacInfo.userId as any).catch((e) => { |
|
|
|
|
|
|
|
this.logger.error(e.message + e.track.toString()); |
|
|
|
|
|
|
|
throw new HttpException('添加菜单关系出现错误,角色写入错误!', HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 增加关联部门
|
|
|
|
|
|
|
|
await this.createDeptLink(roleData[0].roleId, updateAuthRoleDto.deptList, pacInfo.userId as any).catch((e) => { |
|
|
|
|
|
|
|
this.logger.error(e.message + e.track); |
|
|
|
|
|
|
|
throw new HttpException('添加部门数据范围关系出现错误,角色写入错误!', HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 解锁
|
|
|
|
|
|
|
|
lock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 返回结果
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
// ! 解锁
|
|
|
|
|
|
|
|
lock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 抛出错误
|
|
|
|
|
|
|
|
throw e; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Service |
|
|
|
|
|
|
|
* NAME: remove |
|
|
|
|
|
|
|
* DESC: 删除角色信息 |
|
|
|
|
|
|
|
* DATE: 2024-06-26 16:16:55 - |
|
|
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
public async remove(roleId: string, pacInfo: PacInfoType) { |
|
|
|
|
|
|
|
return await this.deleteRole(roleId, pacInfo.userId as any); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DB 通过roleKey查找角色信息
|
|
|
|
// DB 通过roleKey查找角色信息
|
|
|
@ -179,9 +277,10 @@ export class AuthRoleService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DB 建立菜单角色关联
|
|
|
|
// DB 建立菜单角色关联
|
|
|
|
private async createMenuLink(roleId, menuIdList, userId) { |
|
|
|
private async createMenuLink(roleId: string, menuIdList: string[], userId: string) { |
|
|
|
const newMenuIdList = Array.from(new Set(menuIdList)); |
|
|
|
const newMenuIdList = Array.from(new Set(menuIdList)); |
|
|
|
await this.mysqlService.db.delete(pacAuthLinkRoleMenu).where(eq(pacAuthLinkRoleMenu.roleId as any, roleId)); |
|
|
|
await this.mysqlService.db.delete(pacAuthLinkRoleMenu).where(eq(pacAuthLinkRoleMenu.roleId as any, roleId)); |
|
|
|
|
|
|
|
if (menuIdList.length > 0) { |
|
|
|
await this.mysqlService.db.insert(pacAuthLinkRoleMenu).values( |
|
|
|
await this.mysqlService.db.insert(pacAuthLinkRoleMenu).values( |
|
|
|
newMenuIdList.map((i) => ({ |
|
|
|
newMenuIdList.map((i) => ({ |
|
|
|
roleId, |
|
|
|
roleId, |
|
|
@ -191,11 +290,13 @@ export class AuthRoleService { |
|
|
|
})), |
|
|
|
})), |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DB 建立部门角色关联
|
|
|
|
// DB 建立部门角色关联
|
|
|
|
private async createDeptLink(roleId, deptIdList, userId) { |
|
|
|
private async createDeptLink(roleId: string, deptIdList: string[], userId: string) { |
|
|
|
const newDeptIdList = Array.from(new Set(deptIdList)); |
|
|
|
const newDeptIdList = Array.from(new Set(deptIdList)); |
|
|
|
await this.mysqlService.db.delete(pacAuthLinkRoleDept).where(eq(pacAuthLinkRoleDept.roleId as any, roleId)); |
|
|
|
await this.mysqlService.db.delete(pacAuthLinkRoleDept).where(eq(pacAuthLinkRoleDept.roleId as any, roleId)); |
|
|
|
|
|
|
|
if (deptIdList.length > 0) { |
|
|
|
await this.mysqlService.db.insert(pacAuthLinkRoleDept).values( |
|
|
|
await this.mysqlService.db.insert(pacAuthLinkRoleDept).values( |
|
|
|
newDeptIdList.map((i) => ({ |
|
|
|
newDeptIdList.map((i) => ({ |
|
|
|
roleId, |
|
|
|
roleId, |
|
|
@ -206,3 +307,211 @@ export class AuthRoleService { |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DB 查角色-基本查询结构
|
|
|
|
|
|
|
|
private getRoleData(data, selectData = undefined) { |
|
|
|
|
|
|
|
// ! 定义基础查询函数
|
|
|
|
|
|
|
|
// 启用动态查询模式 $dynamic
|
|
|
|
|
|
|
|
const query = this.mysqlService.db |
|
|
|
|
|
|
|
.select(selectData) |
|
|
|
|
|
|
|
.from(pacAuthRole) |
|
|
|
|
|
|
|
.orderBy( |
|
|
|
|
|
|
|
isTrueEnum(data.isAsc) ? asc(pacAuthRole.orderNum) : desc(pacAuthRole.orderNum), |
|
|
|
|
|
|
|
isTrueEnum(data.isAsc) ? asc(pacAuthRole.roleId) : desc(pacAuthRole.roleId), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.leftJoin(pacCoreDict, eq(pacAuthRole.roleType, pacCoreDict.dictId)) |
|
|
|
|
|
|
|
.$dynamic(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 查询条件集合
|
|
|
|
|
|
|
|
const wl = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 未删除
|
|
|
|
|
|
|
|
wl.push(isNull(pacAuthRole.deleteby)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 模糊查询
|
|
|
|
|
|
|
|
wl.push( |
|
|
|
|
|
|
|
or( |
|
|
|
|
|
|
|
like(pacAuthRole.roleName, likeQuery(data.roleInfo)), |
|
|
|
|
|
|
|
like(pacAuthRole.roleKey, likeQuery(data.roleInfo)), |
|
|
|
|
|
|
|
like(pacAuthRole.roleDesc, likeQuery(data.roleInfo)), |
|
|
|
|
|
|
|
).if(isExistKey(data, 'roleInfo')), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 是否查pac的数据
|
|
|
|
|
|
|
|
wl.push(eq(pacAuthRole.root, isTrueEnum(data['root']) ? 0 : 1).if(isExistKey(data, 'root') && isTrueEnum(data['root']))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 按照层级查
|
|
|
|
|
|
|
|
wl.push(eq(pacAuthRole.pid, data.hierarchy).if(isExistKey(data, 'hierarchy'))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 是否查角色类型
|
|
|
|
|
|
|
|
wl.push(eq(pacAuthRole.roleType, data.roleType).if(isExistKey(data, 'roleType'))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 是否查字典状态
|
|
|
|
|
|
|
|
wl.push(eq(pacAuthRole.status, data.status).if(isExistKey(data, 'status'))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query.where(and(...wl)); |
|
|
|
|
|
|
|
return query; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DB 查角色分页
|
|
|
|
|
|
|
|
private async getRolePage(getPacAuthRoleAllDto: GetPacAuthRoleAllDto) { |
|
|
|
|
|
|
|
const offset = (getPacAuthRoleAllDto.pageNumber - 1) * getPacAuthRoleAllDto.pageSize; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 使用基础查询构建查询总记录数
|
|
|
|
|
|
|
|
const totalCountQuery = this.getRoleData(getPacAuthRoleAllDto, { |
|
|
|
|
|
|
|
totalCount: sql`COUNT(*)`, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 使用基础查询构建分页查询
|
|
|
|
|
|
|
|
// 重命名表
|
|
|
|
|
|
|
|
const userTable1 = alias(pacAuthUser, 'userTable1'); |
|
|
|
|
|
|
|
const paginatedQuery = this.getRoleData(getPacAuthRoleAllDto, { |
|
|
|
|
|
|
|
...this.rolePageType, |
|
|
|
|
|
|
|
updateName: userTable1.nickname, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.leftJoin(pacAuthUser, eq(pacAuthRole.createby, pacAuthUser.userId)) |
|
|
|
|
|
|
|
.leftJoin(userTable1, eq(pacAuthRole.updateby, pacAuthUser.userId)) |
|
|
|
|
|
|
|
.limit(getPacAuthRoleAllDto.pageSize) |
|
|
|
|
|
|
|
.offset(offset); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
total: (await totalCountQuery)[0].totalCount, |
|
|
|
|
|
|
|
rowData: await paginatedQuery, |
|
|
|
|
|
|
|
searchData: getPacAuthRoleAllDto, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DB 查角色列表
|
|
|
|
|
|
|
|
private getRoleList(getPacAuthRoleAllDto: GetPacAuthRoleAllDto) { |
|
|
|
|
|
|
|
return this.getRoleData(getPacAuthRoleAllDto, this.roleListType); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DB 查角色树
|
|
|
|
|
|
|
|
getRoleTree() {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DB 查角色详细信息
|
|
|
|
|
|
|
|
private async getRoleMore(roleId: string) { |
|
|
|
|
|
|
|
// ! 定义2次连表查询的用户表
|
|
|
|
|
|
|
|
const userTable1 = alias(pacAuthUser, 'userTable1'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 查询目标角色信息
|
|
|
|
|
|
|
|
const roleRerult = await this.mysqlService.db |
|
|
|
|
|
|
|
.select({ |
|
|
|
|
|
|
|
...this.rolePageType, |
|
|
|
|
|
|
|
updateName: userTable1.nickname, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.from(pacAuthRole) |
|
|
|
|
|
|
|
.leftJoin(pacCoreDict, eq(pacAuthRole.roleType, pacCoreDict.dictId)) |
|
|
|
|
|
|
|
.leftJoin(pacAuthUser, eq(pacAuthRole.createby, pacAuthUser.userId)) |
|
|
|
|
|
|
|
.leftJoin(userTable1, eq(pacAuthRole.updateby, pacAuthUser.userId)) |
|
|
|
|
|
|
|
.where(and(isNull(pacAuthRole.deleteby), eq(pacAuthRole.roleId, roleId))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 如果不存在角色信息直接报错
|
|
|
|
|
|
|
|
if (roleRerult.length === 0) { |
|
|
|
|
|
|
|
throw new HttpException('未找到目标角色详细信息!', HttpStatus.BAD_REQUEST); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 获取角色关联的菜单信息
|
|
|
|
|
|
|
|
const menuList = await this.mysqlService.db |
|
|
|
|
|
|
|
.select({ |
|
|
|
|
|
|
|
menuId: pacCoreMenu.menuId, |
|
|
|
|
|
|
|
menuName: pacCoreMenu.menuName, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.from(pacCoreMenu) |
|
|
|
|
|
|
|
.leftJoin(pacAuthLinkRoleMenu, eq(pacAuthLinkRoleMenu.menuId, pacCoreMenu.menuId)) |
|
|
|
|
|
|
|
.where(and(isNull(pacCoreMenu.deleteby), eq(pacAuthLinkRoleMenu.roleId, roleId))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 获取角色数据权限的部门范围
|
|
|
|
|
|
|
|
let deptList = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 只有自定义数据范围才会选部门
|
|
|
|
|
|
|
|
if (roleRerult[0].deptScope == 5) { |
|
|
|
|
|
|
|
deptList = await this.mysqlService.db |
|
|
|
|
|
|
|
.select({ |
|
|
|
|
|
|
|
deptId: pacAuthDept.deptId, |
|
|
|
|
|
|
|
deptName: pacAuthDept.deptName, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.from(pacAuthDept) |
|
|
|
|
|
|
|
.leftJoin(pacAuthLinkRoleDept, eq(pacAuthLinkRoleDept.deptId, pacAuthDept.deptId)) |
|
|
|
|
|
|
|
.where(and(isNull(pacAuthDept.deleteby), eq(pacAuthLinkRoleDept.roleId, roleId))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 返回重构的数据结构
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
...roleRerult[0], |
|
|
|
|
|
|
|
menuList, |
|
|
|
|
|
|
|
deptList, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据ID查角色基本信息
|
|
|
|
|
|
|
|
private getRoleForRoleId(roleId: string) { |
|
|
|
|
|
|
|
return this.mysqlService.db |
|
|
|
|
|
|
|
.select() |
|
|
|
|
|
|
|
.from(pacAuthRole) |
|
|
|
|
|
|
|
.where(and(isNull(pacAuthRole.deleteby), eq(pacAuthRole.roleId, roleId))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DB 删除角色信息
|
|
|
|
|
|
|
|
private async deleteRole(roleId: string, userId: string) { |
|
|
|
|
|
|
|
// ! 查找角色信息
|
|
|
|
|
|
|
|
const roleData = await this.getRoleForRoleId(roleId); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 当不存在角色信息
|
|
|
|
|
|
|
|
if (roleData.length == 0) { |
|
|
|
|
|
|
|
throw new HttpException('未找到目标角色信息,无法删除!', HttpStatus.BAD_REQUEST); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 判断是否是root账户,非root账户无法设置为root
|
|
|
|
|
|
|
|
if (this.config.get('masterId') !== userId && isTrueEnum(roleData[0].root)) { |
|
|
|
|
|
|
|
throw new HttpException('没有权限创建原始角色!', HttpStatus.UNAUTHORIZED); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 判断是否存在子项
|
|
|
|
|
|
|
|
if (roleData[0].haveChildren != 0) { |
|
|
|
|
|
|
|
throw new HttpException('该角色存在子项!', HttpStatus.BAD_REQUEST); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 清角色菜单关联表
|
|
|
|
|
|
|
|
await this.mysqlService.db.delete(pacAuthLinkRoleMenu).where(eq(pacAuthLinkRoleMenu.roleId as any, roleId)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ? 判断是否是自定义数据范围,清角色部门关联表
|
|
|
|
|
|
|
|
if (roleData[0].deptScope == 5) { |
|
|
|
|
|
|
|
await this.mysqlService.db.delete(pacAuthLinkRoleDept).where(eq(pacAuthLinkRoleDept.roleId as any, roleId)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 判断父节点是否存在
|
|
|
|
|
|
|
|
if (roleData[0].pid != 0) { |
|
|
|
|
|
|
|
// ! 减少父级子节点数量
|
|
|
|
|
|
|
|
await this.haveChildrenSelfIncreasing(roleData[0].pid, false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ! 删除角色数据
|
|
|
|
|
|
|
|
return await this.mysqlService.db |
|
|
|
|
|
|
|
.update(pacAuthRole) |
|
|
|
|
|
|
|
.set({ |
|
|
|
|
|
|
|
deletetime: sql`now()`, |
|
|
|
|
|
|
|
deleteby: userId, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.where(eq(pacAuthRole.roleId, roleId)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DB 更新角色信息
|
|
|
|
|
|
|
|
private async updateRole(roleId: string, updateAuthRoleDto: UpdateAuthRoleDto, userId: string) { |
|
|
|
|
|
|
|
return this.mysqlService.db |
|
|
|
|
|
|
|
.update(pacAuthRole) |
|
|
|
|
|
|
|
.set({ |
|
|
|
|
|
|
|
roleName: updateAuthRoleDto.roleName, |
|
|
|
|
|
|
|
roleKey: updateAuthRoleDto.roleKey, |
|
|
|
|
|
|
|
roleDesc: updateAuthRoleDto.roleDesc, |
|
|
|
|
|
|
|
roleType: updateAuthRoleDto.roleType, |
|
|
|
|
|
|
|
deptScope: updateAuthRoleDto.deptScope, |
|
|
|
|
|
|
|
orderNum: updateAuthRoleDto.orderNum, |
|
|
|
|
|
|
|
status: updateAuthRoleDto.status, |
|
|
|
|
|
|
|
root: isTrueEnum(updateAuthRoleDto.root) ? 1 : 0, |
|
|
|
|
|
|
|
updateby: userId, |
|
|
|
|
|
|
|
updatetime: sql`now()`, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.where(eq(pacAuthRole.roleId, roleId)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|