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

153 lines
3.6 KiB
TypeScript
Raw Normal View History

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