AuthorizeController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. package jnpf.permission.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.dev33.satoken.annotation.SaMode;
  4. import cn.hutool.core.collection.CollectionUtil;
  5. import io.swagger.v3.oas.annotations.Operation;
  6. import io.swagger.v3.oas.annotations.Parameter;
  7. import io.swagger.v3.oas.annotations.Parameters;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import jnpf.annotation.OrganizeAdminIsTrator;
  10. import jnpf.base.ActionResult;
  11. import jnpf.base.UserInfo;
  12. import jnpf.base.controller.SuperController;
  13. import jnpf.base.entity.PrintDevEntity;
  14. import jnpf.base.entity.SystemEntity;
  15. import jnpf.base.model.portalManage.PortalListVO;
  16. import jnpf.base.model.portalManage.PortalVO;
  17. import jnpf.base.model.portalManage.SavePortalAuthModel;
  18. import jnpf.base.model.print.PaginationPrint;
  19. import jnpf.base.service.PrintDevService;
  20. import jnpf.base.service.SystemService;
  21. import jnpf.base.vo.ListVO;
  22. import jnpf.constant.AuthorizeConst;
  23. import jnpf.constant.MsgCode;
  24. import jnpf.constant.PermissionConst;
  25. import jnpf.flowable.entity.TemplateEntity;
  26. import jnpf.flowable.model.template.TemplateTreeListVo;
  27. import jnpf.permission.entity.*;
  28. import jnpf.permission.model.authorize.*;
  29. import jnpf.permission.model.columnspurview.ColumnsPurviewUpForm;
  30. import jnpf.permission.service.*;
  31. import jnpf.permission.util.AuthPermUtil;
  32. import jnpf.util.JsonUtil;
  33. import jnpf.util.StringUtil;
  34. import jnpf.util.UserProvider;
  35. import jnpf.util.context.RequestContext;
  36. import jnpf.workflow.service.TemplateApi;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.web.bind.annotation.*;
  39. import java.util.ArrayList;
  40. import java.util.Comparator;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.stream.Collectors;
  44. /**
  45. * 操作权限
  46. *
  47. * @author JNPF开发平台组
  48. * @version V3.1.0
  49. * @copyright 引迈信息技术有限公司
  50. * @date 2019年9月26日 上午9:18
  51. */
  52. @Tag(name = "操作权限", description = "Authorize")
  53. @RestController
  54. @RequestMapping("/api/permission/Authority")
  55. public class AuthorizeController extends SuperController<AuthorizeService, AuthorizeEntity> {
  56. @Autowired
  57. private AuthorizeService authorizeService;
  58. @Autowired
  59. private ColumnsPurviewService columnsPurviewService;
  60. @Autowired
  61. private PermissionGroupService permissionGroupService;
  62. @Autowired
  63. private SystemService systemApi;
  64. @Autowired
  65. private TemplateApi templateApi;
  66. @Autowired
  67. private PrintDevService printDevService;
  68. @Autowired
  69. private AuthPermUtil authPermUtil;
  70. @Autowired
  71. private OrganizeService organizeService;
  72. @Autowired
  73. private PositionService positionService;
  74. @Autowired
  75. private RoleRelationService roleRelationService;
  76. @Operation(summary = "获取岗位/角色/用户权限树形结构")
  77. @Parameters({
  78. @Parameter(name = "objectId", description = "对象主键", required = true),
  79. @Parameter(name = "dataValuesQuery", description = "权限值", required = true)
  80. })
  81. @SaCheckPermission(value = {"permission.auth", "permission.role"}, mode = SaMode.OR)
  82. @PostMapping("/Data/{objectId}/Values")
  83. public ActionResult<AuthorizeDataReturnVO> getValuesData(@PathVariable("objectId") String objectId, @RequestBody DataValuesQuery dataValuesQuery) {
  84. if (!StringUtil.isEmpty(dataValuesQuery.getType())) {
  85. AuthorizeParam authorizeParam = AuthorizeParam.builder()
  86. .appCode(RequestContext.getAppCode())
  87. .objectId(objectId)
  88. .objectType(dataValuesQuery.getObjectType())
  89. .itemType(dataValuesQuery.getType())
  90. .moduleIds(dataValuesQuery.getModuleIds())
  91. .build();
  92. AuthorizeDataReturnVO dataRes = authPermUtil.getAuthMenuList(authorizeParam);
  93. return ActionResult.success(dataRes);
  94. }
  95. return ActionResult.fail(MsgCode.PS012.get());
  96. }
  97. /**
  98. * 对象数据
  99. *
  100. * @return
  101. */
  102. @Operation(summary = "获取功能权限数据")
  103. @Parameters({
  104. @Parameter(name = "itemId", description = "对象主键", required = true),
  105. @Parameter(name = "objectType", description = "对象类型", required = true)
  106. })
  107. @SaCheckPermission(value = {"permission.auth", "permission.role", "onlineDev.visualPortal"}, mode = SaMode.OR)
  108. @GetMapping("/Model/{itemId}/{objectType}")
  109. public ActionResult<AuthorizeItemObjIdsVO> getObjectAuth(@PathVariable("itemId") String itemId, @PathVariable("objectType") String objectType) {
  110. List<AuthorizeEntity> authorizeList = authorizeService.getListByObjectAndItem(itemId, objectType);
  111. List<String> ids = authorizeList.stream().map(u -> u.getObjectId()).collect(Collectors.toList());
  112. AuthorizeItemObjIdsVO vo = new AuthorizeItemObjIdsVO();
  113. vo.setIds(ids);
  114. return ActionResult.success(vo);
  115. }
  116. @Operation(summary = "门户管理授权")
  117. @Parameters({
  118. @Parameter(name = "itemId", description = "对象主键", required = true),
  119. @Parameter(name = "saveAuthForm", description = "保存权限模型", required = true)})
  120. @PutMapping("/Model/{portalManageId}")
  121. @SaCheckPermission(value = {"permission.auth", "permission.role"}, mode = SaMode.OR)
  122. public ActionResult savePortalManage(@PathVariable("portalManageId") String portalManageId, @RequestBody SavePortalAuthModel model) {
  123. model.setId(portalManageId);
  124. model.setType(AuthorizeConst.AUTHORIZE_PORTAL_MANAGE);
  125. model.setIds(model.getObjectId());
  126. authorizeService.saveObjectAuth(model);
  127. return ActionResult.success(MsgCode.SU005.get());
  128. }
  129. /**
  130. * 保存
  131. *
  132. * @param objectId 对象主键
  133. * @param authorizeDataUpForm 修改权限模型
  134. * @return
  135. */
  136. @OrganizeAdminIsTrator
  137. @Operation(summary = "保存权限")
  138. @Parameters({
  139. @Parameter(name = "objectId", description = "对象主键", required = true),
  140. @Parameter(name = "authorizeDataUpForm", description = "修改权限模型", required = true)
  141. })
  142. @SaCheckPermission(value = {"permission.auth", "permission.role"}, mode = SaMode.OR)
  143. @PutMapping("/Data/{objectId}")
  144. public ActionResult save(@PathVariable("objectId") String objectId, @RequestBody AuthorizeDataUpForm authorizeDataUpForm) {
  145. authorizeDataUpForm.setObjectId(objectId);
  146. String err = authorizeService.save(authorizeDataUpForm);
  147. if (StringUtil.isNotEmpty(err)) {
  148. return ActionResult.success(err);
  149. }
  150. return ActionResult.success(MsgCode.SU005.get());
  151. }
  152. /**
  153. * 获取模块列表展示字段
  154. *
  155. * @param moduleId 菜单Id
  156. * @return
  157. */
  158. @Operation(summary = "获取模块列表展示字段")
  159. @Parameters({
  160. @Parameter(name = "moduleId", description = "菜单id", required = true)
  161. })
  162. @GetMapping("/GetColumnsByModuleId/{moduleId}")
  163. public ActionResult getColumnsByModuleId(@PathVariable("moduleId") String moduleId) {
  164. ColumnsPurviewEntity entity = columnsPurviewService.getInfo(moduleId);
  165. List<Map<String, Object>> jsonToListMap = null;
  166. if (entity != null) {
  167. jsonToListMap = JsonUtil.getJsonToListMap(entity.getFieldList());
  168. }
  169. return ActionResult.success(jsonToListMap != null ? jsonToListMap : new ArrayList<>(16));
  170. }
  171. /**
  172. * 配置模块列表展示字段
  173. *
  174. * @param columnsPurviewUpForm 修改模型
  175. * @return
  176. */
  177. @Operation(summary = "配置模块列表展示字段")
  178. @Parameters({
  179. @Parameter(name = "columnsPurviewUpForm", description = "修改模型", required = true)
  180. })
  181. @PutMapping("/SetColumnsByModuleId")
  182. public ActionResult setColumnsByModuleId(@RequestBody ColumnsPurviewUpForm columnsPurviewUpForm) {
  183. ColumnsPurviewEntity entity = JsonUtil.getJsonToBean(columnsPurviewUpForm, ColumnsPurviewEntity.class);
  184. columnsPurviewService.update(columnsPurviewUpForm.getModuleId(), entity);
  185. return ActionResult.success(MsgCode.SU005.get());
  186. }
  187. /**
  188. * 查看流程权限组
  189. *
  190. * @param id 主键
  191. * @return
  192. */
  193. @Operation(summary = "查看流程权限组")
  194. @Parameter(name = "id", description = "主键", required = true)
  195. @GetMapping("/GroupFlow/{id}")
  196. public ActionResult<ListVO<String>> groupFlow(@PathVariable("id") String id) {
  197. List<PermissionGroupEntity> data = permissionGroupService.list(true, null);
  198. List<String> objectId = data.stream().map(PermissionGroupEntity::getId).collect(Collectors.toList());
  199. List<String> groupId = authorizeService.getListByRoleIdsAndItemType(objectId, AuthorizeConst.FLOW).stream().filter(t -> t.getItemId().equals(id)).map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  200. List<String> groupList = data.stream().filter(t -> groupId.contains(t.getId())).map(PermissionGroupEntity::getId).collect(Collectors.toList());
  201. ListVO listVO = new ListVO<>();
  202. listVO.setList(groupList);
  203. return ActionResult.success(listVO);
  204. }
  205. /**
  206. * 保存流程权限
  207. *
  208. * @return
  209. */
  210. @Operation(summary = "保存流程权限")
  211. @Parameters({
  212. @Parameter(name = "id", description = "对象主键", required = true)
  213. })
  214. @PostMapping("/GroupFlow/{id}")
  215. public ActionResult groupFlow(@PathVariable("id") String id, @RequestBody SavePortalAuthModel model) {
  216. model.setId(id);
  217. model.setType(AuthorizeConst.FLOW);
  218. authorizeService.saveObjectAuth(model);
  219. return ActionResult.success(MsgCode.SU005.get());
  220. }
  221. /**
  222. * 保存流程权限
  223. *
  224. * @return
  225. */
  226. @Operation(summary = "保存流程权限")
  227. @Parameters({
  228. @Parameter(name = "id", description = "对象主键", required = true)
  229. })
  230. @PostMapping("/Flow/{id}")
  231. public ActionResult saveFlowAuth(@PathVariable("id") String id, @RequestBody SavePortalAuthModel model) {
  232. model.setId(id);
  233. model.setType(AuthorizeConst.FLOW);
  234. authorizeService.saveItemAuth(model);
  235. return ActionResult.success(MsgCode.SU005.get());
  236. }
  237. /**
  238. * 获取流程权限
  239. *
  240. * @return
  241. */
  242. @Operation(summary = "获取流程权限")
  243. @Parameters({
  244. @Parameter(name = "id", description = "对象主键", required = true)
  245. })
  246. @GetMapping("/Flow/{id}")
  247. public ActionResult<PortalVO> getFlowAuth(@PathVariable("id") String id, @RequestParam("objectType") String objectType) {
  248. UserInfo userInfo = UserProvider.getUser();
  249. Boolean isAdmin = userInfo.getIsAdministrator();
  250. Boolean isManageRole = userInfo.getIsManageRole();
  251. Boolean isDevRole = userInfo.getIsDevRole();
  252. PortalVO vo = new PortalVO();
  253. //全部流程
  254. List<TemplateTreeListVo> treeList = templateApi.treeListWithPower();
  255. //上级权限传递(treeList移除无权限的)
  256. treeList = filterParent(id, objectType, treeList, AuthorizeConst.FLOW);
  257. // 当前权限组权限
  258. List<AuthorizeEntity> authorizePortalManage = authorizeService.getListByObjectId(id, AuthorizeConst.FLOW);
  259. List<String> ids = authorizePortalManage.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  260. List<String> idAll = new ArrayList<>();
  261. //当前用户拥有的权限
  262. if (isAdmin || isManageRole || isDevRole) {
  263. idList(treeList, idAll, null, ids);
  264. } else {
  265. List<AuthorizeEntity> list = authorizeService.getListByUserId(false, userInfo.getUserId(), true);
  266. List<String> itemIds = list.stream().filter(t -> AuthorizeConst.FLOW.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  267. itemIds.addAll(templateApi.getListByCreUser(userInfo.getUserId()).stream().map(TemplateEntity::getId).collect(Collectors.toList()));
  268. //没有权限的禁用
  269. idList(treeList, idAll, itemIds, ids);
  270. }
  271. vo.setAll(idAll);
  272. vo.setIds(ids);
  273. vo.setList(JsonUtil.getJsonToList(treeList, PortalListVO.class));
  274. return ActionResult.success(vo);
  275. }
  276. /**
  277. * 根据itemIds,禁用以外的选项
  278. *
  279. * @param list 树形结构数据
  280. * @param idAll 提取全部id
  281. * @param itemIds 非禁用id列表
  282. */
  283. private void idList(List<TemplateTreeListVo> list, List<String> idAll, List<String> itemIds, List<String> selectIds) {
  284. for (TemplateTreeListVo vo : list) {
  285. vo.setDisabled(false);
  286. if (vo.getChildren() != null) {
  287. idList(vo.getChildren(), idAll, itemIds, selectIds);
  288. //子集有被禁用且被选中的的,那么上级肯定被禁用,如果没有被选中,那么只有全部禁用的情况才禁用
  289. boolean isDisabled = false;
  290. int n = 0;
  291. for (TemplateTreeListVo child : vo.getChildren()) {
  292. if (child.getDisabled()) {
  293. if (selectIds.contains(child.getId())) {
  294. isDisabled = true;
  295. break;
  296. } else {
  297. n++;
  298. }
  299. }
  300. }
  301. if (n == vo.getChildren().size()) {
  302. isDisabled = true;
  303. }
  304. vo.setDisabled(isDisabled);
  305. } else {
  306. if (itemIds != null) {
  307. if (!itemIds.contains(vo.getId())) {
  308. vo.setDisabled(true);
  309. }
  310. }
  311. }
  312. idAll.add(vo.getId());
  313. }
  314. }
  315. //根据List<String> 过滤树形数据
  316. private List<TemplateTreeListVo> filter(List<TemplateTreeListVo> list, List<String> filterList) {
  317. List<TemplateTreeListVo> listRes = new ArrayList<>(list);
  318. for (TemplateTreeListVo vo : list) {
  319. if (!filterList.contains(vo.getId())) {
  320. listRes.remove(vo);
  321. } else {
  322. if (CollectionUtil.isNotEmpty(vo.getChildren())) {
  323. vo.setChildren(filter(vo.getChildren(), filterList));
  324. }
  325. }
  326. }
  327. return listRes;
  328. }
  329. //传递上级权限--过滤上级没有的权限
  330. private List<TemplateTreeListVo> filterParent(String id, String objectType, List<TemplateTreeListVo> treeList, String itemType) {
  331. boolean filter = false;
  332. List<String> filterList = new ArrayList<>();
  333. if (PermissionConst.ORGANIZE.equals(objectType)) {
  334. OrganizeEntity info = organizeService.getInfo(id);
  335. if (!"-1".equals(info.getParentId())) {
  336. filter = true;
  337. List<String> objectIds = new ArrayList<>();
  338. objectIds.add(info.getParentId());
  339. List<String> collect = roleRelationService.getListByObjectId(info.getParentId(), PermissionConst.ORGANIZE)
  340. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  341. objectIds.addAll(collect);
  342. filterList.addAll(authorizeService.getListByRoleIdsAndItemType(objectIds, itemType)
  343. .stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList()));
  344. }
  345. }
  346. if (PermissionConst.POSITION.equals(objectType)) {
  347. filter = true;
  348. PositionEntity info = positionService.getInfo(id);
  349. if (StringUtil.isEmpty(info.getParentId()) || "-1".equals(info.getParentId())) {
  350. //继承组织
  351. List<String> objectIds = new ArrayList<>();
  352. objectIds.add(info.getOrganizeId());
  353. List<String> collect = roleRelationService.getListByObjectId(info.getOrganizeId(), PermissionConst.ORGANIZE)
  354. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  355. objectIds.addAll(collect);
  356. filterList.addAll(authorizeService.getListByRoleIdsAndItemType(objectIds, itemType)
  357. .stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList()));
  358. } else {
  359. //继承上级岗位
  360. List<String> objectIds = new ArrayList<>();
  361. objectIds.add(info.getParentId());
  362. List<String> collect = roleRelationService.getListByObjectId(info.getParentId(), PermissionConst.POSITION)
  363. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  364. objectIds.addAll(collect);
  365. filterList.addAll(authorizeService.getListByRoleIdsAndItemType(objectIds, itemType)
  366. .stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList()));
  367. }
  368. }
  369. if (filter) {
  370. return filter(treeList, filterList);
  371. }
  372. return treeList;
  373. }
  374. /**
  375. * 保存打印权限
  376. *
  377. * @return
  378. */
  379. @Operation(summary = "保存打印权限")
  380. @Parameters({
  381. @Parameter(name = "id", description = "对象主键", required = true)
  382. })
  383. @PostMapping("/Print/{id}")
  384. public ActionResult savePrintAuth(@PathVariable("id") String id, @RequestBody SavePortalAuthModel model) {
  385. model.setId(id);
  386. model.setType(AuthorizeConst.PRINT);
  387. authorizeService.saveItemAuth(model);
  388. return ActionResult.success(MsgCode.SU005.get());
  389. }
  390. /**
  391. * 获取打印权限
  392. *
  393. * @return
  394. */
  395. @Operation(summary = "获取打印权限")
  396. @Parameters({
  397. @Parameter(name = "id", description = "对象主键", required = true)
  398. })
  399. @GetMapping("/Print/{id}")
  400. public ActionResult<PortalVO> getPrintAuth(@PathVariable("id") String id, @RequestParam("objectType") String objectType) {
  401. UserInfo userInfo = UserProvider.getUser();
  402. Boolean isAdmin = userInfo.getIsAdministrator();
  403. Boolean isManageRole = userInfo.getIsManageRole();
  404. Boolean isDevRole = userInfo.getIsDevRole();
  405. PortalVO vo = new PortalVO();
  406. PaginationPrint paginationPrint = new PaginationPrint();
  407. paginationPrint.setDataType(1);
  408. paginationPrint.setVisibleType(2);
  409. List<PrintDevEntity> list = printDevService.getWorkSelector(paginationPrint);
  410. List<String> systemIds = list.stream().map(PrintDevEntity::getSystemId).collect(Collectors.toList());
  411. List<SystemEntity> systemList = systemApi.getListByIds(systemIds, null);
  412. List<TemplateTreeListVo> treeList = new ArrayList<>();
  413. for (SystemEntity dict : systemList) {
  414. TemplateTreeListVo tree = JsonUtil.getJsonToBean(dict, TemplateTreeListVo.class);
  415. List<PrintDevEntity> childList = list.stream()
  416. .filter(e -> dict.getId().equals(e.getSystemId()))
  417. .sorted(Comparator.comparing(PrintDevEntity::getSortCode, Comparator.nullsLast(Comparator.naturalOrder()))
  418. .thenComparing(PrintDevEntity::getCreatorTime, Comparator.nullsLast(Comparator.reverseOrder()))).collect(Collectors.toList());
  419. if (childList.size() > 0) {
  420. List<TemplateTreeListVo> childListAll = new ArrayList<>();
  421. for (PrintDevEntity entity : childList) {
  422. TemplateTreeListVo user = JsonUtil.getJsonToBean(entity, TemplateTreeListVo.class);
  423. childListAll.add(user);
  424. }
  425. tree.setChildren(childListAll);
  426. treeList.add(tree);
  427. }
  428. }
  429. //上级权限传递(treeList移除无权限的)
  430. treeList = filterParent(id, objectType, treeList, AuthorizeConst.PRINT);
  431. // 当前权限组权限
  432. List<AuthorizeEntity> authorizePortalManage = authorizeService.getListByObjectId(id, AuthorizeConst.PRINT);
  433. List<String> ids = authorizePortalManage.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  434. List<String> idAll = new ArrayList<>();
  435. //当前用户拥有的权限--没有权限禁用
  436. if (isAdmin || isManageRole || isDevRole) {
  437. idList(treeList, idAll, null, ids);
  438. } else {
  439. List<AuthorizeEntity> lista = authorizeService.getListByUserId(false, userInfo.getUserId(), true);
  440. List<String> itemIds = lista.stream().filter(t -> AuthorizeConst.PRINT.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  441. itemIds.addAll(printDevService.getListByCreUser(userInfo.getUserId()).stream().map(PrintDevEntity::getId).collect(Collectors.toList()));
  442. //没有权限的禁用
  443. idList(treeList, idAll, itemIds, ids);
  444. }
  445. vo.setAll(idAll);
  446. vo.setIds(ids);
  447. vo.setList(JsonUtil.getJsonToList(treeList, PortalListVO.class));
  448. return ActionResult.success(vo);
  449. }
  450. }