RoleController.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * BladeX Commercial License Agreement
  3. * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
  4. * <p>
  5. * Use of this software is governed by the Commercial License Agreement
  6. * obtained after purchasing a license from BladeX.
  7. * <p>
  8. * 1. This software is for development use only under a valid license
  9. * from BladeX.
  10. * <p>
  11. * 2. Redistribution of this software's source code to any third party
  12. * without a commercial license is strictly prohibited.
  13. * <p>
  14. * 3. Licensees may copyright their own code but cannot use segments
  15. * from this software for such purposes. Copyright of this software
  16. * remains with BladeX.
  17. * <p>
  18. * Using this software signifies agreement to this License, and the software
  19. * must not be used for illegal purposes.
  20. * <p>
  21. * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
  22. * not liable for any claims arising from secondary or illegal development.
  23. * <p>
  24. * Author: Chill Zhuang (bladejava@qq.com)
  25. */
  26. package org.springblade.modules.system.controller;
  27. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  28. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  29. import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
  30. import io.swagger.v3.oas.annotations.Operation;
  31. import io.swagger.v3.oas.annotations.Parameter;
  32. import io.swagger.v3.oas.annotations.Parameters;
  33. import io.swagger.v3.oas.annotations.enums.ParameterIn;
  34. import io.swagger.v3.oas.annotations.media.Schema;
  35. import io.swagger.v3.oas.annotations.tags.Tag;
  36. import jakarta.validation.Valid;
  37. import lombok.AllArgsConstructor;
  38. import org.springblade.common.cache.SysCache;
  39. import org.springblade.common.cache.UserCache;
  40. import org.springblade.core.boot.ctrl.BladeController;
  41. import org.springblade.core.cache.utils.CacheUtil;
  42. import org.springblade.core.launch.constant.AppConstant;
  43. import org.springblade.core.mp.support.Condition;
  44. import org.springblade.core.secure.BladeUser;
  45. import org.springblade.core.secure.annotation.PreAuth;
  46. import org.springblade.core.secure.constant.AuthConstant;
  47. import org.springblade.core.tenant.annotation.NonDS;
  48. import org.springblade.core.tool.api.R;
  49. import org.springblade.core.tool.constant.BladeConstant;
  50. import org.springblade.core.tool.constant.RoleConstant;
  51. import org.springblade.core.tool.utils.Func;
  52. import org.springblade.modules.system.pojo.entity.Role;
  53. import org.springblade.modules.system.pojo.entity.User;
  54. import org.springblade.modules.system.service.IRoleService;
  55. import org.springblade.modules.system.pojo.vo.GrantVO;
  56. import org.springblade.modules.system.pojo.vo.RoleVO;
  57. import org.springblade.modules.system.wrapper.RoleWrapper;
  58. import org.springframework.web.bind.annotation.*;
  59. import java.util.ArrayList;
  60. import java.util.List;
  61. import java.util.Map;
  62. import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
  63. /**
  64. * 控制器
  65. *
  66. * @author Chill
  67. */
  68. @NonDS
  69. @RestController
  70. @AllArgsConstructor
  71. @RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/role")
  72. @Tag(name = "角色", description = "角色")
  73. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  74. public class RoleController extends BladeController {
  75. private final IRoleService roleService;
  76. /**
  77. * 详情
  78. */
  79. @GetMapping("/detail")
  80. @ApiOperationSupport(order = 1)
  81. @Operation(summary = "详情", description = "传入role")
  82. public R<RoleVO> detail(Role role) {
  83. Role detail = roleService.getOne(Condition.getQueryWrapper(role));
  84. return R.data(RoleWrapper.build().entityVO(detail));
  85. }
  86. /**
  87. * 列表
  88. */
  89. @GetMapping("/list")
  90. @Parameters({
  91. @Parameter(name = "roleName", description = "参数名称", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
  92. @Parameter(name = "roleAlias", description = "角色别名", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
  93. })
  94. @ApiOperationSupport(order = 2)
  95. @Operation(summary = "列表", description = "传入role")
  96. public R<List<RoleVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> role, BladeUser bladeUser) {
  97. QueryWrapper<Role> queryWrapper = Condition.getQueryWrapper(role, Role.class);
  98. List<Role> list = roleService.list((!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(Role::getTenantId, bladeUser.getTenantId()) : queryWrapper);
  99. return R.data(RoleWrapper.build().listNodeVO(list));
  100. }
  101. /**
  102. * 获取角色树形结构
  103. */
  104. @GetMapping("/tree")
  105. @ApiOperationSupport(order = 3)
  106. @Operation(summary = "树形结构", description = "树形结构")
  107. public R<List<RoleVO>> tree(String tenantId, BladeUser bladeUser) {
  108. List<RoleVO> tree = roleService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
  109. return R.data(tree);
  110. }
  111. /**
  112. * 获取指定角色树形结构
  113. */
  114. @GetMapping("/tree-by-id")
  115. @ApiOperationSupport(order = 4)
  116. @Operation(summary = "树形结构", description = "树形结构")
  117. public R<List<RoleVO>> treeById(Long roleId, BladeUser bladeUser) {
  118. Role role = SysCache.getRole(roleId);
  119. List<RoleVO> tree = roleService.tree(Func.notNull(role) ? role.getTenantId() : bladeUser.getTenantId());
  120. return R.data(tree);
  121. }
  122. /**
  123. * 新增或修改
  124. */
  125. @PostMapping("/submit")
  126. @ApiOperationSupport(order = 5)
  127. @Operation(summary = "新增或修改", description = "传入role")
  128. public R submit(@Valid @RequestBody Role role) {
  129. CacheUtil.clear(SYS_CACHE);
  130. CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
  131. return R.status(roleService.submit(role));
  132. }
  133. /**
  134. * 删除
  135. */
  136. @PostMapping("/remove")
  137. @ApiOperationSupport(order = 6)
  138. @Operation(summary = "删除", description = "传入ids")
  139. public R remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
  140. CacheUtil.clear(SYS_CACHE);
  141. CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
  142. return R.status(roleService.removeRole(ids));
  143. }
  144. /**
  145. * 设置角色权限
  146. */
  147. @PostMapping("/grant")
  148. @ApiOperationSupport(order = 7)
  149. @Operation(summary = "权限设置", description = "传入roleId集合以及menuId集合")
  150. public R grant(@RequestBody GrantVO grantVO) {
  151. CacheUtil.clear(SYS_CACHE);
  152. CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
  153. List<Long> roleIds = grantVO.getRoleIds();
  154. if(roleIds!=null && Func.isNull(roleIds.get(0))){
  155. Role role = new Role();
  156. role.setParentId(0L);
  157. role.setRoleName(grantVO.getRoleName());
  158. roleService.submit(role);
  159. roleIds = new ArrayList<>();
  160. roleIds.add(role.getId());
  161. }
  162. boolean temp = roleService.grant(roleIds, grantVO.getMenuIds(), grantVO.getDataScopeIds(), grantVO.getApiScopeIds());
  163. return R.status(temp);
  164. }
  165. /**
  166. * 下拉数据源
  167. */
  168. @PreAuth(AuthConstant.PERMIT_ALL)
  169. @GetMapping("/select")
  170. @ApiOperationSupport(order = 8)
  171. @Operation(summary = "下拉数据源", description = "传入id集合")
  172. public R<List<Role>> select(Long userId, String roleId) {
  173. if (Func.isNotEmpty(userId)) {
  174. User user = UserCache.getUser(userId);
  175. roleId = user.getRoleId();
  176. }
  177. List<Role> list = roleService.list(Wrappers.<Role>lambdaQuery().in(Role::getId, Func.toLongList(roleId)));
  178. return R.data(list);
  179. }
  180. }