feat: RBAC权限管理
This commit is contained in:
@@ -10,4 +10,5 @@ export enum RedisGroup {
|
||||
|
||||
export enum PermissionType {
|
||||
Menu,
|
||||
API,
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ export class CreateMenuDto {
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
url: string;
|
||||
path: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@@ -16,4 +16,8 @@ export class CreateMenuDto {
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
sort?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
type: number;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,9 @@ export class MenuService {
|
||||
|
||||
findOne(params: { id?: string; excludeId?: string }) {
|
||||
const { id, excludeId } = params;
|
||||
return this.prisma.sysMenu.findFirst({
|
||||
return this.prisma.sysResource.findFirst({
|
||||
where: {
|
||||
type: PermissionType.Menu,
|
||||
...(id ? { id } : {}),
|
||||
...(excludeId ? { NOT: { id: excludeId } } : {}),
|
||||
},
|
||||
@@ -20,18 +21,20 @@ export class MenuService {
|
||||
}
|
||||
|
||||
findOneById(id: string) {
|
||||
return this.prisma.sysMenu.findUnique({ where: { id } });
|
||||
return this.prisma.sysResource.findUnique({ where: { id } });
|
||||
}
|
||||
|
||||
async create(data: CreateMenuDto) {
|
||||
await this.prisma.$transaction(async (prisma) => {
|
||||
const { id: menuId } = await prisma.sysMenu.create({ data });
|
||||
const { id: resourceId } = await prisma.sysResource.create({ data });
|
||||
// 创建菜单权限
|
||||
const { id: permissionId } = await prisma.sysPermission.create({
|
||||
data: { type: PermissionType.Menu },
|
||||
data: {},
|
||||
});
|
||||
// // 关联权限和菜单
|
||||
await prisma.sysResourcePermission.create({
|
||||
data: { resourceId, permissionId },
|
||||
});
|
||||
// 关联权限和菜单
|
||||
await prisma.sysMenuPermission.create({ data: { menuId, permissionId } });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -50,23 +53,23 @@ export class MenuService {
|
||||
}
|
||||
|
||||
async getTreeMenu() {
|
||||
const query: Prisma.SysMenuFindManyArgs = {
|
||||
const query: Prisma.SysResourceFindManyArgs = {
|
||||
orderBy: [{ sort: 'desc' }, { createdAt: 'desc' }],
|
||||
};
|
||||
const list = await this.prisma.sysMenu.findMany(query);
|
||||
const list = await this.prisma.sysResource.findMany(query);
|
||||
return this.generatorMenu(list);
|
||||
}
|
||||
|
||||
findAll() {
|
||||
const query: Prisma.SysMenuFindManyArgs = {
|
||||
const query: Prisma.SysResourceFindManyArgs = {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
url: true,
|
||||
path: true,
|
||||
parentId: true,
|
||||
},
|
||||
};
|
||||
return this.prisma.sysMenu.findMany(query);
|
||||
return this.prisma.sysResource.findMany(query);
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateMenuDto) {
|
||||
@@ -82,18 +85,18 @@ export class MenuService {
|
||||
);
|
||||
}
|
||||
|
||||
await this.prisma.sysMenu.update({ data, where: { id } });
|
||||
await this.prisma.sysResource.update({ data, where: { id } });
|
||||
}
|
||||
|
||||
async remove(menuId: string) {
|
||||
const doesExists = await this.findOneById(menuId);
|
||||
async remove(resourceId: string) {
|
||||
const doesExists = await this.findOneById(resourceId);
|
||||
if (!doesExists) {
|
||||
throw new HttpException('菜单不存在!', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
// 判断是否存在子菜单
|
||||
const doesExistsChildren = await this.prisma.sysMenu.findFirst({
|
||||
where: { parentId: menuId },
|
||||
const doesExistsChildren = await this.prisma.sysResource.findFirst({
|
||||
where: { parentId: resourceId },
|
||||
});
|
||||
|
||||
if (doesExistsChildren) {
|
||||
@@ -108,14 +111,14 @@ export class MenuService {
|
||||
// 因为schema文件中设置了onDelete: Cascade 删除菜单会删除关联表数据 则无法找出对应权限
|
||||
await prisma.sysPermission.deleteMany({
|
||||
where: {
|
||||
sysMenuPermission: {
|
||||
some: { menuId },
|
||||
sysResourcePermission: {
|
||||
some: { resourceId },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
//step2 删除菜单数据
|
||||
await prisma.sysMenu.delete({ where: { id: menuId } });
|
||||
await prisma.sysResourcePermission.delete({ where: { id: resourceId } });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ export class PermissionService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getMenu(id: string) {
|
||||
const menuIds = await this.prisma.sysMenuPermission.findMany({
|
||||
const menuIds = await this.prisma.sysResourcePermission.findMany({
|
||||
select: {
|
||||
menuId: true,
|
||||
resourceId: true,
|
||||
},
|
||||
where: {
|
||||
permission: {
|
||||
@@ -20,7 +20,7 @@ export class PermissionService {
|
||||
},
|
||||
});
|
||||
|
||||
return menuIds.map((item) => item.menuId);
|
||||
return menuIds.map((item) => item.resourceId);
|
||||
}
|
||||
|
||||
async grantMenu(id: string, { menuIds }: GrantMenuDto) {
|
||||
@@ -30,9 +30,9 @@ export class PermissionService {
|
||||
where: { roleId: id },
|
||||
});
|
||||
// 通过菜单id 找出所有权限id
|
||||
const rolePermIds = await prisma.sysMenuPermission.findMany({
|
||||
const rolePermIds = await prisma.sysResourcePermission.findMany({
|
||||
select: { permissionId: true },
|
||||
where: { menuId: { in: menuIds } },
|
||||
where: { resourceId: { in: menuIds } },
|
||||
});
|
||||
// 根据权限id 关联菜单和权限
|
||||
if (rolePermIds.length > 0) {
|
||||
|
||||
@@ -13,7 +13,7 @@ import { QueryListDto } from './dto/query-list.dto';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { UpdatePasswordDto } from './dto/update-password.dto';
|
||||
import { SkipAuth } from '@/common/decorators/skip-auth.decorator';
|
||||
import { RedisGroup } from '@/common/enum';
|
||||
import { PermissionType, RedisGroup } from '@/common/enum';
|
||||
import { Cache, CACHE_MANAGER } from '@nestjs/cache-manager';
|
||||
|
||||
@Injectable()
|
||||
@@ -126,11 +126,12 @@ export class UserService {
|
||||
},
|
||||
});
|
||||
|
||||
const query: Prisma.SysMenuFindManyArgs = { where: {} };
|
||||
const query: Prisma.SysResourceFindManyArgs = { where: {} };
|
||||
|
||||
if (!isAdmin) {
|
||||
query.where = {
|
||||
sysMenuPermission: {
|
||||
type: PermissionType.Menu,
|
||||
sysResourcePermission: {
|
||||
some: {
|
||||
permission: {
|
||||
sysRolePermission: {
|
||||
@@ -142,7 +143,8 @@ export class UserService {
|
||||
};
|
||||
}
|
||||
|
||||
const menus = await this.prisma.sysMenu.findMany(query);
|
||||
const menus = await this.prisma.sysResource.findMany();
|
||||
|
||||
return {
|
||||
...user,
|
||||
menus,
|
||||
|
||||
Reference in New Issue
Block a user