feat: RBAC
This commit is contained in:
@@ -67,11 +67,9 @@ export class MenuService {
|
||||
|
||||
async update(id: string, data: UpdateMenuDto) {
|
||||
const doesExists = await this.prisma.sysResource.findUnique({
|
||||
where: { path: data.path },
|
||||
where: { path: data.path, NOT: { id } },
|
||||
});
|
||||
|
||||
console.log(doesExists);
|
||||
|
||||
if (doesExists) {
|
||||
throw new HttpException('菜单路径不可重复!', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ import { IsString } from 'class-validator';
|
||||
|
||||
export class GrantMenuDto {
|
||||
@IsString({ each: true })
|
||||
menuIds: string[];
|
||||
ids: string[];
|
||||
}
|
||||
|
||||
@@ -2,18 +2,37 @@ import { Controller, Body, Put, Param, Get } from '@nestjs/common';
|
||||
import { PermissionService } from './permission.service';
|
||||
|
||||
import { GrantMenuDto } from './dto/grant-menu.dto';
|
||||
import { PermissionType } from '@/common/enum';
|
||||
|
||||
@Controller()
|
||||
export class PermissionController {
|
||||
constructor(private readonly permissionService: PermissionService) {}
|
||||
|
||||
@Get('/roles/:roleId/resources')
|
||||
@Get('/roles/:roleId/menus')
|
||||
getRoleMenus(@Param('roleId') roleId: string) {
|
||||
return this.permissionService.getRoleMenus(roleId);
|
||||
return this.permissionService.getRoleResources(roleId, PermissionType.Menu);
|
||||
}
|
||||
|
||||
@Put('/roles/:roleId/menus')
|
||||
assignMenus(@Param('roleId') roleId: string, @Body() data: GrantMenuDto) {
|
||||
return this.permissionService.assignResources(
|
||||
roleId,
|
||||
PermissionType.Menu,
|
||||
data,
|
||||
);
|
||||
}
|
||||
|
||||
@Get('/roles/:roleId/resources')
|
||||
getRolResources(@Param('roleId') roleId: string) {
|
||||
return this.permissionService.getRoleResources(roleId, PermissionType.Menu);
|
||||
}
|
||||
|
||||
@Put('/roles/:roleId/resources')
|
||||
assignMenus(@Param('roleId') roleId: string, @Body() dto: GrantMenuDto) {
|
||||
return this.permissionService.assignMenus(roleId, dto);
|
||||
assignResources(@Param('roleId') roleId: string, @Body() data: GrantMenuDto) {
|
||||
return this.permissionService.assignResources(
|
||||
roleId,
|
||||
PermissionType.Menu,
|
||||
data,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -8,7 +8,7 @@ export class CreateResourceDto {
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
permission: string;
|
||||
permissionKey: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
|
||||
@@ -11,7 +11,7 @@ export class ResourceService {
|
||||
|
||||
async create(data: CreateResourceDto) {
|
||||
const doesExists = await this.prisma.sysResource.findUnique({
|
||||
where: { path: data.permission },
|
||||
where: { permissionKey: data.permissionKey },
|
||||
});
|
||||
|
||||
if (doesExists)
|
||||
@@ -66,7 +66,7 @@ export class ResourceService {
|
||||
|
||||
async update(id: string, data: UpdateResourceDto) {
|
||||
const doesExists = await this.prisma.sysResource.findUnique({
|
||||
where: { path: data.permission, NOT: { id } },
|
||||
where: { permissionKey: data.permissionKey, NOT: { id } },
|
||||
});
|
||||
|
||||
if (doesExists)
|
||||
|
||||
@@ -33,7 +33,6 @@ export class UserController {
|
||||
getUserInfo(@Request() req) {
|
||||
const { sub } = req['user'];
|
||||
const { isAdmin } = req;
|
||||
console.log(isAdmin);
|
||||
return this.userService.getUserInfo(sub, isAdmin);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user