潘志宝
2024-12-16 df99e46312fdd5ee830f1451e478f6658e09f9ed
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.controller.admin.permission;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import com.iailab.framework.common.pojo.CommonResult;
5 import com.iailab.module.system.controller.admin.permission.vo.permission.PermissionAssignRoleDataScopeReqVO;
6 import com.iailab.module.system.controller.admin.permission.vo.permission.PermissionAssignRoleMenuReqVO;
7 import com.iailab.module.system.controller.admin.permission.vo.permission.PermissionAssignUserRoleReqVO;
8 import com.iailab.module.system.service.permission.PermissionService;
9 import com.iailab.module.system.service.tenant.TenantService;
10 import io.swagger.v3.oas.annotations.Operation;
11 import io.swagger.v3.oas.annotations.Parameter;
12 import io.swagger.v3.oas.annotations.tags.Tag;
13 import org.springframework.security.access.prepost.PreAuthorize;
14 import org.springframework.validation.annotation.Validated;
15 import org.springframework.web.bind.annotation.*;
16
17 import javax.annotation.Resource;
18 import javax.validation.Valid;
19 import java.util.Set;
20
21 import static com.iailab.framework.common.pojo.CommonResult.success;
22
23 /**
24  * 权限 Controller,提供赋予用户、角色的权限的 API 接口
25  *
26  * @author iailab
27  */
28 @Tag(name = "管理后台 - 权限")
29 @RestController
30 @RequestMapping("/system/permission")
31 public class PermissionController {
32
33     @Resource
34     private PermissionService permissionService;
35     @Resource
36     private TenantService tenantService;
37
38     @Operation(summary = "获得角色拥有的菜单编号")
39     @Parameter(name = "roleId", description = "角色编号", required = true)
40     @GetMapping("/list-role-menus")
41     @PreAuthorize("@ss.hasPermission('system:permission:assign-role-menu')")
42     public CommonResult<Set<Long>> getRoleMenuList(Long roleId) {
43         return success(permissionService.getRoleMenuListByRoleId(roleId));
44     }
45
818a01 46     @Operation(summary = "获得角色拥有的菜单编号")
H 47     @Parameter(name = "roleId", description = "角色编号", required = true)
48     @GetMapping("/list-role-app-menus")
49     @PreAuthorize("@ss.hasPermission('system:permission:assign-role-menu')")
50     public CommonResult<Set<Long>> getRoleAppMenuList(Long roleId) {
51         return success(permissionService.getRoleAppMenuListByRoleId(roleId));
52     }
53
e7c126 54     @PostMapping("/assign-role-menu")
H 55     @Operation(summary = "赋予角色菜单")
56     @PreAuthorize("@ss.hasPermission('system:permission:assign-role-menu')")
57     public CommonResult<Boolean> assignRoleMenu(@Validated @RequestBody PermissionAssignRoleMenuReqVO reqVO) {
58         // 开启多租户的情况下,需要过滤掉未开通的菜单
59         tenantService.handleTenantMenu(menuIds -> reqVO.getMenuIds().removeIf(menuId -> !CollUtil.contains(menuIds, menuId)));
60
61         // 执行菜单的分配
62         permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds());
63         return success(true);
64     }
65
7da8f1 66 //    @PostMapping("/assign-role-app-menu")
H 67 //    @Operation(summary = "赋予角色菜单")
68 //    @PreAuthorize("@ss.hasPermission('system:permission:assign-role-menu')")
69 //    public CommonResult<Boolean> assignRoleAppMenu(@Validated @RequestBody PermissionAssignRoleMenuReqVO reqVO) {
70 //        // 执行菜单的分配
71 //        permissionService.assignRoleAppMenu(reqVO.getRoleId(), reqVO.getMenuIds());
72 //        return success(true);
73 //    }
818a01 74
e7c126 75     @PostMapping("/assign-role-data-scope")
H 76     @Operation(summary = "赋予角色数据权限")
77     @PreAuthorize("@ss.hasPermission('system:permission:assign-role-data-scope')")
78     public CommonResult<Boolean> assignRoleDataScope(@Valid @RequestBody PermissionAssignRoleDataScopeReqVO reqVO) {
79         permissionService.assignRoleDataScope(reqVO.getRoleId(), reqVO.getDataScope(), reqVO.getDataScopeDeptIds());
80         return success(true);
81     }
82
83     @Operation(summary = "获得管理员拥有的角色编号列表")
84     @Parameter(name = "userId", description = "用户编号", required = true)
85     @GetMapping("/list-user-roles")
86     @PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')")
87     public CommonResult<Set<Long>> listAdminRoles(@RequestParam("userId") Long userId) {
88         return success(permissionService.getUserRoleIdListByUserId(userId));
89     }
90
91     @Operation(summary = "赋予用户角色")
92     @PostMapping("/assign-user-role")
93     @PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')")
94     public CommonResult<Boolean> assignUserRole(@Validated @RequestBody PermissionAssignUserRoleReqVO reqVO) {
95         permissionService.assignUserRole(reqVO.getUserId(), reqVO.getRoleIds());
96         return success(true);
97     }
98
99 }