StandingController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package jnpf.permission.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.dev33.satoken.annotation.SaMode;
  4. import io.swagger.v3.oas.annotations.Operation;
  5. import io.swagger.v3.oas.annotations.Parameter;
  6. import io.swagger.v3.oas.annotations.Parameters;
  7. import io.swagger.v3.oas.annotations.tags.Tag;
  8. import jakarta.validation.Valid;
  9. import jnpf.base.ActionResult;
  10. import jnpf.base.vo.PageListVO;
  11. import jnpf.base.vo.PaginationVO;
  12. import jnpf.constant.MsgCode;
  13. import jnpf.constant.PermissionConst;
  14. import jnpf.permission.entity.*;
  15. import jnpf.permission.model.standing.*;
  16. import jnpf.permission.model.usergroup.GroupInfoVO;
  17. import jnpf.permission.service.*;
  18. import jnpf.util.JsonUtil;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.web.bind.annotation.*;
  22. import java.util.*;
  23. import java.util.stream.Collectors;
  24. /**
  25. * 身份管理控制器
  26. *
  27. * @author JNPF开发平台组
  28. * @version v6.0.0
  29. * @copyright 引迈信息技术有限公司
  30. * @date 2025/3/4 18:25:30
  31. */
  32. @Tag(name = "身份管理控制器", description = "Standing")
  33. @RestController
  34. @RequestMapping("/api/permission/Standing")
  35. @Slf4j
  36. public class StandingController {
  37. @Autowired
  38. private StandingService standingService;
  39. @Autowired
  40. private AuthorizeService authorizeService;
  41. @Autowired
  42. private UserService userService;
  43. @Autowired
  44. private UserRelationService userRelationService;
  45. @Autowired
  46. private RoleRelationService roleRelationService;
  47. @Autowired
  48. private RoleService roleService;
  49. @Autowired
  50. private PositionService positionService;
  51. @Autowired
  52. private OrganizeService organizeService;
  53. @Operation(summary = "列表")
  54. @SaCheckPermission(value = {"permission.identity"})
  55. @GetMapping
  56. public ActionResult<PageListVO<StandingVO>> list(StandingPagination pagination) {
  57. List<StandingEntity> list = standingService.getList(pagination);
  58. List<StandingVO> listVo = JsonUtil.getJsonToList(list, StandingVO.class);
  59. for (StandingVO item : listVo) {
  60. item.setIsSystem(Objects.equals(item.getIsSystem(), 1) ? 1 : 0);
  61. }
  62. return ActionResult.page(listVo, null);
  63. }
  64. @Operation(summary = "创建")
  65. @Parameters({
  66. @Parameter(name = "StandingForm", description = "新建模型", required = true)
  67. })
  68. @SaCheckPermission(value = {"permission.identity"})
  69. @PostMapping
  70. public ActionResult create(@RequestBody @Valid StandingForm form) {
  71. StandingEntity entity = JsonUtil.getJsonToBean(form, StandingEntity.class);
  72. // 判断名称是否重复
  73. if (standingService.isExistByFullName(entity.getFullName(), null)) {
  74. return ActionResult.fail(MsgCode.EXIST001.get());
  75. }
  76. if (standingService.isExistByEnCode(entity.getEnCode(), null)) {
  77. return ActionResult.fail(MsgCode.EXIST002.get());
  78. }
  79. standingService.crete(entity);
  80. return ActionResult.success(MsgCode.SU001.get());
  81. }
  82. @Operation(summary = "更新")
  83. @Parameters({
  84. @Parameter(name = "id", description = "主键", required = true),
  85. @Parameter(name = "StandingForm", description = "修改模型", required = true)
  86. })
  87. @SaCheckPermission(value = {"permission.identity"})
  88. @PutMapping("/{id}")
  89. public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid StandingForm form) {
  90. StandingEntity info = standingService.getInfo(id);
  91. if (info == null) {
  92. return ActionResult.fail(MsgCode.FA002.get());
  93. }
  94. StandingEntity entity = JsonUtil.getJsonToBean(form, StandingEntity.class);
  95. // 判断名称是否重复
  96. if (standingService.isExistByFullName(entity.getFullName(), id)) {
  97. return ActionResult.fail(MsgCode.EXIST001.get());
  98. }
  99. if (standingService.isExistByEnCode(entity.getEnCode(), id)) {
  100. return ActionResult.fail(MsgCode.EXIST002.get());
  101. }
  102. standingService.update(id, entity);
  103. return ActionResult.success(MsgCode.SU004.get());
  104. }
  105. @Operation(summary = "信息")
  106. @Parameters({
  107. @Parameter(name = "id", description = "主键", required = true)
  108. })
  109. @SaCheckPermission(value = {"permission.identity"})
  110. @GetMapping("/{id}")
  111. public ActionResult<StandingInfoVO> info(@PathVariable("id") String id) {
  112. StandingEntity entity = standingService.getInfo(id);
  113. StandingInfoVO vo = JsonUtil.getJsonToBean(entity, StandingInfoVO.class);
  114. return ActionResult.success(vo);
  115. }
  116. @Operation(summary = "删除")
  117. @Parameters({
  118. @Parameter(name = "id", description = "主键", required = true)
  119. })
  120. @SaCheckPermission(value = {"permission.identity"})
  121. @DeleteMapping("/{id}")
  122. public ActionResult delete(@PathVariable("id") String id) {
  123. StandingEntity entity = standingService.getInfo(id);
  124. if (entity == null) {
  125. return ActionResult.fail(MsgCode.FA003.get());
  126. }
  127. // todo 删除绑定
  128. standingService.delete(entity);
  129. return ActionResult.success(MsgCode.SU003.get());
  130. }
  131. //++++++++++++++++++++++++++++++++动作start++++++++++++++++++++++++++++++++++++++
  132. @Operation(summary = "添加岗位或用户角色")
  133. @Parameters({
  134. @Parameter(name = "id", description = "主键", required = true),
  135. @Parameter(name = "userIdModel", description = "参数对象", required = false)
  136. })
  137. @SaCheckPermission(value = {"permission.user", "permission.identity"}, mode = SaMode.OR)
  138. @PostMapping("{id}/Actions/AddObject")
  139. public ActionResult<GroupInfoVO> addObject(@PathVariable("id") String id, @RequestBody @Valid StandingActionForm model) {
  140. //AuthorizeEntity objectType:standing身份绑定类型itemType:role/position
  141. StandingEntity info = standingService.getInfo(id);
  142. List<String> ids = model.getIds();
  143. List<String> authorizeList = authorizeService.getListByObjectAndItem(id, model.getType())
  144. .stream().map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  145. if (info != null && ids.size() > 0) {
  146. for (String thisId : ids) {
  147. if (!authorizeList.contains(thisId)) {
  148. AuthorizeEntity authorize = new AuthorizeEntity();
  149. authorize.setObjectId(thisId);
  150. authorize.setObjectType(model.getType());
  151. authorize.setItemId(id);
  152. authorize.setItemType(PermissionConst.STAND);
  153. authorizeService.save(authorize);
  154. }
  155. }
  156. }
  157. List<AuthorizeEntity> standList = authorizeService.getAuthorizeByItem(PermissionConst.STAND, id);
  158. List<String> posIds = standList.stream().filter(t -> PermissionConst.POSITION.equals(t.getObjectType())).map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  159. List<String> roleIds = standList.stream().filter(t -> PermissionConst.ROLE.equals(t.getObjectType())).map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  160. Set<String> userIds = new HashSet<>();
  161. userIds.addAll(userRelationService.getListByObjectIdAll(posIds).stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
  162. userIds.addAll(roleRelationService.getListByRoleId(roleIds, PermissionConst.USER).stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toList()));
  163. userService.delCurUser(MsgCode.PS010.get(), new ArrayList<>(userIds));
  164. return ActionResult.success(MsgCode.SU018.get());
  165. }
  166. @Operation(summary = "移除岗位或用户角色")
  167. @Parameters({
  168. @Parameter(name = "id", description = "主键", required = true),
  169. @Parameter(name = "userIdModel", description = "参数对象", required = false)
  170. })
  171. @SaCheckPermission(value = {"permission.user", "permission.identity"}, mode = SaMode.OR)
  172. @PostMapping("{id}/Actions/DeleteObject")
  173. public ActionResult<GroupInfoVO> deleteObject(@PathVariable("id") String id, @RequestBody @Valid StandingActionForm model) {
  174. StandingEntity info = standingService.getInfo(id);
  175. String notRemove = "";
  176. List<String> codeList = Arrays.asList(PermissionConst.MANAGER_CODE, PermissionConst.DEVELOPER_CODE, PermissionConst.USER_CODE);
  177. if (codeList.contains(info.getEnCode())) {
  178. RoleEntity byEnCode = roleService.getByEnCode(info.getEnCode());
  179. notRemove = byEnCode != null ? byEnCode.getId() : "";
  180. }
  181. List<String> ids = model.getIds();
  182. if (info != null && ids.size() > 0) {
  183. //获取身份下的岗位或者用户角色(itemType)
  184. List<AuthorizeEntity> listByObjectId = authorizeService.getListByObjectAndItem(id, model.getType());
  185. List<String> itemIds = new ArrayList<>();
  186. for (AuthorizeEntity item : listByObjectId) {
  187. if (ids.contains(item.getObjectId()) && !Objects.equals(notRemove, item.getObjectId())) {
  188. authorizeService.removeById(item);
  189. itemIds.add(item.getObjectId());
  190. }
  191. }
  192. }
  193. List<AuthorizeEntity> standList = authorizeService.getAuthorizeByItem(PermissionConst.STAND, id);
  194. List<String> posIds = standList.stream().filter(t -> PermissionConst.POSITION.equals(t.getObjectType())).map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  195. List<String> roleIds = standList.stream().filter(t -> PermissionConst.ROLE.equals(t.getObjectType())).map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  196. if (PermissionConst.POSITION.equals(model.getType())) {
  197. posIds.addAll(ids);
  198. } else {
  199. roleIds.addAll(ids);
  200. }
  201. Set<String> userIds = new HashSet<>();
  202. userIds.addAll(userRelationService.getListByObjectIdAll(posIds).stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
  203. userIds.addAll(roleRelationService.getListByRoleId(roleIds, PermissionConst.USER).stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toList()));
  204. userService.delCurUser(MsgCode.PS010.get(), new ArrayList<>(userIds));
  205. return ActionResult.success(MsgCode.SU021.get());
  206. }
  207. //++++++++++++++++++++++++++++++++动作end++++++++++++++++++++++++++++++++++++++++
  208. @Operation(summary = "列表")
  209. @SaCheckPermission(value = {"permission.identity"})
  210. @GetMapping("/list")
  211. public ActionResult<PageListVO<StandingVO>> lists(StandingPagination pagination) {
  212. StandingEntity info = standingService.getInfo(pagination.getId());
  213. List<StandingVO> list = new ArrayList<>();
  214. //岗位、用户角色
  215. if (PermissionConst.POSITION.equals(pagination.getType())) {
  216. Map<String, Object> allOrgsTreeName = organizeService.getAllOrgsTreeName();
  217. List<PositionEntity> listByIds = standingService.getPosPage(pagination);
  218. for (PositionEntity pe : listByIds) {
  219. StandingVO vo = JsonUtil.getJsonToBean(pe, StandingVO.class);
  220. vo.setFullName(allOrgsTreeName.get(pe.getOrganizeId()) + "/" + pe.getFullName());
  221. list.add(vo);
  222. }
  223. } else {
  224. List<RoleEntity> listByIds = standingService.getRolePage(pagination);
  225. list = JsonUtil.getJsonToList(listByIds, StandingVO.class);
  226. for (StandingVO vo : list) {
  227. vo.setIsSystem(Objects.equals(vo.getGlobalMark(), 1) ? 1 : 0);
  228. if (Objects.equals(info.getIsSystem(), 1) && info.getEnCode().equals(vo.getEnCode())) {
  229. vo.setDisable(1);
  230. }
  231. }
  232. }
  233. PaginationVO jsonToBean = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  234. return ActionResult.page(list, jsonToBean);
  235. }
  236. }