oa_based/src/modules/system/role/role.service.ts

158 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-02-28 17:02:46 +08:00
import { Injectable } from '@nestjs/common';
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
import { isEmpty, isNumber } from 'lodash';
import { EntityManager, In, Like, Repository } from 'typeorm';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { PagerDto } from '~/common/dto/pager.dto';
import { ROOT_ROLE_ID } from '~/constants/system.constant';
import { paginate } from '~/helper/paginate';
import { Pagination } from '~/helper/paginate/pagination';
import { SseService } from '~/modules/sse/sse.service';
import { MenuEntity } from '~/modules/system/menu/menu.entity';
import { RoleEntity } from '~/modules/system/role/role.entity';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { RoleDto, RoleQueryDto, RoleUpdateDto } from './role.dto';
2024-02-28 08:32:35 +08:00
@Injectable()
export class RoleService {
constructor(
@InjectRepository(RoleEntity)
private roleRepository: Repository<RoleEntity>,
@InjectRepository(MenuEntity)
private menuRepository: Repository<MenuEntity>,
@InjectEntityManager() private entityManager: EntityManager,
2024-02-28 17:02:46 +08:00
private sseService: SseService
2024-02-28 08:32:35 +08:00
) {}
/**
*
*/
async findAll({
page,
pageSize,
name,
value,
2024-02-29 09:29:03 +08:00
status
}: RoleQueryDto): Promise<Pagination<RoleEntity>> {
2024-02-28 17:02:46 +08:00
const queryBuilder = this.roleRepository.createQueryBuilder('role').where({
...(name ? { name: Like(`%${name}%`) } : null),
...(value ? { value: Like(`%${value}%`) } : null),
2024-02-29 09:29:03 +08:00
...(isNumber(status) ? { status } : null)
2024-02-28 17:02:46 +08:00
});
2024-02-28 17:02:46 +08:00
return paginate<RoleEntity>(queryBuilder, {
page,
2024-02-29 09:29:03 +08:00
pageSize
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
}
/**
*
*/
async info(id: number) {
const info = await this.roleRepository
.createQueryBuilder('role')
.where({
2024-02-29 09:29:03 +08:00
id
2024-02-28 08:32:35 +08:00
})
2024-02-28 17:02:46 +08:00
.getOne();
2024-02-28 08:32:35 +08:00
const menus = await this.menuRepository.find({
where: { roles: { id } },
2024-02-29 09:29:03 +08:00
select: ['id']
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
return { ...info, menuIds: menus.map(m => m.id) };
2024-02-28 08:32:35 +08:00
}
async delete(id: number): Promise<void> {
2024-02-28 17:02:46 +08:00
if (id === ROOT_ROLE_ID) throw new Error('不能删除超级管理员');
await this.roleRepository.delete(id);
2024-02-28 08:32:35 +08:00
}
/**
*
*/
async create({ menuIds, ...data }: RoleDto): Promise<{ roleId: number }> {
const role = await this.roleRepository.save({
...data,
2024-02-29 09:29:03 +08:00
menus: menuIds ? await this.menuRepository.findBy({ id: In(menuIds) }) : []
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
return { roleId: role.id };
2024-02-28 08:32:35 +08:00
}
/**
*
*/
async update(id, { menuIds, ...data }: RoleUpdateDto): Promise<void> {
2024-02-28 17:02:46 +08:00
await this.roleRepository.update(id, data);
2024-02-28 08:32:35 +08:00
if (!isEmpty(menuIds)) {
// using transaction
2024-02-28 17:02:46 +08:00
await this.entityManager.transaction(async manager => {
2024-02-28 08:32:35 +08:00
const menus = await this.menuRepository.find({
2024-02-29 09:29:03 +08:00
where: { id: In(menuIds) }
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
const role = await this.roleRepository.findOne({ where: { id } });
role.menus = menus;
await manager.save(role);
});
2024-02-28 08:32:35 +08:00
}
}
/**
* id查找角色信息
*/
async getRoleIdsByUser(id: number): Promise<number[]> {
const roles = await this.roleRepository.find({
where: {
2024-02-29 09:29:03 +08:00
users: { id }
}
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
if (!isEmpty(roles)) return roles.map(r => r.id);
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
return [];
2024-02-28 08:32:35 +08:00
}
async getRoleValues(ids: number[]): Promise<string[]> {
return (
await this.roleRepository.findBy({
2024-02-29 09:29:03 +08:00
id: In(ids)
2024-02-28 08:32:35 +08:00
})
2024-02-28 17:02:46 +08:00
).map(r => r.value);
2024-02-28 08:32:35 +08:00
}
async isAdminRoleByUser(uid: number): Promise<boolean> {
const roles = await this.roleRepository.find({
where: {
2024-02-29 09:29:03 +08:00
users: { id: uid }
}
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
if (!isEmpty(roles)) {
2024-02-28 17:02:46 +08:00
return roles.some(r => r.id === ROOT_ROLE_ID);
2024-02-28 08:32:35 +08:00
}
2024-02-28 17:02:46 +08:00
return false;
2024-02-28 08:32:35 +08:00
}
hasAdminRole(rids: number[]): boolean {
2024-02-28 17:02:46 +08:00
return rids.includes(ROOT_ROLE_ID);
2024-02-28 08:32:35 +08:00
}
/**
* ID查找是否有关联用户
*/
async checkUserByRoleId(id: number): Promise<boolean> {
return this.roleRepository.exist({
where: {
users: {
2024-02-29 09:29:03 +08:00
roles: { id }
}
}
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
}
}