houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
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
46     @PostMapping("/assign-role-menu")
47     @Operation(summary = "赋予角色菜单")
48     @PreAuthorize("@ss.hasPermission('system:permission:assign-role-menu')")
49     public CommonResult<Boolean> assignRoleMenu(@Validated @RequestBody PermissionAssignRoleMenuReqVO reqVO) {
50         // 开启多租户的情况下,需要过滤掉未开通的菜单
51         tenantService.handleTenantMenu(menuIds -> reqVO.getMenuIds().removeIf(menuId -> !CollUtil.contains(menuIds, menuId)));
52
53         // 执行菜单的分配
54         permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds());
55         return success(true);
56     }
57
58     @PostMapping("/assign-role-data-scope")
59     @Operation(summary = "赋予角色数据权限")
60     @PreAuthorize("@ss.hasPermission('system:permission:assign-role-data-scope')")
61     public CommonResult<Boolean> assignRoleDataScope(@Valid @RequestBody PermissionAssignRoleDataScopeReqVO reqVO) {
62         permissionService.assignRoleDataScope(reqVO.getRoleId(), reqVO.getDataScope(), reqVO.getDataScopeDeptIds());
63         return success(true);
64     }
65
66     @Operation(summary = "获得管理员拥有的角色编号列表")
67     @Parameter(name = "userId", description = "用户编号", required = true)
68     @GetMapping("/list-user-roles")
69     @PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')")
70     public CommonResult<Set<Long>> listAdminRoles(@RequestParam("userId") Long userId) {
71         return success(permissionService.getUserRoleIdListByUserId(userId));
72     }
73
74     @Operation(summary = "赋予用户角色")
75     @PostMapping("/assign-user-role")
76     @PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')")
77     public CommonResult<Boolean> assignUserRole(@Validated @RequestBody PermissionAssignUserRoleReqVO reqVO) {
78         permissionService.assignUserRole(reqVO.getUserId(), reqVO.getRoleIds());
79         return success(true);
80     }
81
82 }