feat: RBAC

This commit is contained in:
2025-03-12 11:38:34 +08:00
parent e06d00feff
commit fb3686968d
17 changed files with 144 additions and 98 deletions

View File

@@ -1,13 +1,14 @@
import { Injectable } from '@nestjs/common';
import { GrantMenuDto } from '@/modules/system/permission/dto/grant-menu.dto';
import { PrismaService } from '@/prisma/prisma.service';
import { PermissionType } from '@/common/enum';
@Injectable()
export class PermissionService {
constructor(private readonly prisma: PrismaService) {}
async getRoleMenus(roleId: string) {
const menuIds = await this.prisma.sysResourcePermission.findMany({
async getRoleResources(roleId: string, type: PermissionType) {
const ids = await this.prisma.sysResourcePermission.findMany({
select: {
resourceId: true,
},
@@ -20,19 +21,25 @@ export class PermissionService {
},
});
return menuIds.map((item) => item.resourceId);
return ids.map((item) => item.resourceId);
}
async assignMenus(roleId: string, { menuIds }: GrantMenuDto) {
async assignResources(
roleId: string,
type: PermissionType,
{ ids }: GrantMenuDto,
) {
await this.prisma.$transaction(async (prisma) => {
// 删除角色对应的权限
await prisma.sysRolePermission.deleteMany({
where: { roleId },
where: {
roleId,
},
});
// 通过菜单id 找出所有权限id
// 通过资源id 找出所有权限id
const rolePermIds = await prisma.sysResourcePermission.findMany({
select: { permissionId: true },
where: { resourceId: { in: menuIds } },
where: { resourceId: { in: ids } },
});
// 根据权限id 关联菜单和权限
if (rolePermIds.length > 0) {