AuthPermUtil.java 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. package jnpf.permission.util;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import jnpf.base.UserInfo;
  5. import jnpf.base.entity.*;
  6. import jnpf.base.model.base.SystemBaeModel;
  7. import jnpf.base.model.button.ButtonModel;
  8. import jnpf.base.model.column.ColumnModel;
  9. import jnpf.base.model.form.ModuleFormModel;
  10. import jnpf.base.model.module.ModuleModel;
  11. import jnpf.base.model.print.PaginationPrint;
  12. import jnpf.base.model.resource.ResourceModel;
  13. import jnpf.base.service.*;
  14. import jnpf.constant.AuthorizeConst;
  15. import jnpf.constant.CodeConst;
  16. import jnpf.constant.JnpfConst;
  17. import jnpf.constant.PermissionConst;
  18. import jnpf.flowable.entity.TemplateEntity;
  19. import jnpf.flowable.model.template.TemplateTreeListVo;
  20. import jnpf.permission.entity.*;
  21. import jnpf.permission.model.authorize.*;
  22. import jnpf.permission.model.user.UserAuthForm;
  23. import jnpf.permission.model.user.mod.UserAuthorizeModel;
  24. import jnpf.permission.model.user.vo.UserAuthorizeVO;
  25. import jnpf.permission.service.*;
  26. import jnpf.util.JsonUtil;
  27. import jnpf.util.StringUtil;
  28. import jnpf.util.UserProvider;
  29. import jnpf.util.treeutil.SumTree;
  30. import jnpf.util.treeutil.newtreeutil.TreeDotUtils;
  31. import jnpf.workflow.service.TemplateApi;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Component;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. /**
  37. * 权限查询列表
  38. *
  39. * @author JNPF开发平台组
  40. * @version v6.0.0
  41. * @copyright 引迈信息技术有限公司
  42. * @date 2025/3/19 14:59:51
  43. */
  44. @Component
  45. public class AuthPermUtil {
  46. @Autowired
  47. private AuthorizeService authorizeService;
  48. @Autowired
  49. private UserService userService;
  50. @Autowired
  51. private OrganizeService organizeService;
  52. @Autowired
  53. private PositionService positionService;
  54. @Autowired
  55. private RoleRelationService roleRelationService;
  56. @Autowired
  57. private SystemService systemApi;
  58. @Autowired
  59. private ModuleService moduleApi;
  60. @Autowired
  61. private ModuleButtonService buttonApi;
  62. @Autowired
  63. private ModuleColumnService columnApi;
  64. @Autowired
  65. private ModuleDataAuthorizeSchemeService schemeApi;
  66. @Autowired
  67. private ModuleFormService formApi;
  68. @Autowired
  69. private TemplateApi templateApi;
  70. @Autowired
  71. private PrintDevService printDevApi;
  72. public AuthorizeDataReturnVO getAuthMenuList(AuthorizeParam authorizeParam) {
  73. UserInfo userInfo = UserProvider.getUser();
  74. Boolean isManageRole = userInfo.getIsManageRole();
  75. Boolean isDevRole = userInfo.getIsDevRole();
  76. //当前用户可配置权限
  77. AuthorizeVO authorizeModel = authorizeService.getAuthorize(false, null, 0, true);
  78. //继承上级权限
  79. List<AuthorizeEntity> authorizeList = authorizeService.list(new QueryWrapper<AuthorizeEntity>().lambda().eq(AuthorizeEntity::getObjectId, authorizeParam.getObjectId()));
  80. //获取资源系统和菜单全数据
  81. setAllSysAndModule(authorizeParam);
  82. // 处理当前用户不可选的
  83. List<ModuleModel> authModelList = authorizeModel.getModuleList();
  84. List<SystemBaeModel> authSysList = authorizeModel.getSystemList();
  85. Set<String> noContainsIds = new HashSet<>();
  86. //额外添加用户创建的应用-及菜单
  87. List<SystemEntity> listByCreUser = systemApi.getListByCreUser(userInfo.getUserId());
  88. List<String> creSysIds = listByCreUser.stream().map(SystemEntity::getId).collect(Collectors.toList());
  89. authSysList.addAll(JsonUtil.getJsonToList(listByCreUser, SystemBaeModel.class));
  90. noContainsIds.addAll(moduleApi.getModuleBySystemIds(creSysIds, null, null, 1).stream().map(ModuleEntity::getId).collect(Collectors.toList()));
  91. //添加当前用户有的权限
  92. for (SystemBaeModel systemBaeModel : authSysList) {
  93. noContainsIds.add(systemBaeModel.getId());
  94. if (Objects.equals(systemBaeModel.getIsMain(), 1)) {
  95. noContainsIds.add(CodeConst.XTCD);
  96. } else {
  97. noContainsIds.add(CodeConst.YYCD);
  98. }
  99. }
  100. for (ModuleModel moduleModel : authModelList) {
  101. noContainsIds.add(moduleModel.getId());
  102. if (JnpfConst.WEB.equals(moduleModel.getCategory())) {
  103. noContainsIds.add(moduleModel.getSystemId() + "2");
  104. }
  105. if (JnpfConst.APP.equals(moduleModel.getCategory())) {
  106. noContainsIds.add(moduleModel.getSystemId() + "1");
  107. }
  108. }
  109. String moduleIds = authorizeParam.getModuleIds();
  110. List<String> selectIds = new ArrayList<>();
  111. if (StringUtil.isNotBlank(moduleIds)) {
  112. String[] split = moduleIds.split(",");
  113. selectIds.addAll(Arrays.asList(split));
  114. }
  115. //出系统菜单外其他类型的数据
  116. List<ModuleModel> otherMList = new ArrayList<>();
  117. // 选中的菜单
  118. List<String> ids = new ArrayList<>();
  119. //实际业务
  120. List<ModuleEntity> menuEntityList = authorizeParam.getMenuEntityList();
  121. Set<String> hasModule = new HashSet<>();
  122. switch (authorizeParam.getItemType()) {
  123. case AuthorizeConst.MODULE:
  124. ids = authorizeList.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  125. break;
  126. case AuthorizeConst.BUTTON:
  127. List<ModuleButtonEntity> buttonList = authorizeParam.getButtonEntityList();
  128. for (ModuleButtonEntity item : buttonList) {
  129. //菜单选中的数据过滤
  130. if (selectIds.contains(item.getModuleId())) {
  131. ModuleModel model = JsonUtil.getJsonToBean(item, ModuleModel.class);
  132. model.setParentId(item.getModuleId());
  133. otherMList.add(model);
  134. hasModule.add(item.getModuleId());
  135. }
  136. }
  137. //移除没有按钮的菜单
  138. menuEntityList = removeLeaf(menuEntityList, hasModule);
  139. authorizeParam.setMenuEntityList(menuEntityList);
  140. //处理选中的
  141. ids = authorizeList.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  142. //处理当前用户不可选的
  143. List<ButtonModel> authBtnList = authorizeModel.getButtonList();
  144. authBtnList.stream().forEach(t -> noContainsIds.add(t.getId()));
  145. break;
  146. case AuthorizeConst.COLUMN:
  147. List<ModuleColumnEntity> columnEntityList = authorizeParam.getColumnEntityList();
  148. for (ModuleColumnEntity item : columnEntityList) {
  149. //菜单选中的数据过滤
  150. if (selectIds.contains(item.getModuleId())) {
  151. ModuleModel model = JsonUtil.getJsonToBean(item, ModuleModel.class);
  152. model.setParentId(item.getModuleId());
  153. otherMList.add(model);
  154. hasModule.add(item.getModuleId());
  155. }
  156. }
  157. //移除没有的菜单
  158. menuEntityList = removeLeaf(menuEntityList, hasModule);
  159. authorizeParam.setMenuEntityList(menuEntityList);
  160. //处理选中的
  161. ids = authorizeList.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  162. //处理当前用户不可选的
  163. List<ColumnModel> columnList = authorizeModel.getColumnList();
  164. columnList.stream().forEach(t -> noContainsIds.add(t.getId()));
  165. break;
  166. case AuthorizeConst.RESOURCE:
  167. List<ModuleDataAuthorizeSchemeEntity> resEntityList = authorizeParam.getResEntityList();
  168. for (ModuleDataAuthorizeSchemeEntity item : resEntityList) {
  169. //菜单选中的数据过滤
  170. if (selectIds.contains(item.getModuleId())) {
  171. ModuleModel model = JsonUtil.getJsonToBean(item, ModuleModel.class);
  172. model.setParentId(item.getModuleId());
  173. otherMList.add(model);
  174. hasModule.add(item.getModuleId());
  175. }
  176. }
  177. //移除没有的菜单
  178. menuEntityList = removeLeaf(menuEntityList, hasModule);
  179. authorizeParam.setMenuEntityList(menuEntityList);
  180. //处理选中的
  181. ids = authorizeList.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  182. //处理当前用户不可选的
  183. List<ResourceModel> resourceList = authorizeModel.getResourceList();
  184. resourceList.stream().forEach(t -> noContainsIds.add(t.getId()));
  185. break;
  186. case AuthorizeConst.FROM:
  187. List<ModuleFormEntity> formEntityList = authorizeParam.getFormEntityList();
  188. for (ModuleFormEntity item : formEntityList) {
  189. //菜单选中的数据过滤
  190. if (selectIds.contains(item.getModuleId())) {
  191. ModuleModel model = JsonUtil.getJsonToBean(item, ModuleModel.class);
  192. model.setParentId(item.getModuleId());
  193. otherMList.add(model);
  194. hasModule.add(item.getModuleId());
  195. }
  196. }
  197. //移除没有的菜单
  198. menuEntityList = removeLeaf(menuEntityList, hasModule);
  199. authorizeParam.setMenuEntityList(menuEntityList);
  200. //处理选中的
  201. ids = authorizeList.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  202. //处理当前用户不可选的
  203. List<ModuleFormModel> formsList = authorizeModel.getFormsList();
  204. formsList.stream().forEach(t -> noContainsIds.add(t.getId()));
  205. break;
  206. default:
  207. break;
  208. }
  209. //组装系统菜单数据
  210. List<ModuleModel> moduleList = new ArrayList<>(getModuleModels(authorizeParam));
  211. //添加其他类型数据
  212. moduleList.addAll(otherMList);
  213. //树形转换
  214. List<String> allIds = moduleList.stream().map(t -> t.getId()).collect(Collectors.toList());
  215. List<AuthorizeDataModel> treeList = JsonUtil.getJsonToList(moduleList, AuthorizeDataModel.class);
  216. treeList = treeList.stream().sorted(Comparator.comparing(AuthorizeDataModel::getSortCode, Comparator.nullsLast(Comparator.naturalOrder()))
  217. .thenComparing(AuthorizeDataModel::getCreatorTime, Comparator.nullsLast(Comparator.reverseOrder()))).collect(Collectors.toList());
  218. //递归禁用当前用户不可操作的数据
  219. Set<String> disList = new HashSet<>();
  220. treeList.forEach(t -> {
  221. if (!noContainsIds.contains(t.getId())) {
  222. t.setDisabled(true);
  223. disList.add(t.getId());
  224. }
  225. });
  226. setDisable(treeList, disList);
  227. List<SumTree<AuthorizeDataModel>> trees = TreeDotUtils.convertListToTreeDot(treeList, "-1");
  228. List<AuthorizeDataReturnModel> data = JsonUtil.getJsonToList(trees, AuthorizeDataReturnModel.class);
  229. if ((isManageRole || isDevRole)) {
  230. setDisableFalse(data, true);
  231. }
  232. setParentFalse(data, ids);
  233. AuthorizeDataReturnVO vo = new AuthorizeDataReturnVO();
  234. vo.setList(data);
  235. vo.setAll(allIds);
  236. vo.setIds(ids);
  237. return vo;
  238. }
  239. /**
  240. * 菜单添加上级
  241. *
  242. * @param authorizeParam
  243. * @return
  244. */
  245. private List<ModuleModel> getModuleModels(AuthorizeParam authorizeParam) {
  246. boolean isMain = JnpfConst.MAIN_SYSTEM_CODE.equals(authorizeParam.getAppCode()) ? true : false;
  247. List<SystemEntity> systemEntityList = authorizeParam.getSystemEntityList();
  248. List<ModuleEntity> menuEntityList = authorizeParam.getMenuEntityList();
  249. List<String> removeMenu = new ArrayList<>();
  250. removeMenu.addAll(JnpfConst.APP_CONFIG_MODULE);
  251. removeMenu.addAll(JnpfConst.ONLINE_DEV_MODULE);
  252. List<String> menuHasSystemId = menuEntityList.stream().map(ModuleEntity::getSystemId).collect(Collectors.toList());
  253. List<String> xtIds = new ArrayList<>();//系统
  254. List<ModuleModel> yyList = new ArrayList<>();//应用
  255. List<String> yyIds = new ArrayList<>();
  256. for (SystemEntity systemEntity : systemEntityList) {
  257. if (!menuHasSystemId.contains(systemEntity.getId())) continue;
  258. if (Objects.equals(systemEntity.getIsMain(), 1)) {
  259. xtIds.add(systemEntity.getId());
  260. } else {
  261. ModuleModel yymodel = JsonUtil.getJsonToBean(systemEntity, ModuleModel.class);
  262. yymodel.setParentId(CodeConst.YYCD);
  263. yyList.add(yymodel);
  264. yyIds.add(systemEntity.getId());
  265. }
  266. }
  267. List<ModuleModel> xtCdList = new ArrayList<>();//系统菜单
  268. List<ModuleModel> yyCdList = new ArrayList<>();//应用菜单
  269. Map<String, String> appIds = new HashMap<>(16);
  270. Map<String, String> webIds = new HashMap<>(16);
  271. for (ModuleEntity moduleEntity : menuEntityList) {
  272. if (removeMenu.contains(moduleEntity.getEnCode())) continue;
  273. ModuleModel t = JsonUtil.getJsonToBean(moduleEntity, ModuleModel.class);
  274. if (xtIds.contains(t.getSystemId())) {
  275. if ("-1".equals(t.getParentId())) {
  276. t.setParentId(CodeConst.XTCD);
  277. }
  278. xtCdList.add(t);
  279. } else {
  280. if (JnpfConst.APP.equals(t.getCategory()) && "-1".equals(t.getParentId())) {
  281. if (!appIds.containsKey(t.getSystemId())) {
  282. t.setParentId(t.getSystemId() + "1");
  283. ModuleModel appData = new ModuleModel();
  284. appData.setId(t.getSystemId() + "1");
  285. appData.setSortCode(0L);
  286. appData.setFullName("APP菜单");
  287. appData.setIcon(PermissionConst.APP_ICON);
  288. appData.setParentId(t.getSystemId());
  289. appData.setSystemId(t.getSystemId());
  290. yyCdList.add(appData);
  291. appIds.put(t.getSystemId(), appData.getId());
  292. } else {
  293. t.setParentId(appIds.get(t.getSystemId()) + "");
  294. }
  295. } else if (JnpfConst.WEB.equals(t.getCategory()) && "-1".equals(t.getParentId())) {
  296. if (!webIds.containsKey(t.getSystemId())) {
  297. t.setParentId(t.getSystemId() + "2");
  298. ModuleModel webData = new ModuleModel();
  299. webData.setId(t.getSystemId() + "2");
  300. webData.setSortCode(-1L);
  301. webData.setFullName("WEB菜单");
  302. webData.setIcon(PermissionConst.PC_ICON);
  303. webData.setParentId(t.getSystemId());
  304. webData.setSystemId(t.getSystemId());
  305. yyCdList.add(webData);
  306. webIds.put(t.getSystemId(), webData.getId());
  307. } else {
  308. t.setParentId(webIds.get(t.getSystemId()) + "");
  309. }
  310. }
  311. yyCdList.add(t);
  312. }
  313. }
  314. List<ModuleModel> modelList = new ArrayList<>();
  315. if (isMain) {
  316. ModuleModel moduleModel = new ModuleModel();
  317. moduleModel.setId(CodeConst.XTCD);
  318. moduleModel.setFullName("系统菜单");
  319. moduleModel.setParentId("-1");
  320. ModuleModel moduleModel2 = new ModuleModel();
  321. moduleModel2.setId(CodeConst.YYCD);
  322. moduleModel2.setFullName("应用菜单");
  323. moduleModel2.setParentId("-1");
  324. if (CollectionUtil.isNotEmpty(xtCdList)) {
  325. modelList.add(moduleModel);
  326. }
  327. if (CollectionUtil.isNotEmpty(yyCdList)) {
  328. modelList.add(moduleModel2);
  329. }
  330. modelList.addAll(yyList);
  331. modelList.addAll(xtCdList);
  332. modelList.addAll(yyCdList);
  333. } else {
  334. modelList.addAll(yyCdList);
  335. }
  336. return modelList;
  337. }
  338. /**
  339. * 获取当前 组织、岗位、角色拥有的全部系统和菜单权限
  340. */
  341. private void setAllSysAndModule(AuthorizeParam authorizeParam) {
  342. String objectId = authorizeParam.getObjectId();
  343. String objectType = authorizeParam.getObjectType();
  344. String itemType = authorizeParam.getItemType();
  345. boolean needFilter = true;//根据组织岗位角色判断是否需要过滤
  346. List<AuthorizeEntity> list = new ArrayList<>();
  347. if (PermissionConst.ORGANIZE.equals(objectType)) {
  348. OrganizeEntity info = organizeService.getInfo(objectId);
  349. if ("-1".equals(info.getParentId()) || StringUtil.isEmpty(info.getParentId())) {
  350. //全数据
  351. needFilter = false;
  352. } else {
  353. //获取父级组织和组织角色的全部授权菜单
  354. List<String> objectIds = new ArrayList<>();
  355. objectIds.add(info.getParentId());
  356. List<String> collect = roleRelationService.getListByObjectId(info.getParentId(), PermissionConst.ORGANIZE)
  357. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  358. objectIds.addAll(collect);
  359. list = authorizeService.list(new QueryWrapper<AuthorizeEntity>().lambda().in(AuthorizeEntity::getObjectId, objectIds));
  360. }
  361. }
  362. if (PermissionConst.POSITION.equals(objectType)) {
  363. PositionEntity info = positionService.getInfo(objectId);
  364. if ("-1".equals(info.getParentId()) || StringUtil.isEmpty(info.getParentId())) {
  365. List<String> objectIds = new ArrayList<>();
  366. objectIds.add(info.getOrganizeId());
  367. List<String> collect = roleRelationService.getListByObjectId(info.getOrganizeId(), PermissionConst.ORGANIZE)
  368. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  369. objectIds.addAll(collect);
  370. list = authorizeService.list(new QueryWrapper<AuthorizeEntity>().lambda().in(AuthorizeEntity::getObjectId, objectIds));
  371. } else {
  372. //获取父级岗位及岗位角色的全部授权信息
  373. List<String> objectIds = new ArrayList<>();
  374. objectIds.add(info.getParentId());
  375. List<String> collect = roleRelationService.getListByObjectId(info.getParentId(), PermissionConst.POSITION)
  376. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  377. objectIds.addAll(collect);
  378. list = authorizeService.list(new QueryWrapper<AuthorizeEntity>().lambda().in(AuthorizeEntity::getObjectId, objectIds));
  379. }
  380. }
  381. if (PermissionConst.ROLE.equals(objectType)) {
  382. //全数据
  383. needFilter = false;
  384. }
  385. if (needFilter) {
  386. List<String> systemList = list.stream().filter(t -> AuthorizeConst.SYSTEM.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  387. List<String> moduleList = list.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  388. authorizeParam.setSystemEntityList(systemApi.getListByIds(systemList, null)
  389. .stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  390. authorizeParam.setMenuEntityList(moduleApi.getModuleByIds(moduleList)
  391. .stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  392. if (AuthorizeConst.BUTTON.equals(itemType)) {
  393. List<String> itemIds = list.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  394. authorizeParam.setButtonEntityList(buttonApi.getListByIds(itemIds).stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  395. }
  396. if (AuthorizeConst.COLUMN.equals(itemType)) {
  397. List<String> itemIds = list.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  398. authorizeParam.setColumnEntityList(columnApi.getListByIds(itemIds).stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  399. }
  400. if (AuthorizeConst.RESOURCE.equals(itemType)) {
  401. List<String> itemIds = list.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  402. authorizeParam.setResEntityList(schemeApi.getListByIds(itemIds).stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  403. }
  404. if (AuthorizeConst.FROM.equals(itemType)) {
  405. List<String> itemIds = list.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  406. authorizeParam.setFormEntityList(formApi.getListByIds(itemIds).stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  407. }
  408. } else {
  409. authorizeParam.setSystemEntityList(systemApi.getList().stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  410. authorizeParam.setMenuEntityList(moduleApi.getList().stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  411. if (AuthorizeConst.BUTTON.equals(itemType)) {
  412. authorizeParam.setButtonEntityList(buttonApi.getList().stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  413. }
  414. if (AuthorizeConst.COLUMN.equals(itemType)) {
  415. authorizeParam.setColumnEntityList(columnApi.getList().stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  416. }
  417. if (AuthorizeConst.RESOURCE.equals(itemType)) {
  418. authorizeParam.setResEntityList(schemeApi.getList().stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  419. }
  420. if (AuthorizeConst.FROM.equals(itemType)) {
  421. authorizeParam.setFormEntityList(formApi.getList().stream().filter(t -> Objects.equals(t.getEnabledMark(), 1)).collect(Collectors.toList()));
  422. }
  423. }
  424. }
  425. /**
  426. * 递归获取包含的上级(根据下级移除没有下级的数据)
  427. *
  428. * @param list
  429. * @param hasIds
  430. * @return
  431. */
  432. public static List<ModuleEntity> removeLeaf(List<ModuleEntity> list, Set<String> hasIds) {
  433. List<ModuleEntity> res = new ArrayList<>();
  434. Set<String> newHasIds = new HashSet<>();
  435. for (ModuleEntity moduleEntity : list) {
  436. if (hasIds.contains(moduleEntity.getId())) {
  437. res.add(moduleEntity);
  438. newHasIds.add(moduleEntity.getParentId());
  439. }
  440. }
  441. if (CollectionUtil.isNotEmpty(newHasIds)) {
  442. res.addAll(removeLeaf(list, newHasIds));
  443. }
  444. return res;
  445. }
  446. private static void setDisable(List<AuthorizeDataModel> list, Set<String> disList) {
  447. Set<String> newIds = new HashSet<>();
  448. for (AuthorizeDataModel item : list) {
  449. if (disList.contains(item.getId())) {
  450. item.setDisabled(true);
  451. if (StringUtil.isNotEmpty(item.getParentId()) && !"-1".equals(item.getParentId())) {
  452. newIds.add(item.getParentId());
  453. }
  454. }
  455. }
  456. if (CollectionUtil.isNotEmpty(newIds)) {
  457. setDisable(list, newIds);
  458. }
  459. }
  460. /**
  461. * 管理员和开发者角色:应用部分菜单可授权
  462. *
  463. * @param list
  464. * @param isFirst 是否一级
  465. */
  466. private static void setDisableFalse(List<AuthorizeDataReturnModel> list, boolean isFirst) {
  467. for (AuthorizeDataReturnModel item : list) {
  468. if (!isFirst || CodeConst.YYCD.equals(item.getId())) {
  469. item.setDisabled(false);
  470. if (CollectionUtil.isNotEmpty(item.getChildren())) {
  471. setDisableFalse(item.getChildren(), false);
  472. }
  473. }
  474. }
  475. }
  476. private static void setParentFalse(List<AuthorizeDataReturnModel> list, List<String> ids) {
  477. for (AuthorizeDataReturnModel item : list) {
  478. if (CollectionUtil.isNotEmpty(item.getChildren())) {
  479. //先进底层设置
  480. setParentFalse(item.getChildren(), ids);
  481. //设置完后,设置当前层级问题
  482. List<AuthorizeDataReturnModel> children = item.getChildren();
  483. List<AuthorizeDataReturnModel> collect = children.stream().filter(t -> t.isDisabled()).collect(Collectors.toList());
  484. boolean disCheck = collect.stream().anyMatch(t -> ids.contains(t.getId()));
  485. //全部禁用不调整,禁用的没有被勾选--放开上级勾选
  486. if (children.size() != collect.size() && !disCheck) {
  487. item.setDisabled(false);
  488. }
  489. }
  490. }
  491. }
  492. /**
  493. * 递归获取需要删除的权限idlist
  494. *
  495. * @param param
  496. * @return
  497. */
  498. public static List<String> getDelAllAuth(AuthorizeSaveParam param) {
  499. List<String> listRes = new ArrayList<>();
  500. Map<String, List<AuthorizeEntity>> allAuthMap = param.getAllAuthMap();
  501. List<RoleRelationEntity> roleRealationList = param.getRoleRealationList();
  502. List<OrganizeEntity> allOrgList = param.getAllOrgList();
  503. List<PositionEntity> allPosList = param.getAllPosList();
  504. String objectType = param.getObjectType();
  505. String objectId = param.getObjectId();
  506. List<String> systemSave = new ArrayList<>(param.getSystemSave());
  507. List<String> moduleSave = new ArrayList<>(param.getModuleSave());
  508. List<String> buttonSave = new ArrayList<>(param.getButtonSave());
  509. List<String> columnSave = new ArrayList<>(param.getColumnSave());
  510. List<String> resourceSave = new ArrayList<>(param.getResourceSave());
  511. List<String> formSave = new ArrayList<>(param.getFormSave());
  512. if (PermissionConst.ORGANIZE.equals(objectType)) {
  513. OrganizeEntity organizeEntity = allOrgList.stream().filter(t -> objectId.equals(t.getId())).findFirst().orElse(null);
  514. if (organizeEntity != null) {
  515. List<String> roleIds = roleRealationList.stream().filter(t -> PermissionConst.ORGANIZE.equals(t.getObjectType())
  516. && objectId.equals(t.getObjectId())).map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  517. //本级角色拥有的下级不删除
  518. for (String roleId : roleIds) {
  519. //移除角色相关权限时,需要跳过当前权限
  520. if (StringUtil.isNotEmpty(param.getThisRole())) continue;
  521. //角色权限为空跳过
  522. if (allAuthMap.get(roleId) == null) continue;
  523. for (AuthorizeEntity t : allAuthMap.get(roleId)) {
  524. if (AuthorizeConst.SYSTEM.equals(t.getItemType()) && !systemSave.contains(t.getItemId()))
  525. systemSave.add(t.getItemId());
  526. if (AuthorizeConst.MODULE.equals(t.getItemType()) && !moduleSave.contains(t.getItemId()))
  527. moduleSave.add(t.getItemId());
  528. if (AuthorizeConst.BUTTON.equals(t.getItemType()) && !buttonSave.contains(t.getItemId()))
  529. buttonSave.add(t.getItemId());
  530. if (AuthorizeConst.COLUMN.equals(t.getItemType()) && !columnSave.contains(t.getItemId()))
  531. columnSave.add(t.getItemId());
  532. if (AuthorizeConst.RESOURCE.equals(t.getItemType()) && !resourceSave.contains(t.getItemId()))
  533. resourceSave.add(t.getItemId());
  534. if (AuthorizeConst.FROM.equals(t.getItemType()) && !formSave.contains(t.getItemId()))
  535. formSave.add(t.getItemId());
  536. }
  537. }
  538. //移除子组织权限
  539. List<OrganizeEntity> orgList = allOrgList.stream().filter(t -> objectId.equals(t.getParentId())).collect(Collectors.toList());
  540. if (CollectionUtil.isNotEmpty(orgList)) {
  541. for (OrganizeEntity entity : orgList) {
  542. //获取需要删除的权限关联数据id
  543. List<AuthorizeEntity> authorizeEntities = allAuthMap.get(entity.getId()) == null ? Collections.EMPTY_LIST : allAuthMap.get(entity.getId());
  544. List<String> delIds = authorizeEntities.stream().filter(t ->
  545. (AuthorizeConst.SYSTEM.equals(t.getItemType()) && !systemSave.contains(t.getItemId())) ||
  546. (AuthorizeConst.MODULE.equals(t.getItemType()) && !moduleSave.contains(t.getItemId())) ||
  547. (AuthorizeConst.BUTTON.equals(t.getItemType()) && !buttonSave.contains(t.getItemId())) ||
  548. (AuthorizeConst.COLUMN.equals(t.getItemType()) && !columnSave.contains(t.getItemId())) ||
  549. (AuthorizeConst.RESOURCE.equals(t.getItemType()) && !resourceSave.contains(t.getItemId())) ||
  550. (AuthorizeConst.FROM.equals(t.getItemType()) && !formSave.contains(t.getItemId()))).map(AuthorizeEntity::getId).collect(Collectors.toList());
  551. listRes.addAll(delIds);
  552. //递归子数据删除
  553. List<String> deleteAllAuth = AuthPermUtil.getDelAllAuth(AuthorizeSaveParam
  554. .builder().objectId(entity.getId()).objectType(PermissionConst.ORGANIZE).allOrgList(allOrgList).allPosList(allPosList).allAuthMap(allAuthMap)
  555. .roleRealationList(roleRealationList)
  556. .systemSave(authorizeEntities.stream().filter(t -> AuthorizeConst.SYSTEM.equals(t.getItemType()) && systemSave.contains(t.getItemId()))
  557. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  558. .moduleSave(authorizeEntities.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType()) && moduleSave.contains(t.getItemId()))
  559. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  560. .buttonSave(authorizeEntities.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType()) && buttonSave.contains(t.getItemId()))
  561. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  562. .columnSave(authorizeEntities.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType()) && columnSave.contains(t.getItemId()))
  563. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  564. .resourceSave(authorizeEntities.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType()) && resourceSave.contains(t.getItemId()))
  565. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  566. .formSave(authorizeEntities.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType()) && formSave.contains(t.getItemId()))
  567. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  568. .build());
  569. listRes.addAll(deleteAllAuth);
  570. }
  571. }
  572. //移除改组织下岗位的权限
  573. List<PositionEntity> posList = allPosList.stream().filter(t -> objectId.equals(t.getOrganizeId()) && StringUtil.isEmpty(t.getParentId())).collect(Collectors.toList());
  574. if (CollectionUtil.isNotEmpty(posList)) {
  575. for (PositionEntity entity : posList) {
  576. //获取需要删除的权限关联数据id
  577. List<AuthorizeEntity> authorizeEntities = allAuthMap.get(entity.getId()) == null ? Collections.EMPTY_LIST : allAuthMap.get(entity.getId());
  578. List<String> delIds = authorizeEntities.stream().filter(t ->
  579. (AuthorizeConst.SYSTEM.equals(t.getItemType()) && !systemSave.contains(t.getItemId())) ||
  580. (AuthorizeConst.MODULE.equals(t.getItemType()) && !moduleSave.contains(t.getItemId())) ||
  581. (AuthorizeConst.BUTTON.equals(t.getItemType()) && !buttonSave.contains(t.getItemId())) ||
  582. (AuthorizeConst.COLUMN.equals(t.getItemType()) && !columnSave.contains(t.getItemId())) ||
  583. (AuthorizeConst.RESOURCE.equals(t.getItemType()) && !resourceSave.contains(t.getItemId())) ||
  584. (AuthorizeConst.FROM.equals(t.getItemType()) && !formSave.contains(t.getItemId()))).map(AuthorizeEntity::getId).collect(Collectors.toList());
  585. listRes.addAll(delIds);
  586. List<String> deleteAllAuth = AuthPermUtil.getDelAllAuth(AuthorizeSaveParam
  587. .builder().objectId(entity.getId()).objectType(PermissionConst.POSITION).allOrgList(allOrgList).allPosList(allPosList).allAuthMap(allAuthMap)
  588. .roleRealationList(roleRealationList)
  589. .systemSave(authorizeEntities.stream().filter(t -> AuthorizeConst.SYSTEM.equals(t.getItemType()) && systemSave.contains(t.getItemId()))
  590. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  591. .moduleSave(authorizeEntities.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType()) && moduleSave.contains(t.getItemId()))
  592. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  593. .buttonSave(authorizeEntities.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType()) && buttonSave.contains(t.getItemId()))
  594. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  595. .columnSave(authorizeEntities.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType()) && columnSave.contains(t.getItemId()))
  596. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  597. .resourceSave(authorizeEntities.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType()) && resourceSave.contains(t.getItemId()))
  598. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  599. .formSave(authorizeEntities.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType()) && formSave.contains(t.getItemId()))
  600. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  601. .build());
  602. listRes.addAll(deleteAllAuth);
  603. }
  604. }
  605. }
  606. }
  607. if (PermissionConst.POSITION.equals(objectType)) {
  608. PositionEntity positionEntity = allPosList.stream().filter(t -> objectId.equals(t.getId())).findFirst().orElse(null);
  609. if (positionEntity != null) {
  610. List<String> roleIds = roleRealationList.stream().filter(t -> PermissionConst.POSITION.equals(t.getObjectType())
  611. && objectId.equals(t.getObjectId())).map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  612. //本级角色拥有的下级不删除
  613. for (String roleId : roleIds) {
  614. if (allAuthMap.get(roleId) == null) continue;
  615. for (AuthorizeEntity t : allAuthMap.get(roleId)) {
  616. if (AuthorizeConst.SYSTEM.equals(t.getItemType()) && !systemSave.contains(t.getItemId()))
  617. systemSave.add(t.getItemId());
  618. if (AuthorizeConst.MODULE.equals(t.getItemType()) && !moduleSave.contains(t.getItemId()))
  619. moduleSave.add(t.getItemId());
  620. if (AuthorizeConst.BUTTON.equals(t.getItemType()) && !buttonSave.contains(t.getItemId()))
  621. buttonSave.add(t.getItemId());
  622. if (AuthorizeConst.COLUMN.equals(t.getItemType()) && !columnSave.contains(t.getItemId()))
  623. columnSave.add(t.getItemId());
  624. if (AuthorizeConst.RESOURCE.equals(t.getItemType()) && !resourceSave.contains(t.getItemId()))
  625. resourceSave.add(t.getItemId());
  626. if (AuthorizeConst.FROM.equals(t.getItemType()) && !formSave.contains(t.getItemId()))
  627. formSave.add(t.getItemId());
  628. }
  629. }
  630. //移除下级岗位的权限
  631. List<PositionEntity> posList = allPosList.stream().filter(t -> objectId.equals(t.getParentId())).collect(Collectors.toList());
  632. if (CollectionUtil.isNotEmpty(posList)) {
  633. for (PositionEntity entity : posList) {
  634. //获取需要删除的权限关联数据id
  635. List<AuthorizeEntity> authorizeEntities = allAuthMap.get(entity.getId()) == null ? Collections.EMPTY_LIST : allAuthMap.get(entity.getId());
  636. List<String> delIds = authorizeEntities.stream().filter(t ->
  637. (AuthorizeConst.SYSTEM.equals(t.getItemType()) && !systemSave.contains(t.getItemId())) ||
  638. (AuthorizeConst.MODULE.equals(t.getItemType()) && !moduleSave.contains(t.getItemId())) ||
  639. (AuthorizeConst.BUTTON.equals(t.getItemType()) && !buttonSave.contains(t.getItemId())) ||
  640. (AuthorizeConst.COLUMN.equals(t.getItemType()) && !columnSave.contains(t.getItemId())) ||
  641. (AuthorizeConst.RESOURCE.equals(t.getItemType()) && !resourceSave.contains(t.getItemId())) ||
  642. (AuthorizeConst.FROM.equals(t.getItemType()) && !formSave.contains(t.getItemId()))).map(AuthorizeEntity::getId).collect(Collectors.toList());
  643. listRes.addAll(delIds);
  644. List<String> deleteAllAuth = AuthPermUtil.getDelAllAuth(AuthorizeSaveParam
  645. .builder().objectId(entity.getId()).objectType(PermissionConst.POSITION).allOrgList(allOrgList).allPosList(allPosList).allAuthMap(allAuthMap)
  646. .roleRealationList(roleRealationList)
  647. .systemSave(authorizeEntities.stream().filter(t -> AuthorizeConst.SYSTEM.equals(t.getItemType()) && systemSave.contains(t.getItemId()))
  648. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  649. .moduleSave(authorizeEntities.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType()) && moduleSave.contains(t.getItemId()))
  650. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  651. .buttonSave(authorizeEntities.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType()) && buttonSave.contains(t.getItemId()))
  652. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  653. .columnSave(authorizeEntities.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType()) && columnSave.contains(t.getItemId()))
  654. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  655. .resourceSave(authorizeEntities.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType()) && resourceSave.contains(t.getItemId()))
  656. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  657. .formSave(authorizeEntities.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType()) && formSave.contains(t.getItemId()))
  658. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  659. .build());
  660. listRes.addAll(deleteAllAuth);
  661. }
  662. }
  663. }
  664. }
  665. if (PermissionConst.ROLE.equals(objectType)) {
  666. List<RoleRelationEntity> rList = roleRealationList.stream().filter(t -> t.getRoleId().equals(objectId)).collect(Collectors.toList());
  667. if (CollectionUtil.isNotEmpty(rList)) {
  668. for (RoleRelationEntity item : rList) {
  669. if (PermissionConst.ORGANIZE.equals(item.getObjectType())) {
  670. OrganizeEntity organizeEntity = allOrgList.stream().filter(t -> item.getObjectId().equals(t.getId())).findFirst().orElse(null);
  671. if (organizeEntity != null) {
  672. List<AuthorizeEntity> authorizeEntities = allAuthMap.get(organizeEntity.getId()) == null ? Collections.EMPTY_LIST : allAuthMap.get(organizeEntity.getId());
  673. //当前不移除,移除子组织的相关权限
  674. List<String> deleteAllAuth = AuthPermUtil.getDelAllAuth(AuthorizeSaveParam
  675. .builder().objectId(organizeEntity.getId()).objectType(PermissionConst.ORGANIZE).allOrgList(allOrgList).allPosList(allPosList).allAuthMap(allAuthMap)
  676. .roleRealationList(roleRealationList)
  677. .systemSave(authorizeEntities.stream().filter(t -> AuthorizeConst.SYSTEM.equals(t.getItemType()) && systemSave.contains(t.getItemId()))
  678. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  679. .moduleSave(authorizeEntities.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType()) && moduleSave.contains(t.getItemId()))
  680. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  681. .buttonSave(authorizeEntities.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType()) && buttonSave.contains(t.getItemId()))
  682. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  683. .columnSave(authorizeEntities.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType()) && columnSave.contains(t.getItemId()))
  684. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  685. .resourceSave(authorizeEntities.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType()) && resourceSave.contains(t.getItemId()))
  686. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  687. .formSave(authorizeEntities.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType()) && formSave.contains(t.getItemId()))
  688. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  689. .thisRole(objectId)
  690. .build());
  691. listRes.addAll(deleteAllAuth);
  692. }
  693. } else {
  694. PositionEntity positionEntity = allPosList.stream().filter(t -> item.getObjectId().equals(t.getId())).findFirst().orElse(null);
  695. if (positionEntity != null) {
  696. List<AuthorizeEntity> authorizeEntities = allAuthMap.get(positionEntity.getId()) == null ? Collections.EMPTY_LIST : allAuthMap.get(positionEntity.getId());
  697. //当前不移除,移除子岗位的相关权限
  698. List<String> deleteAllAuth = AuthPermUtil.getDelAllAuth(AuthorizeSaveParam
  699. .builder().objectId(positionEntity.getId()).objectType(PermissionConst.ORGANIZE).allOrgList(allOrgList).allPosList(allPosList).allAuthMap(allAuthMap)
  700. .roleRealationList(roleRealationList)
  701. .systemSave(authorizeEntities.stream().filter(t -> AuthorizeConst.SYSTEM.equals(t.getItemType()) && systemSave.contains(t.getItemId()))
  702. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  703. .moduleSave(authorizeEntities.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType()) && moduleSave.contains(t.getItemId()))
  704. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  705. .buttonSave(authorizeEntities.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType()) && buttonSave.contains(t.getItemId()))
  706. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  707. .columnSave(authorizeEntities.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType()) && columnSave.contains(t.getItemId()))
  708. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  709. .resourceSave(authorizeEntities.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType()) && resourceSave.contains(t.getItemId()))
  710. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  711. .formSave(authorizeEntities.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType()) && formSave.contains(t.getItemId()))
  712. .map(AuthorizeEntity::getItemId).collect(Collectors.toList()))
  713. .thisRole(objectId)
  714. .build());
  715. listRes.addAll(deleteAllAuth);
  716. }
  717. }
  718. }
  719. }
  720. }
  721. return listRes;
  722. }
  723. /**
  724. * 个人权限
  725. *
  726. * @return
  727. */
  728. public UserAuthorizeVO getUserAuth(UserAuthForm param) {
  729. String userId = param.getUserId();
  730. if (StringUtil.isEmpty(param.getUserId())) {
  731. userId = UserProvider.getUser().getUserId();
  732. }
  733. UserEntity info = userService.getInfo(userId);
  734. if (info == null) return new UserAuthorizeVO();
  735. boolean isAdmin = Objects.equals(info.getIsAdministrator(), 1);
  736. boolean filter = isAdmin;
  737. List<AuthorizeEntity> list;
  738. if (PermissionConst.POSITION.equals(param.getObjectType()) || PermissionConst.ROLE.equals(param.getObjectType())) {
  739. list = authorizeService.getListByPosOrRoleId(param.getObjectId(), param.getObjectType());
  740. filter = false;
  741. } else {
  742. list = authorizeService.getListByUserId(false, userId, false);
  743. }
  744. List<AuthorizeEntity> systemAuthList = list.stream().filter(t -> AuthorizeConst.SYSTEM.equals(t.getItemType())).collect(Collectors.toList());
  745. List<AuthorizeEntity> moduleAuthList = list.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType())).collect(Collectors.toList());
  746. List<AuthorizeEntity> buttonAuthList = list.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType())).collect(Collectors.toList());
  747. List<AuthorizeEntity> columnAuthList = list.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType())).collect(Collectors.toList());
  748. List<AuthorizeEntity> formAuthList = list.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType())).collect(Collectors.toList());
  749. List<AuthorizeEntity> dataAuthList = list.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType())).collect(Collectors.toList());
  750. List<AuthorizeEntity> flowAuthList = list.stream().filter(t -> AuthorizeConst.FLOW.equals(t.getItemType())).collect(Collectors.toList());
  751. List<AuthorizeEntity> printAuthList = list.stream().filter(t -> AuthorizeConst.PRINT.equals(t.getItemType())).collect(Collectors.toList());
  752. List<String> systemIds = systemAuthList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  753. List<String> moduleIds = moduleAuthList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  754. List<String> buttonIds = buttonAuthList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  755. List<String> columnIds = columnAuthList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  756. List<String> formIds = formAuthList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  757. List<String> dataIds = dataAuthList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  758. List<String> flowIds = flowAuthList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  759. List<String> printIds = printAuthList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  760. List<SystemEntity> systemList = filter ? systemApi.getList() : systemApi.getListByIds(systemIds, new ArrayList<>());
  761. List<AuthorizeModel> sysModelList = new ArrayList<>();
  762. Set<String> mainSys = new HashSet<>();
  763. for (SystemEntity s : systemList) {
  764. AuthorizeModel authorizeModel;
  765. if (Objects.equals(s.getIsMain(), 1)) {
  766. authorizeModel = new AuthorizeModel();
  767. authorizeModel.setId(CodeConst.XTCD);
  768. authorizeModel.setFullName("系统菜单");
  769. authorizeModel.setParentId("-1");
  770. authorizeModel.setSortCode(0l);
  771. mainSys.add(s.getId());
  772. } else {
  773. authorizeModel = JsonUtil.getJsonToBean(s, AuthorizeModel.class);
  774. AuthorizeModel web = new AuthorizeModel();
  775. web.setId(authorizeModel.getId() + "2");
  776. web.setFullName("WEB菜单");
  777. web.setParentId(authorizeModel.getId());
  778. web.setSortCode(0l);
  779. AuthorizeModel app = new AuthorizeModel();
  780. app.setId(authorizeModel.getId() + "1");
  781. app.setFullName("APP菜单");
  782. app.setParentId(authorizeModel.getId());
  783. app.setSortCode(0l);
  784. sysModelList.add(web);
  785. sysModelList.add(app);
  786. }
  787. sysModelList.add(authorizeModel);
  788. }
  789. List<ModuleEntity> moduleList = filter ? moduleApi.getList() : moduleApi.getModuleByIds(moduleIds);
  790. List<String> appComModule = new ArrayList<>();
  791. appComModule.addAll(JnpfConst.APP_CONFIG_MODULE);
  792. appComModule.addAll(JnpfConst.ONLINE_DEV_MODULE);
  793. //移除固定菜单
  794. moduleList = moduleList.stream().filter(t -> !appComModule.contains(t.getEnCode())).collect(Collectors.toList());
  795. for (ModuleEntity me : moduleList) {
  796. if (StringUtil.isEmpty(me.getParentId()) || "-1".equals(me.getParentId())) {
  797. if (mainSys.contains(me.getSystemId())) {
  798. me.setParentId(CodeConst.XTCD);
  799. } else {
  800. if (JnpfConst.WEB.equals(me.getCategory())) {
  801. me.setParentId(me.getSystemId() + "2");
  802. } else {
  803. me.setParentId(me.getSystemId() + "1");
  804. }
  805. }
  806. }
  807. }
  808. List<AuthorizeModel> moduleAuth = JsonUtil.getJsonToList(moduleList, AuthorizeModel.class);
  809. //获取菜单
  810. if (isAdmin) {
  811. moduleIds = adminGetAuth(moduleList, null, mainSys);
  812. }
  813. List<UserAuthorizeModel> module = getEachAuth(sysModelList, moduleAuth, moduleIds);
  814. List<AuthorizeModel> sysAndModule = new ArrayList<>();
  815. sysAndModule.addAll(sysModelList);
  816. sysAndModule.addAll(moduleAuth);
  817. //获取按钮
  818. List<ModuleButtonEntity> buttonEntityList = filter ? buttonApi.getList() : buttonApi.getListByIds(buttonIds);
  819. List<AuthorizeModel> buttonAuth = new ArrayList<>();
  820. for (ModuleButtonEntity item : buttonEntityList) {
  821. AuthorizeModel auth = JsonUtil.getJsonToBean(item, AuthorizeModel.class);
  822. auth.setParentId(item.getModuleId());
  823. buttonAuth.add(auth);
  824. }
  825. if (isAdmin) {
  826. List<String> collect = buttonEntityList.stream().map(ModuleButtonEntity::getModuleId).collect(Collectors.toList());
  827. buttonIds = adminGetAuth(moduleList, collect, mainSys);
  828. }
  829. List<UserAuthorizeModel> button = getEachAuth(sysAndModule, buttonAuth, buttonIds);
  830. //获取列表
  831. List<ModuleColumnEntity> columnEntityList = filter ? columnApi.getList() : columnApi.getListByIds(columnIds);
  832. List<AuthorizeModel> columnAuth = new ArrayList<>();
  833. for (ModuleColumnEntity item : columnEntityList) {
  834. AuthorizeModel auth = JsonUtil.getJsonToBean(item, AuthorizeModel.class);
  835. auth.setParentId(item.getModuleId());
  836. columnAuth.add(auth);
  837. }
  838. if (isAdmin) {
  839. List<String> collect = columnEntityList.stream().map(ModuleColumnEntity::getModuleId).collect(Collectors.toList());
  840. columnIds = adminGetAuth(moduleList, collect, mainSys);
  841. }
  842. List<UserAuthorizeModel> column = getEachAuth(sysAndModule, columnAuth, columnIds);
  843. //获取表单
  844. List<ModuleFormEntity> formEntityList = filter ? formApi.getList() : formApi.getListByIds(formIds);
  845. List<AuthorizeModel> formAuth = new ArrayList<>();
  846. for (ModuleFormEntity item : formEntityList) {
  847. AuthorizeModel auth = JsonUtil.getJsonToBean(item, AuthorizeModel.class);
  848. auth.setParentId(item.getModuleId());
  849. formAuth.add(auth);
  850. }
  851. if (isAdmin) {
  852. List<String> collect = formEntityList.stream().map(ModuleFormEntity::getModuleId).collect(Collectors.toList());
  853. formIds = adminGetAuth(moduleList, collect, mainSys);
  854. }
  855. List<UserAuthorizeModel> form = getEachAuth(sysAndModule, formAuth, formIds);
  856. //获取数据权限
  857. List<ModuleDataAuthorizeSchemeEntity> dataEntityList = filter ? schemeApi.getList() : schemeApi.getListByIds(dataIds);
  858. List<AuthorizeModel> dataAuth = new ArrayList<>();
  859. for (ModuleDataAuthorizeSchemeEntity item : dataEntityList) {
  860. AuthorizeModel auth = JsonUtil.getJsonToBean(item, AuthorizeModel.class);
  861. auth.setParentId(item.getModuleId());
  862. dataAuth.add(auth);
  863. }
  864. if (isAdmin) {
  865. List<String> collect = dataEntityList.stream().map(ModuleDataAuthorizeSchemeEntity::getModuleId).collect(Collectors.toList());
  866. dataIds = adminGetAuth(moduleList, collect, mainSys);
  867. }
  868. List<UserAuthorizeModel> dataScheme = getEachAuth(sysAndModule, dataAuth, dataIds);
  869. //全部应用
  870. List<SystemEntity> systemAll = systemApi.getList();
  871. List<AuthorizeModel> systemAuthAll = JsonUtil.getJsonToList(systemAll, AuthorizeModel.class);
  872. //获取流程权限
  873. if (isAdmin) {
  874. List<TemplateTreeListVo> treeList = templateApi.treeListWithPower();
  875. List<String> idAll = new ArrayList<>();
  876. idList(treeList, idAll);
  877. flowIds = idAll;
  878. }
  879. List<TemplateEntity> flowEntityList = templateApi.getListByFlowIds(flowIds);
  880. List<AuthorizeModel> flowAuth = new ArrayList<>();
  881. List<String> flowIdsAdmin = new ArrayList<>();
  882. for (TemplateEntity item : flowEntityList) {
  883. AuthorizeModel auth = JsonUtil.getJsonToBean(item, AuthorizeModel.class);
  884. auth.setParentId(item.getSystemId());
  885. flowAuth.add(auth);
  886. flowIdsAdmin.add(item.getId());
  887. flowIdsAdmin.add(item.getSystemId());
  888. }
  889. if (isAdmin) {
  890. flowIds = flowIdsAdmin;
  891. }
  892. List<UserAuthorizeModel> flow = getEachAuth(systemAuthAll, flowAuth, flowIds);
  893. //获取打印权限
  894. PaginationPrint paginationPrint = new PaginationPrint();
  895. paginationPrint.setDataType(1);
  896. paginationPrint.setVisibleType(2);
  897. List<PrintDevEntity> printEntityList = filter ? printDevApi.getWorkSelector(paginationPrint) : printDevApi.getListByIds(printIds);
  898. List<AuthorizeModel> printAuth = new ArrayList<>();
  899. List<String> printIdsAdmin = new ArrayList<>();
  900. for (PrintDevEntity item : printEntityList) {
  901. AuthorizeModel auth = JsonUtil.getJsonToBean(item, AuthorizeModel.class);
  902. auth.setParentId(item.getSystemId());
  903. printAuth.add(auth);
  904. printIdsAdmin.add(item.getId());
  905. printIdsAdmin.add(item.getSystemId());
  906. }
  907. if (isAdmin) {
  908. printIds = printIdsAdmin;
  909. }
  910. List<UserAuthorizeModel> print = getEachAuth(systemAuthAll, printAuth, printIds);
  911. UserAuthorizeVO vo = UserAuthorizeVO.builder()
  912. .module(module)
  913. .button(button)
  914. .column(column)
  915. .form(form)
  916. .resource(dataScheme)
  917. .flow(flow)
  918. .print(print)
  919. .build();
  920. return vo;
  921. }
  922. private static List<UserAuthorizeModel> getEachAuth(List<AuthorizeModel> sysModelList, List<AuthorizeModel> moduleAuth, List<String> moduleIds) {
  923. List<AuthorizeModel> allAuth = new ArrayList<>();
  924. for (AuthorizeModel authorizeModel : sysModelList) {
  925. if (moduleIds.contains(authorizeModel.getId())) {
  926. allAuth.add(authorizeModel);
  927. }
  928. }
  929. allAuth.addAll(moduleAuth);
  930. List<SumTree<AuthorizeModel>> trees = TreeDotUtils.convertListToTreeDot(allAuth);
  931. List<UserAuthorizeModel> module = JsonUtil.getJsonToList(trees, UserAuthorizeModel.class);
  932. module = module.stream().filter(t -> CollectionUtil.isNotEmpty(t.getChildren())).collect(Collectors.toList());
  933. return module;
  934. }
  935. private void idList(List<TemplateTreeListVo> list, List<String> idAll) {
  936. for (TemplateTreeListVo vo : list) {
  937. idAll.add(vo.getId());
  938. vo.setDisabled(false);
  939. if (vo.getChildren() != null) {
  940. idList(vo.getChildren(), idAll);
  941. }
  942. }
  943. }
  944. private static List<String> adminGetAuth(List<ModuleEntity> moduleList, List<String> moduleIds, Set<String> mainSys) {
  945. Set<String> auth = new HashSet<>();
  946. List<ModuleEntity> moduleListResult;
  947. if (moduleIds != null) {
  948. List<String> finalModuleList = new ArrayList<>();
  949. //递归获取父级菜单
  950. getParent(moduleList, moduleIds, finalModuleList);
  951. //有权限的菜单
  952. finalModuleList.addAll(moduleIds);
  953. moduleListResult = moduleList.stream().filter(t -> finalModuleList.contains(t.getId())).collect(Collectors.toList());
  954. } else {
  955. moduleListResult = moduleList;
  956. }
  957. for (ModuleEntity item : moduleListResult) {
  958. auth.add(item.getId());
  959. if (mainSys.contains(item.getSystemId())) {
  960. auth.add(CodeConst.XTCD);
  961. } else {
  962. auth.add(item.getSystemId());
  963. if (JnpfConst.WEB.equals(item.getCategory())) {
  964. auth.add(item.getSystemId() + "2");
  965. } else {
  966. auth.add(item.getSystemId() + "1");
  967. }
  968. }
  969. }
  970. return new ArrayList<>(auth);
  971. }
  972. /**
  973. * 递归获取父级菜单
  974. *
  975. * @param list
  976. * @param child
  977. * @param parent
  978. */
  979. private static void getParent(List<ModuleEntity> list, List<String> child, List<String> parent) {
  980. List<ModuleEntity> childList = list.stream().filter(t -> child.contains(t.getId())).collect(Collectors.toList());
  981. List<String> parenIds = childList.stream().map(ModuleEntity::getParentId).collect(Collectors.toList());
  982. List<ModuleEntity> parentList = list.stream().filter(t -> parenIds.contains(t.getId())).collect(Collectors.toList());
  983. List<String> newParent = parentList.stream().map(ModuleEntity::getId).collect(Collectors.toList());
  984. if (CollectionUtil.isNotEmpty(parentList)) {
  985. parent.addAll(newParent);
  986. getParent(list, newParent, parent);
  987. }
  988. }
  989. }