AuthorizeServiceImpl.java 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. package jnpf.permission.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.text.StrPool;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.baomidou.dynamic.datasource.annotation.DS;
  6. import com.baomidou.dynamic.datasource.annotation.DSTransactional;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.google.common.collect.Lists;
  9. import jnpf.base.UserInfo;
  10. import jnpf.base.entity.ModuleEntity;
  11. import jnpf.base.entity.SystemEntity;
  12. import jnpf.base.model.base.SystemBaeModel;
  13. import jnpf.base.model.button.ButtonModel;
  14. import jnpf.base.model.column.ColumnModel;
  15. import jnpf.base.model.form.ModuleFormModel;
  16. import jnpf.base.model.module.ModuleModel;
  17. import jnpf.base.model.portalManage.PortalManagePageDO;
  18. import jnpf.base.model.portalManage.PortalModel;
  19. import jnpf.base.model.portalManage.SavePortalAuthModel;
  20. import jnpf.base.model.resource.ResourceModel;
  21. import jnpf.base.service.*;
  22. import jnpf.config.ConfigValueUtil;
  23. import jnpf.constant.*;
  24. import jnpf.consts.DeviceType;
  25. import jnpf.database.model.dto.PrepSqlDTO;
  26. import jnpf.database.model.superQuery.SuperJsonModel;
  27. import jnpf.database.model.superQuery.SuperQueryJsonModel;
  28. import jnpf.database.sql.util.SqlFrameFastUtil;
  29. import jnpf.database.util.DataSourceUtil;
  30. import jnpf.database.util.DbTypeUtil;
  31. import jnpf.database.util.JdbcUtil;
  32. import jnpf.database.util.TenantDataSourceUtil;
  33. import jnpf.exception.DataException;
  34. import jnpf.exception.NoPermiLoginException;
  35. import jnpf.model.BaseSystemInfo;
  36. import jnpf.model.login.UserSystemVO;
  37. import jnpf.model.tenant.TenantAuthorizeModel;
  38. import jnpf.model.visualJson.FieLdsModel;
  39. import jnpf.model.visualJson.config.ConfigModel;
  40. import jnpf.permission.entity.*;
  41. import jnpf.permission.mapper.AuthorizeMapper;
  42. import jnpf.permission.model.authCondition.AuthConditionModel;
  43. import jnpf.permission.model.authCondition.AuthGroup;
  44. import jnpf.permission.model.authCondition.AuthItem;
  45. import jnpf.permission.model.authorize.*;
  46. import jnpf.permission.model.position.PosConModel;
  47. import jnpf.permission.service.*;
  48. import jnpf.permission.util.AuthPermUtil;
  49. import jnpf.util.*;
  50. import jnpf.util.context.RequestContext;
  51. import lombok.extern.slf4j.Slf4j;
  52. import org.apache.commons.collections4.CollectionUtils;
  53. import org.springframework.beans.factory.annotation.Autowired;
  54. import org.springframework.stereotype.Service;
  55. import java.net.URLDecoder;
  56. import java.util.*;
  57. import java.util.function.Function;
  58. import java.util.stream.Collectors;
  59. /**
  60. * 操作权限
  61. *
  62. * @author JNPF开发平台组
  63. * @version V3.1.0
  64. * @copyright 引迈信息技术有限公司
  65. * @date 2019年9月26日 上午9:18
  66. */
  67. @Slf4j
  68. @Service
  69. public class AuthorizeServiceImpl extends SuperServiceImpl<AuthorizeMapper, AuthorizeEntity> implements AuthorizeService {
  70. @Autowired
  71. private UserRelationService userRelationService;
  72. @Autowired
  73. private DataSourceUtil dataSourceUtils;
  74. @Autowired
  75. private CacheKeyUtil cacheKeyUtil;
  76. @Autowired
  77. private RedisUtil redisUtil;
  78. @Autowired
  79. private UserService userService;
  80. @Autowired
  81. private OrganizeAdministratorService organizeAdminIsTratorService;
  82. @Autowired
  83. private SystemService systemApi;
  84. @Autowired
  85. private ConfigValueUtil configValueUtil;
  86. @Autowired
  87. private ModuleService moduleApi;
  88. @Autowired
  89. private PortalManageService portalManageApi;
  90. @Autowired
  91. private StandingService standingService;
  92. @Autowired
  93. private OrganizeService organizeService;
  94. @Autowired
  95. private PositionService positionService;
  96. @Autowired
  97. private RoleRelationService roleRelationService;
  98. @Autowired
  99. private RoleService roleService;
  100. @Autowired
  101. private SysconfigService sysconfigApi;
  102. //当前系统权限
  103. @Override
  104. public AuthorizeVO getAuthorize(boolean singletonOrg, String currentSystemCode, Integer isBackend) {
  105. return this.getAuthorize(singletonOrg, currentSystemCode, isBackend, false);
  106. }
  107. //全部系统权限
  108. @Override
  109. public AuthorizeVO getAuthorizeByUser(boolean singletonOrg) {
  110. //获取全部应用权限
  111. return this.getAuthorize(singletonOrg, null, 0, true);
  112. }
  113. @Override
  114. public AuthorizeVO getAuthorize(boolean singletonOrg, String currentSystemCode, Integer isBackend, Boolean allSystem) {
  115. boolean isPc = RequestContext.isOrignPc();
  116. String pcCode = isPc ? JnpfConst.WEB : JnpfConst.APP;
  117. BaseSystemInfo baseSystemInfo = sysconfigApi.getSysInfo();
  118. UserInfo userInfo = UserProvider.getUser();
  119. OtherModel otherModel = new OtherModel();
  120. List<ModuleModel> moduleList = new ArrayList<>();
  121. List<ButtonModel> buttonList = new ArrayList<>();
  122. List<ColumnModel> columnList = new ArrayList<>();
  123. List<ResourceModel> resourceList = new ArrayList<>();
  124. List<ModuleFormModel> formsList = new ArrayList<>();
  125. List<SystemBaeModel> systemList = new ArrayList<>();
  126. List<String> flowList = new ArrayList<>();
  127. List<UserSystemVO> standingListVo = new ArrayList<>();
  128. boolean isAdmin = userInfo.getIsAdministrator();
  129. String currentSystemId = "";
  130. SystemEntity info = null;
  131. if (StringUtil.isNotEmpty(currentSystemCode)) {
  132. try {
  133. currentSystemCode = URLDecoder.decode(currentSystemCode, "UTF-8");
  134. } catch (Exception e) {
  135. }
  136. info = systemApi.getInfoByEnCode(currentSystemCode);
  137. if (info == null) {
  138. throw new NoPermiLoginException(MsgCode.PS032.get());
  139. }
  140. currentSystemId = info.getId();
  141. }
  142. List<String> moduleAuthorize = new ArrayList<>();
  143. List<String> moduleUrlAddressAuthorize = new ArrayList<>();
  144. if (configValueUtil.isMultiTenancy()) {
  145. TenantAuthorizeModel tenantAuthorizeModel = TenantDataSourceUtil.getCacheModuleAuthorize(userInfo.getTenantId());
  146. moduleAuthorize = tenantAuthorizeModel.getModuleIdList();
  147. moduleUrlAddressAuthorize = tenantAuthorizeModel.getUrlAddressList();
  148. }
  149. if (!isAdmin) {
  150. //获取用户所有权限
  151. List<String> objectIds = new ArrayList<>();
  152. List<String> positionIds = userInfo.getPositionIds();
  153. List<String> roleIds = roleService.getListByIds(userInfo.getRoleIds()).stream()
  154. .filter(t -> !PermissionConst.ORGANIZE.equals(t.getType())).map(RoleEntity::getId).collect(Collectors.toList());
  155. objectIds.addAll(positionIds);
  156. objectIds.addAll(roleIds);
  157. List<AuthorizeEntity> authorizeList = this.getListByObjectId(objectIds);
  158. List<AuthorizeEntity> standingList = authorizeList.stream().filter(t -> PermissionConst.STAND.equals(t.getItemType())).collect(Collectors.toList());
  159. List<String> posAndRoles = new ArrayList<>();
  160. //非管理员,走身份(standingSwitch)
  161. if (CollectionUtil.isNotEmpty(standingList) && configValueUtil.isStandingSwitch()) {
  162. //获取身份id列表
  163. List<String> standingIds = standingList.stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  164. //设置当前身份
  165. String standingId = setCurrentStanding(standingIds, standingListVo);
  166. if (StringUtil.isNotEmpty(standingId)) {
  167. //根据当前身份获取角色和岗位
  168. List<String> stdPos = standingList.stream().filter(t -> t.getItemId().equals(standingId)
  169. && PermissionConst.POSITION.equals(t.getObjectType())).map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  170. //获取当前岗位角色
  171. List<String> stdPosRole = roleRelationService.getListByObjectId(stdPos, null)
  172. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  173. List<String> stdRole = standingList.stream().filter(t -> t.getItemId().equals(standingId)
  174. && PermissionConst.ROLE.equals(t.getObjectType())).map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  175. posAndRoles.addAll(stdPos);
  176. posAndRoles.addAll(stdPosRole);
  177. posAndRoles.addAll(stdRole);
  178. //重新设置当前用户角色
  179. List<RoleEntity> roleList = roleService.getListByIds(stdRole);
  180. for (RoleEntity roleEntity : roleList) {
  181. if (PermissionConst.MANAGER_CODE.equals(roleEntity.getEnCode())) {
  182. otherModel.setIsManageRole(true);
  183. } else if (PermissionConst.DEVELOPER_CODE.equals(roleEntity.getEnCode())) {
  184. otherModel.setIsDevRole(true);
  185. } else if (PermissionConst.USER_CODE.equals(roleEntity.getEnCode())) {
  186. otherModel.setIsUserRole(true);
  187. } else {
  188. otherModel.setIsOtherRole(true);
  189. }
  190. }
  191. //通过身份过滤掉多于的权限
  192. authorizeList = authorizeList.stream().filter(t -> posAndRoles.contains(t.getObjectId())).collect(Collectors.toList());
  193. }
  194. }
  195. //应用
  196. List<String> systemId = authorizeList.stream().filter(t -> AuthorizeConst.SYSTEM.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  197. if (systemId.size() > 0) {
  198. List<SystemEntity> systemAdmin = systemApi.getListByIds(systemId, moduleAuthorize);
  199. //配置了流程和协作的菜单但是没有主系统菜单时,直接添加主系统权限
  200. List<SystemEntity> mainList = systemAdmin.stream().filter(t -> JnpfConst.MAIN_SYSTEM_CODE.equals(t.getEnCode())).collect(Collectors.toList());
  201. if (CollectionUtil.isEmpty(mainList)) {
  202. List<SystemEntity> collect = systemAdmin.stream().filter(t -> JnpfConst.WORK_FLOW_CODE.equals(t.getEnCode()) || JnpfConst.TEAMWORK_CODE.equals(t.getEnCode())).collect(Collectors.toList());
  203. if (CollectionUtil.isNotEmpty(collect)) {
  204. systemAdmin.add(systemApi.getInfoByEnCode(JnpfConst.MAIN_SYSTEM_CODE));
  205. }
  206. }
  207. //app无主应用时获取当前第一个应用
  208. if (!isPc && StringUtil.isEmpty(currentSystemCode)) {
  209. info = systemAdmin.stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).findFirst().orElse(null);
  210. if (info != null) {
  211. currentSystemId = info.getId();
  212. currentSystemCode = info.getEnCode();
  213. }
  214. }
  215. systemList = JsonUtil.getJsonToList(systemAdmin, SystemBaeModel.class);
  216. }
  217. List<String> moduleId = authorizeList.stream().filter(t -> AuthorizeConst.MODULE.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  218. if (moduleId.size() > 0) {
  219. List<ModuleModel> allSysMenu = this.baseMapper.findModule(moduleId, null, moduleAuthorize, moduleUrlAddressAuthorize, singletonOrg ? 0 : 1);
  220. List<String> currSys = new ArrayList<>();
  221. if (StringUtil.isNotEmpty(currentSystemId) && !allSystem) {
  222. currSys.add(currentSystemId);
  223. }
  224. moduleList = CollectionUtils.isNotEmpty(currSys) ? allSysMenu.stream().filter(t -> currSys.contains(t.getSystemId())).collect(Collectors.toList()) : allSysMenu;
  225. //当前系统是否有流程菜单
  226. if (StringUtil.isNotEmpty(currentSystemCode)) {
  227. if (JnpfConst.MAIN_SYSTEM_CODE.equals(currentSystemCode)) {
  228. otherModel.setWorkflowEnabled(allSysMenu.stream().anyMatch(t -> JnpfConst.MODULE_CODE.contains(t.getEnCode()) && pcCode.equals(t.getCategory())) ? 1 : 0);
  229. } else {
  230. otherModel.setWorkflowEnabled(moduleList.stream().anyMatch(t -> JnpfConst.MODULE_CODE.contains(t.getEnCode()) && pcCode.equals(t.getCategory())) ? 1 : 0);
  231. }
  232. }
  233. }
  234. // 按钮
  235. List<String> buttonId = authorizeList.stream().filter(t -> AuthorizeConst.BUTTON.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  236. if (buttonId.size() > 0) {
  237. buttonList = this.baseMapper.findButton(buttonId);
  238. }
  239. // 列表
  240. List<String> columnId = authorizeList.stream().filter(t -> AuthorizeConst.COLUMN.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  241. if (columnId.size() > 0) {
  242. columnList = this.baseMapper.findColumn(columnId);
  243. }
  244. // 数据
  245. List<String> resourceId = authorizeList.stream().filter(t -> AuthorizeConst.RESOURCE.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  246. if (resourceId.size() > 0) {
  247. resourceList = this.baseMapper.findResource(resourceId);
  248. if (CollectionUtil.isNotEmpty(posAndRoles)) {
  249. resourceList = resourceList.stream().filter(t -> posAndRoles.contains(t.getObjectId())).collect(Collectors.toList());
  250. }
  251. }
  252. // 表单
  253. List<String> formId = authorizeList.stream().filter(t -> AuthorizeConst.FROM.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  254. if (formId.size() > 0) {
  255. formsList = this.baseMapper.findForms(formId);
  256. }
  257. // 流程
  258. flowList = authorizeList.stream().filter(t -> AuthorizeConst.FLOW.equals(t.getItemType())).map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  259. } else {
  260. buttonList = this.baseMapper.findButtonAdmin(1);
  261. columnList = this.baseMapper.findColumnAdmin(1);
  262. resourceList = this.baseMapper.findResourceAdmin(1);
  263. formsList = this.baseMapper.findFormsAdmin(1);
  264. List<SystemEntity> systemAdmin = systemApi.findSystemAdmin(moduleAuthorize);
  265. if (!isPc && StringUtil.isEmpty(currentSystemCode)) {
  266. info = systemAdmin.stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).findFirst().orElse(null);
  267. if (info != null) {
  268. currentSystemId = info.getId();
  269. currentSystemCode = info.getEnCode();
  270. }
  271. }
  272. systemList = JsonUtil.getJsonToList(systemAdmin, SystemBaeModel.class);
  273. List<ModuleEntity> moduleAdmin = moduleApi.findModuleAdmin(singletonOrg ? 0 : 1, null, moduleAuthorize, moduleUrlAddressAuthorize);
  274. String thisid = currentSystemId;
  275. List<ModuleModel> allSysMenu = JsonUtil.getJsonToList(moduleAdmin, ModuleModel.class);
  276. if (StringUtil.isNotEmpty(currentSystemId) && !allSystem) {
  277. moduleList = allSysMenu.stream().filter(t -> Objects.equals(t.getSystemId(), thisid)).collect(Collectors.toList());
  278. } else {
  279. moduleList = allSysMenu;
  280. }
  281. //当前系统是否有流程菜单
  282. if (StringUtil.isNotEmpty(currentSystemCode)) {
  283. if (JnpfConst.MAIN_SYSTEM_CODE.equals(currentSystemCode)) {
  284. otherModel.setWorkflowEnabled(allSysMenu.stream().anyMatch(t -> JnpfConst.MODULE_CODE.contains(t.getEnCode()) && pcCode.equals(t.getCategory())) ? 1 : 0);
  285. } else {
  286. otherModel.setWorkflowEnabled(moduleList.stream().anyMatch(t -> JnpfConst.MODULE_CODE.contains(t.getEnCode()) && pcCode.equals(t.getCategory())) ? 1 : 0);
  287. }
  288. }
  289. //超管添加身份
  290. if (configValueUtil.isStandingSwitch()) {
  291. UserSystemVO admin = new UserSystemVO();
  292. admin.setId(1 + "");
  293. admin.setName(MsgCode.OA025.get());
  294. admin.setCurrentStanding(true);
  295. admin.setIcon(PermissionConst.SD_ADMIN_ICON);
  296. standingListVo.add(admin);
  297. }
  298. }
  299. //添加固定菜单
  300. List<String> appComModule = new ArrayList<>();
  301. appComModule.addAll(JnpfConst.APP_CONFIG_MODULE);
  302. appComModule.addAll(JnpfConst.ONLINE_DEV_MODULE);
  303. if (JnpfConst.MAIN_SYSTEM_CODE.equals(currentSystemCode)) {
  304. moduleList = moduleList.stream().filter(t -> !appComModule.contains(t.getEnCode())).collect(Collectors.toList());
  305. } else if (Objects.equals(isBackend, 1)) {
  306. //是后台管理
  307. List<ModuleEntity> listByEnCode = moduleApi.getListByEnCode(appComModule);
  308. moduleList = JsonUtil.getJsonToList(listByEnCode, ModuleModel.class);
  309. }
  310. //系统配置-流程开关
  311. moduleList = moduleList.stream().filter(t -> {
  312. if (!Objects.equals(baseSystemInfo.getFlowSign(), 1) && JnpfConst.WORK_FLOWSIGN.equals(t.getEnCode())) {
  313. return false;
  314. } else if (!Objects.equals(baseSystemInfo.getFlowTodo(), 1) && JnpfConst.WORK_FLOWTODO.equals(t.getEnCode())) {
  315. return false;
  316. }
  317. return true;
  318. }).collect(Collectors.toList());
  319. //应用前台不需要添加菜单数据
  320. return new AuthorizeVO(moduleList, buttonList, columnList, resourceList, formsList, systemList, standingListVo, info, flowList, otherModel);
  321. }
  322. /**
  323. * 设置当前身份
  324. *
  325. * @param standingIds
  326. * @param standingListVo
  327. */
  328. private String setCurrentStanding(List<String> standingIds, List<UserSystemVO> standingListVo) {
  329. List<StandingEntity> listByIds = standingService.getListByIds(standingIds);
  330. UserEntity info = userService.getInfo(UserProvider.getUser().getUserId());
  331. String currentStanding = "";
  332. if (RequestContext.isOrignPc()) {
  333. currentStanding = info.getStanding();
  334. } else {
  335. currentStanding = info.getAppStanding();
  336. }
  337. for (StandingEntity standing : listByIds) {
  338. UserSystemVO standingVo = JsonUtil.getJsonToBean(standing, UserSystemVO.class);
  339. standingVo.setName(standing.getFullName());
  340. if (StringUtil.isNotEmpty(currentStanding) && currentStanding.equals(standing.getId())) {
  341. standingVo.setCurrentStanding(true);
  342. }
  343. String icon = "";
  344. switch (standing.getEnCode()) {
  345. case PermissionConst.MANAGER_CODE:
  346. icon = PermissionConst.SD_MANAGER_ICON;
  347. break;
  348. case PermissionConst.DEVELOPER_CODE:
  349. icon = PermissionConst.SD_DEVELOPER_ICON;
  350. break;
  351. case PermissionConst.USER_CODE:
  352. icon = PermissionConst.SD_USER_ICON;
  353. break;
  354. default:
  355. icon = PermissionConst.SD_EXPERIENCER_ICON;
  356. break;
  357. }
  358. standingVo.setIcon(icon);
  359. standingListVo.add(standingVo);
  360. }
  361. if (CollectionUtil.isEmpty(standingListVo)) {
  362. return null;
  363. }
  364. UserSystemVO currStand = standingListVo.stream().filter(t -> t.isCurrentStanding()).findFirst().orElse(null);
  365. if (currStand == null) {
  366. UserSystemVO userSystemVO = standingListVo.stream().filter(t -> PermissionConst.USER_CODE.equals(t.getEnCode()))
  367. .findFirst().orElse(standingListVo.get(0));
  368. userSystemVO.setCurrentStanding(true);
  369. if (RequestContext.isOrignPc()) {
  370. info.setStanding(userSystemVO.getId());
  371. } else {
  372. info.setAppStanding(userSystemVO.getId());
  373. }
  374. userService.updateById(info);
  375. currentStanding = userSystemVO.getId();
  376. } else {
  377. currentStanding = currStand.getId();
  378. }
  379. return currentStanding;
  380. }
  381. @Override
  382. public void getPortal(List<SystemEntity> systemList, List<PortalModel> portalList, Long dateTime, List<String> collect) {
  383. Map<String, SystemEntity> systemBaeModelMap = systemList.stream().collect(Collectors.toMap(SystemEntity::getId, Function.identity()));
  384. List<String> systemIds = systemList.stream().map(SystemEntity::getId).collect(Collectors.toList());
  385. List<PortalManagePageDO> portalManagePageDOS = portalManageApi.selectPortalBySystemIds(systemIds, collect);
  386. if (portalManagePageDOS.size() == 0) {
  387. return;
  388. }
  389. Map<String, List<PortalManagePageDO>> systemIdAndPortalMap = portalManagePageDOS.stream().collect(Collectors.groupingBy(PortalManagePageDO::getSystemId));
  390. if (systemIdAndPortalMap != null) {
  391. systemIdAndPortalMap.keySet().forEach(t -> {
  392. if (Optional.ofNullable(systemBaeModelMap.get(t)).isPresent()) {
  393. PortalModel systemModel = JsonUtil.getJsonToBean(systemBaeModelMap.get(t), PortalModel.class);
  394. systemModel.setParentId("-1");
  395. portalList.add(systemModel);
  396. Map<String, String> platFormId = new HashMap<>();
  397. List<PortalManagePageDO> portalManagePageDOList = systemIdAndPortalMap.get(t);
  398. Map<String, List<PortalManagePageDO>> platFormMap = portalManagePageDOList.stream().collect(Collectors.groupingBy(PortalManagePageDO::getPlatform));
  399. List<PortalManagePageDO> web = platFormMap.get(JnpfConst.WEB);
  400. List<PortalManagePageDO> app = platFormMap.get(JnpfConst.APP);
  401. if (web != null && web.size() > 0) {
  402. PortalModel platForm = new PortalModel();
  403. platForm.setId(systemModel.getId() + "1");
  404. platForm.setParentId(systemModel.getId());
  405. platForm.setFullName("WEB门户");
  406. platForm.setIcon(PermissionConst.PC_ICON);
  407. platForm.setSortCode(0L);
  408. platForm.setCreatorTime(dateTime);
  409. platFormId.put(JnpfConst.WEB, platForm.getId());
  410. portalList.add(platForm);
  411. }
  412. if (app != null && app.size() > 0) {
  413. PortalModel platForm = new PortalModel();
  414. platForm.setId(systemModel.getId() + "2");
  415. platForm.setParentId(systemModel.getId());
  416. platForm.setFullName("APP门户");
  417. platForm.setIcon(PermissionConst.APP_ICON);
  418. platForm.setSortCode(0L);
  419. platForm.setCreatorTime(dateTime);
  420. platFormId.put(JnpfConst.APP, platForm.getId());
  421. portalList.add(platForm);
  422. }
  423. portalManagePageDOList.forEach(pageDO -> {
  424. // if (!categoryList.contains(pageDO.getCategoryId())) {
  425. // categoryList.add(pageDO.getCategoryId());
  426. // PortalModel categoryModel = new PortalModel();
  427. // categoryModel.setId(pageDO.getCategoryId());
  428. // categoryModel.setParentId(platFormId.get(pageDO.getPlatform()));
  429. // categoryModel.setFullName(pageDO.getCategoryName());
  430. // categoryModel.setOnlyId(RandomUtil.uuId());
  431. // list.add(categoryModel);
  432. // }
  433. PortalModel model = JsonUtil.getJsonToBean(pageDO, PortalModel.class);
  434. // model.setParentId(pageDO.getCategoryId());
  435. model.setParentId(platFormId.get(pageDO.getPlatform()));
  436. model.setFullName(pageDO.getPortalName());
  437. portalList.add(model);
  438. });
  439. }
  440. });
  441. }
  442. }
  443. @Override
  444. @DSTransactional
  445. public void saveItemAuth(SavePortalAuthModel portalAuthModel) {
  446. List<String> ids = portalAuthModel.getIds();
  447. String id = portalAuthModel.getId();
  448. String type = portalAuthModel.getType();
  449. String userId = UserProvider.getLoginUserId();
  450. // 原始授权角色
  451. List<AuthorizeEntity> list = new ArrayList<>();
  452. for (int i = 0; i < ids.size(); i++) {
  453. AuthorizeEntity authorizeEntity = new AuthorizeEntity();
  454. authorizeEntity.setId(RandomUtil.uuId());
  455. authorizeEntity.setItemType(type);
  456. authorizeEntity.setItemId(ids.get(i));
  457. authorizeEntity.setObjectType(portalAuthModel.getObjectType());
  458. authorizeEntity.setObjectId(id);
  459. authorizeEntity.setSortCode((long) i);
  460. authorizeEntity.setCreatorTime(new Date());
  461. authorizeEntity.setCreatorUserId(userId);
  462. list.add(authorizeEntity);
  463. }
  464. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  465. queryWrapper.lambda().eq(AuthorizeEntity::getItemType, type);
  466. queryWrapper.lambda().eq(AuthorizeEntity::getObjectId, id);
  467. this.remove(queryWrapper);
  468. list.forEach(this::save);
  469. List<String> userIds = new ArrayList<>();
  470. String objectType = portalAuthModel.getObjectType();
  471. if (PermissionConst.ORGANIZE.equals(objectType) || PermissionConst.POSITION.equals(objectType)) {
  472. userIds.addAll(userRelationService.getListByObjectId(id).stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
  473. }
  474. if (PermissionConst.ROLE.equals(objectType)) {
  475. userIds.addAll(roleRelationService.getListByRoleId(id, PermissionConst.USER).stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toList()));
  476. }
  477. userService.delCurUser(MsgCode.PS010.get(), userIds);
  478. }
  479. @Override
  480. @DSTransactional
  481. public void saveObjectAuth(SavePortalAuthModel portalAuthModel) {
  482. List<String> ids = portalAuthModel.getIds();
  483. String id = portalAuthModel.getId();
  484. String type = portalAuthModel.getType();
  485. String userId = UserProvider.getLoginUserId();
  486. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  487. queryWrapper.lambda().eq(AuthorizeEntity::getItemType, type);
  488. queryWrapper.lambda().eq(AuthorizeEntity::getItemId, id);
  489. this.remove(queryWrapper);
  490. List<AuthorizeEntity> portalSystem = new ArrayList<>();
  491. boolean isPortal = AuthorizeConst.AUTHORIZE_PORTAL_MANAGE.equals(type);
  492. if (isPortal && !ids.isEmpty() && StringUtil.isNotEmpty(portalAuthModel.getSystemId())) {
  493. QueryWrapper<AuthorizeEntity> wrapper = new QueryWrapper<>();
  494. wrapper.lambda().eq(AuthorizeEntity::getItemType, AuthorizeConst.SYSTEM);
  495. wrapper.lambda().eq(AuthorizeEntity::getItemId, portalAuthModel.getSystemId());
  496. wrapper.lambda().in(AuthorizeEntity::getObjectId, ids);
  497. portalSystem.addAll(this.list(wrapper));
  498. }
  499. // 原始授权角色
  500. List<AuthorizeEntity> list = new ArrayList<>();
  501. for (int i = 0; i < ids.size(); i++) {
  502. String objectId = ids.get(i);
  503. AuthorizeEntity authorizeEntity = new AuthorizeEntity();
  504. authorizeEntity.setId(RandomUtil.uuId());
  505. authorizeEntity.setItemType(type);
  506. authorizeEntity.setObjectId(objectId);
  507. authorizeEntity.setObjectType(PermissionConst.ROLE);
  508. authorizeEntity.setItemId(id);
  509. authorizeEntity.setSortCode((long) i);
  510. authorizeEntity.setCreatorTime(new Date());
  511. authorizeEntity.setCreatorUserId(userId);
  512. list.add(authorizeEntity);
  513. if (isPortal && StringUtil.isNotEmpty(portalAuthModel.getSystemId())) {
  514. boolean portalCount = portalSystem.stream().filter(t -> Objects.equals(t.getObjectId(), objectId)).count() == 0;
  515. if (portalCount) {
  516. AuthorizeEntity systemAuthorize = new AuthorizeEntity();
  517. systemAuthorize.setId(RandomUtil.uuId());
  518. systemAuthorize.setItemType(AuthorizeConst.SYSTEM);
  519. systemAuthorize.setObjectId(ids.get(i));
  520. systemAuthorize.setObjectType(PermissionConst.ROLE);
  521. systemAuthorize.setItemId(portalAuthModel.getSystemId());
  522. systemAuthorize.setSortCode(0l);
  523. systemAuthorize.setCreatorTime(new Date());
  524. systemAuthorize.setCreatorUserId(userId);
  525. list.add(systemAuthorize);
  526. }
  527. }
  528. }
  529. list.forEach(this::save);
  530. }
  531. @Override
  532. public List<AuthorizeEntity> getAuthorizeByItem(String itemType, String itemId) {
  533. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  534. queryWrapper.lambda().eq(AuthorizeEntity::getItemType, itemType);
  535. queryWrapper.lambda().eq(AuthorizeEntity::getItemId, itemId);
  536. return this.list(queryWrapper);
  537. }
  538. @Override
  539. public List<AuthorizeEntity> getListByRoleIdsAndItemType(List<String> roleIds, String itemType) {
  540. if (roleIds.size() == 0) {
  541. return Collections.EMPTY_LIST;
  542. }
  543. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  544. queryWrapper.lambda().eq(AuthorizeEntity::getItemType, itemType);
  545. queryWrapper.lambda().in(AuthorizeEntity::getObjectId, roleIds);
  546. return this.list(queryWrapper);
  547. }
  548. @Override
  549. public String save(AuthorizeDataUpForm form) {
  550. String errStr = "";
  551. try {
  552. UserInfo userInfo = UserProvider.getUser();
  553. String objectType = form.getObjectType();
  554. String objectId = form.getObjectId();
  555. List<AuthorizeEntity> objectList = new ArrayList<>();
  556. List<AuthorizeEntity> authorizeList = new ArrayList<>();
  557. PosConModel posConModel = null;
  558. // 设置权限归属对象
  559. if (PermissionConst.ORGANIZE.equals(objectType)) {
  560. setEntity(new String[]{objectId}, PermissionConst.ORGANIZE, objectList, true);
  561. }
  562. if (PermissionConst.POSITION.equals(objectType)) {
  563. setEntity(new String[]{objectId}, PermissionConst.POSITION, objectList, true);
  564. PositionEntity info = positionService.getInfo(objectId);
  565. if (Objects.equals(info.getIsCondition(), 1)) {
  566. posConModel = JsonUtil.getJsonToBean(info.getConditionJson(), PosConModel.class);
  567. posConModel.init();
  568. }
  569. }
  570. if (PermissionConst.ROLE.equals(objectType)) {
  571. setEntity(new String[]{objectId}, PermissionConst.ROLE, objectList, true);
  572. RoleEntity info = roleService.getInfo(objectId);
  573. if (Objects.equals(info.getIsCondition(), 1)) {
  574. posConModel = JsonUtil.getJsonToBean(info.getConditionJson(), PosConModel.class);
  575. posConModel.init();
  576. }
  577. }
  578. List<SystemEntity> sysList = systemApi.getList();
  579. List<String> mainSysIds = sysList.stream().filter(t -> Objects.equals(t.getIsMain(), 1)).map(SystemEntity::getId).collect(Collectors.toList());
  580. if (form.getModule() != null) {
  581. List<String> menuList = Arrays.asList(form.getModule());
  582. List<ModuleEntity> menuEntityList = moduleApi.getModuleByIds(menuList);
  583. Set<String> array = new HashSet<>(menuList);
  584. //超出权限基数的截取
  585. if (posConModel != null && posConModel.getNumFlag()) {
  586. //移除非菜单数据,用于基数计算
  587. List<String> menuIds = menuEntityList.stream().map(ModuleEntity::getId).collect(Collectors.toList());
  588. array = menuList.stream().filter(menuIds::contains).collect(Collectors.toSet());
  589. List<String> listByObjectId = this.getListByObjectId(objectId, PermissionConst.MODULE).stream().map(AuthorizeEntity::getItemId).collect(Collectors.toList());
  590. listByObjectId.removeAll(mainSysIds);
  591. if (listByObjectId.size() > 0 && menuList.size() == listByObjectId.size() && menuList.containsAll(listByObjectId)) {
  592. return "";
  593. }
  594. //修改的时候权限包含原来的全部权限-并且原权限已达基数。
  595. if (listByObjectId.size() > 0 && menuList.size() > posConModel.getPermissionNum() && menuList.containsAll(listByObjectId)) {
  596. throw new DataException(MsgCode.SYS144.get());
  597. }
  598. //权限基数的时候只算实际菜单
  599. List<String> collect = menuEntityList.stream().filter(t -> !Objects.equals(t.getType(), 1)).map(ModuleEntity::getId).collect(Collectors.toList());
  600. List<String> collect1 = menuList.stream().filter(t -> collect.contains(t)).collect(Collectors.toList());
  601. int num = collect1.size() - posConModel.getPermissionNum();
  602. if (num > 0) {
  603. errStr = MsgCode.SYS145.get();
  604. List<String> newIds = collect1.subList(0, posConModel.getPermissionNum());
  605. newIds.addAll(getParentMenu(menuEntityList, new HashSet<>(newIds)));
  606. array = new HashSet<>(newIds);
  607. }
  608. }
  609. Set<String> systemIds = new HashSet<>();
  610. Set<String> moduleIds = new HashSet<>(array);
  611. for (ModuleEntity item : menuEntityList) {
  612. if (array.contains(item.getId())) {
  613. moduleIds.add(item.getSystemId());
  614. systemIds.add(item.getSystemId());
  615. if (mainSysIds.contains(item.getSystemId())) {
  616. moduleIds.add(CodeConst.XTCD);
  617. } else {
  618. moduleIds.add(CodeConst.YYCD);
  619. if (JnpfConst.WEB.equals(item.getCategory())) {
  620. moduleIds.add(item.getSystemId() + "2");
  621. }
  622. if (JnpfConst.APP.equals(item.getCategory())) {
  623. moduleIds.add(item.getSystemId() + "1");
  624. }
  625. }
  626. }
  627. }
  628. form.setModule(moduleIds.toArray(new String[0]));
  629. form.setSystemIds(systemIds.toArray(new String[0]));
  630. setEntity(form.getSystemIds(), AuthorizeConst.SYSTEM, authorizeList, false);
  631. }
  632. // 设置权限模块
  633. setEntity(form.getButton(), AuthorizeConst.BUTTON, authorizeList, false);
  634. setEntity(form.getModule(), AuthorizeConst.MODULE, authorizeList, false);
  635. setEntity(form.getColumn(), AuthorizeConst.COLUMN, authorizeList, false);
  636. setEntity(form.getResource(), AuthorizeConst.RESOURCE, authorizeList, false);
  637. setEntity(form.getForm(), AuthorizeConst.FROM, authorizeList, false);
  638. //todo 删除角色相关信息 移除下级权限
  639. List<OrganizeEntity> allOrgList = organizeService.getList(true);
  640. List<PositionEntity> allPosList = positionService.getList(true);
  641. List<String> objectIdAll = objectList.stream().map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  642. //移除关联子数据
  643. deleteAllAuth(form, allOrgList, allPosList);
  644. //移除权限缓存
  645. this.removeAuthByUserOrMenu(null, Arrays.asList(form.getModule()));
  646. //移除权限
  647. String ids = String.join(",", objectIdAll);
  648. JdbcUtil.creUpDe(new PrepSqlDTO(XSSEscape.escapeEmpty(SqlFrameFastUtil.AUTHOR_DEL.replace("{authorizeIds}", ids))).withConn(dataSourceUtils, null));
  649. //权限变更提示
  650. List<String> userIds = new ArrayList<>();
  651. List<String> listIds = new ArrayList<>();
  652. if (PermissionConst.ORGANIZE.equals(objectType)) {
  653. List<String> orgIds = allOrgList.stream().filter(t -> t.getOrganizeIdTree().contains(objectId))
  654. .map(OrganizeEntity::getId).collect(Collectors.toList());
  655. List<String> posIds = allPosList.stream().filter(t -> listIds.contains(t.getOrganizeId())).map(PositionEntity::getId).collect(Collectors.toList());
  656. listIds.addAll(orgIds);
  657. listIds.addAll(posIds);
  658. }
  659. if (PermissionConst.POSITION.equals(objectType)) {
  660. List<String> positionIds = allPosList.stream().filter(t -> StringUtil.isNotEmpty(t.getPositionIdTree()) && t.getPositionIdTree().contains(objectId))
  661. .map(PositionEntity::getId).collect(Collectors.toList());
  662. listIds.addAll(positionIds);
  663. }
  664. if (PermissionConst.ROLE.equals(objectType)) {
  665. List<RoleRelationEntity> listByRoleId = roleRelationService.getListByRoleId(objectId, null);
  666. for (RoleRelationEntity rre : listByRoleId) {
  667. if (PermissionConst.ORGANIZE.equals(rre.getObjectType())) {
  668. List<String> orgIds = allOrgList.stream().filter(t -> t.getOrganizeIdTree().contains(rre.getObjectId()))
  669. .map(OrganizeEntity::getId).collect(Collectors.toList());
  670. List<String> posIds = allPosList.stream().filter(t -> listIds.contains(t.getOrganizeId())).map(PositionEntity::getId).collect(Collectors.toList());
  671. listIds.addAll(orgIds);
  672. listIds.addAll(posIds);
  673. } else if (PermissionConst.POSITION.equals(rre.getObjectType())) {
  674. List<String> positionIds = allPosList.stream().filter(t -> t.getPositionIdTree().contains(rre.getObjectId()))
  675. .map(PositionEntity::getId).collect(Collectors.toList());
  676. listIds.addAll(positionIds);
  677. } else {
  678. userIds.addAll(listByRoleId.stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toList()));
  679. }
  680. }
  681. }
  682. if (listIds.size() > 0) {
  683. List<UserRelationEntity> listByObjectIdAll = userRelationService.getListByObjectIdAll(listIds);
  684. userIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
  685. }
  686. // 插入数据
  687. String sql = DbTypeUtil.checkOracle(dataSourceUtils) || DbTypeUtil.checkPostgre(dataSourceUtils) ?
  688. SqlFrameFastUtil.INSERT_AUTHORIZE2 : SqlFrameFastUtil.INSERT_AUTHORIZE;
  689. String column_key = StringUtil.EMPTY, column_plceholder = StringUtil.EMPTY, column_value = TenantDataSourceUtil.getTenantColumn();
  690. if (StringUtil.isNotEmpty(column_value)) {
  691. column_key = StrPool.COMMA + configValueUtil.getMultiTenantColumn();
  692. column_plceholder = ",?";
  693. }
  694. sql = sql.replace("%COLUMN_KEY%", column_key).replace("%COLUMN_PLACEHOLDER%", column_plceholder);
  695. PrepSqlDTO dto = new PrepSqlDTO(sql).withConn(dataSourceUtils, null);
  696. for (int i = 0; i < objectList.size(); i++) {
  697. for (AuthorizeEntity entityItem : authorizeList) {
  698. List<Object> data = new LinkedList<>();
  699. data.add(RandomUtil.uuId());
  700. data.add(entityItem.getItemType());
  701. data.add(entityItem.getItemId());
  702. data.add(objectList.get(i).getObjectType());
  703. data.add(objectList.get(i).getObjectId());
  704. data.add(i);
  705. data.add(DateUtil.getNow());
  706. data.add(userInfo.getUserId());
  707. if (StringUtil.isNotEmpty(column_value)) {
  708. data.add(column_value);
  709. }
  710. dto.addMultiData(data);
  711. }
  712. }
  713. JdbcUtil.creUpDeBatchOneSql(dto);
  714. userService.delCurUser(MsgCode.PS010.get(), userIds);
  715. } catch (DataException e1) {
  716. e1.printStackTrace();
  717. log.error("权限报错:" + e1.getMessage());
  718. throw new DataException(e1.getMessage());
  719. } catch (Exception e) {
  720. e.printStackTrace();
  721. log.error("权限报错:" + e.getMessage());
  722. }
  723. return errStr;
  724. }
  725. private Set<String> getParentMenu(List<ModuleEntity> allMenu, Set<String> childIds) {
  726. Set<String> newIds = new HashSet<>();
  727. if (CollectionUtil.isNotEmpty(childIds)) {
  728. for (ModuleEntity menu : allMenu) {
  729. if (childIds.contains(menu.getId()) && !"-1".equals(menu.getParentId())) {
  730. newIds.add(menu.getParentId());
  731. }
  732. }
  733. if (CollectionUtil.isNotEmpty(newIds)) {
  734. newIds.addAll(getParentMenu(allMenu, newIds));
  735. }
  736. newIds.addAll(childIds);
  737. }
  738. return newIds;
  739. }
  740. /**
  741. * 删除子权限
  742. *
  743. * @param form
  744. * @param allOrgList
  745. * @param allPosList
  746. */
  747. private void deleteAllAuth(AuthorizeDataUpForm form, List<OrganizeEntity> allOrgList, List<PositionEntity> allPosList) {
  748. List<RoleRelationEntity> roleRealationList = roleRelationService.list(new QueryWrapper<>());
  749. List<AuthorizeEntity> allAuthList = this.list(new QueryWrapper<>());
  750. Map<String, List<AuthorizeEntity>> allAuthMap = allAuthList.stream().collect(Collectors.groupingBy(AuthorizeEntity::getObjectId));
  751. //表单提交的列表
  752. List<String> systemSave = form.getSystemIds() == null ? Collections.EMPTY_LIST : Arrays.asList(form.getModule());
  753. List<String> moduleSave = form.getModule() == null ? Collections.EMPTY_LIST : Arrays.asList(form.getModule());
  754. List<String> buttonSave = form.getButton() == null ? Collections.EMPTY_LIST : Arrays.asList(form.getButton());
  755. List<String> columnSave = form.getColumn() == null ? Collections.EMPTY_LIST : Arrays.asList(form.getColumn());
  756. List<String> resourceSave = form.getResource() == null ? Collections.EMPTY_LIST : Arrays.asList(form.getResource());
  757. List<String> formSave = form.getForm() == null ? Collections.EMPTY_LIST : Arrays.asList(form.getForm());
  758. String objectId = form.getObjectId();
  759. //递归获取
  760. List<String> deleteAllAuth = AuthPermUtil.getDelAllAuth(AuthorizeSaveParam
  761. .builder().objectId(form.getObjectId()).objectType(form.getObjectType()).allOrgList(allOrgList).allPosList(allPosList).allAuthMap(allAuthMap)
  762. .roleRealationList(roleRealationList)
  763. .systemSave(systemSave).moduleSave(moduleSave).buttonSave(buttonSave).columnSave(columnSave).resourceSave(resourceSave).formSave(formSave)
  764. .build());
  765. if (CollectionUtil.isNotEmpty(deleteAllAuth)) {
  766. QueryWrapper<AuthorizeEntity> qw = new QueryWrapper<>();
  767. if (deleteAllAuth.size() > 1000) {
  768. List<List<String>> lists = Lists.partition(deleteAllAuth, 1000);
  769. for (List<String> list : lists) {
  770. qw.lambda().in(AuthorizeEntity::getId, list).or();
  771. }
  772. } else {
  773. qw.lambda().in(AuthorizeEntity::getId, deleteAllAuth);
  774. }
  775. this.remove(qw);
  776. }
  777. }
  778. /**
  779. * 权限
  780. */
  781. private void setEntity(String[] ids, String type, List<AuthorizeEntity> entityList, Boolean objectFlag) {
  782. if (ids != null) {
  783. for (String id : ids) {
  784. AuthorizeEntity entity = new AuthorizeEntity();
  785. if (objectFlag) {
  786. entity.setObjectType(type);
  787. entity.setObjectId(id);
  788. } else {
  789. entity.setItemType(type);
  790. entity.setItemId(id);
  791. }
  792. entityList.add(entity);
  793. }
  794. }
  795. }
  796. @Override
  797. public List<AuthorizeEntity> getListByUserId(boolean isAdmin, String userId, boolean standingfilter) {
  798. if (!isAdmin) {
  799. QueryWrapper<UserRelationEntity> queryWrapper = new QueryWrapper<>();
  800. queryWrapper.lambda().eq(UserRelationEntity::getUserId, userId);
  801. queryWrapper.lambda().eq(UserRelationEntity::getObjectType, PermissionConst.POSITION);
  802. List<UserRelationEntity> list = userRelationService.list(queryWrapper);
  803. List<String> userRelationList = list.stream().map(u -> u.getObjectId()).collect(Collectors.toList());
  804. userRelationList.add(userId);
  805. List<String> roleList = roleRelationService.getListByObjectId(userRelationList, null)
  806. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  807. userRelationList.addAll(roleList);
  808. //如果开启身份,根据身份过滤部分权限
  809. List<AuthorizeEntity> listByObjectId = this.getListByObjectId(userRelationList);
  810. List<AuthorizeEntity> standingList = listByObjectId.stream().filter(t -> PermissionConst.STAND.equals(t.getItemId())).collect(Collectors.toList());
  811. if (standingfilter && configValueUtil.isStandingSwitch() && standingList.size() > 0) {
  812. UserEntity info = userService.getInfo(userId);
  813. List<AuthorizeEntity> authorizeByItem = this.getAuthorizeByItem(PermissionConst.STAND, info.getStanding());
  814. List<String> collect = authorizeByItem.stream().map(AuthorizeEntity::getObjectId).collect(Collectors.toList());
  815. userRelationList = userRelationList.stream().filter(t -> collect.contains(t)).collect(Collectors.toList());
  816. }
  817. if (CollectionUtil.isEmpty(userRelationList)) {
  818. return Collections.EMPTY_LIST;
  819. }
  820. QueryWrapper<AuthorizeEntity> wrapper = new QueryWrapper<>();
  821. wrapper.lambda().in(AuthorizeEntity::getObjectId, userRelationList);
  822. return this.list(wrapper);
  823. } else {
  824. return Collections.EMPTY_LIST;
  825. }
  826. }
  827. @Override
  828. public List<AuthorizeEntity> getListByPosOrRoleId(String objectId, String objectType) {
  829. QueryWrapper<AuthorizeEntity> wrapper = new QueryWrapper<>();
  830. if (PermissionConst.POSITION.equals(objectType)) {
  831. List<String> posId = userRelationService.getListByObjectId(objectId, objectType).stream().map(u -> u.getObjectId()).collect(Collectors.toList());
  832. List<String> posRoleList = roleRelationService.getListByObjectId(posId, null)
  833. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  834. posId.addAll(posRoleList);
  835. wrapper.lambda().in(AuthorizeEntity::getObjectId, posId);
  836. } else {
  837. wrapper.lambda().eq(AuthorizeEntity::getObjectId, objectId);
  838. wrapper.lambda().eq(AuthorizeEntity::getObjectType, PermissionConst.ROLE);
  839. }
  840. return this.list(wrapper);
  841. }
  842. @Override
  843. public List<AuthorizeEntity> getListByObjectId(List<String> objectId) {
  844. if (objectId.size() == 0) {
  845. return new ArrayList<>();
  846. }
  847. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  848. queryWrapper.lambda().in(AuthorizeEntity::getObjectId, objectId);
  849. return this.list(queryWrapper);
  850. }
  851. @Override
  852. public Boolean existAuthorize(String roleId, String systemId) {
  853. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  854. queryWrapper.lambda().eq(AuthorizeEntity::getObjectId, roleId);
  855. if (StringUtil.isNotEmpty(systemId)) {
  856. queryWrapper.lambda().eq(AuthorizeEntity::getItemId, systemId);
  857. queryWrapper.lambda().eq(AuthorizeEntity::getItemType, AuthorizeConst.SYSTEM);
  858. }
  859. return this.count(queryWrapper) > 0;
  860. }
  861. @Override
  862. public List<AuthorizeEntity> getListByRoleId(String roleId) {
  863. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  864. queryWrapper.lambda().eq(AuthorizeEntity::getObjectId, roleId);
  865. return this.list(queryWrapper);
  866. }
  867. @Override
  868. public List<AuthorizeEntity> getListByObjectId(String objectId, String itemType) {
  869. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  870. if (StringUtil.isNotEmpty(objectId)) {
  871. queryWrapper.lambda().eq(AuthorizeEntity::getObjectId, objectId);
  872. }
  873. queryWrapper.lambda().eq(AuthorizeEntity::getItemType, itemType);
  874. return this.list(queryWrapper);
  875. }
  876. @Override
  877. public List<AuthorizeEntity> getListByObjectAndItem(String itemId, String objectType) {
  878. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  879. queryWrapper.lambda().eq(AuthorizeEntity::getObjectType, objectType).eq(AuthorizeEntity::getItemId, itemId);
  880. return this.list(queryWrapper);
  881. }
  882. @Override
  883. public List<AuthorizeEntity> getListByObjectAndItemIdAndType(String itemId, String itemType) {
  884. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  885. queryWrapper.lambda().eq(AuthorizeEntity::getItemType, itemType).eq(AuthorizeEntity::getItemId, itemId);
  886. return this.list(queryWrapper);
  887. }
  888. @Override
  889. public List<UserSystemVO> getUserStanding(boolean isLogin, String userId) {
  890. return getUserStanding(isLogin, userId, null);
  891. }
  892. @Override
  893. public List<UserSystemVO> getUserStanding(boolean isLogin, String userId, String loginDevice) {
  894. UserInfo userInfo = UserProvider.getUser();
  895. List<UserSystemVO> systemVOS = new ArrayList<>();
  896. UserEntity info = userService.getInfo(userId);
  897. String device = StringUtil.isNotEmpty(loginDevice) ? loginDevice : userInfo.getLoginDevice();
  898. if (DeviceType.PC.getDevice().equals(device)) {
  899. if (ObjectUtil.isNotEmpty(info.getStanding())) {
  900. UserSystemVO vo = new UserSystemVO();
  901. vo.setId(info.getStanding() + "");
  902. vo.setCurrentStanding(true);
  903. systemVOS.add(vo);
  904. }
  905. } else {
  906. if (ObjectUtil.isNotEmpty(info.getAppStanding())) {
  907. UserSystemVO vo = new UserSystemVO();
  908. vo.setId(info.getAppStanding() + "");
  909. vo.setCurrentStanding(true);
  910. systemVOS.add(vo);
  911. }
  912. }
  913. List<OrganizeAdministratorEntity> infoByUserId = organizeAdminIsTratorService.getInfoByUserId(userId);
  914. if (isLogin) {
  915. if (infoByUserId.size() > 0) {
  916. UserSystemVO vo = new UserSystemVO();
  917. vo.setId("2");
  918. systemVOS.add(vo);
  919. }
  920. } else {
  921. if (systemVOS.size() == 0) {
  922. if (Objects.equals(info.getIsAdministrator(), 1)) {
  923. UserSystemVO vo = new UserSystemVO();
  924. vo.setId("1");
  925. systemVOS.add(vo);
  926. } else if (infoByUserId.size() > 0) {
  927. UserSystemVO vo = new UserSystemVO();
  928. vo.setId("2");
  929. systemVOS.add(vo);
  930. } else {
  931. UserSystemVO vo = new UserSystemVO();
  932. vo.setId("3");
  933. systemVOS.add(vo);
  934. }
  935. }
  936. }
  937. return systemVOS;
  938. }
  939. @Override
  940. @DS("")
  941. public List<SuperJsonModel> getConditionSql(String moduleId, String systemCode) {
  942. List<SuperJsonModel> list = new ArrayList<>();
  943. UserInfo userInfo = UserProvider.getUser();
  944. String reidsKey = cacheKeyUtil.getUserAuthorize() + moduleId + "_" + userInfo.getUserId();
  945. long time = 60 * 5;
  946. AuthorizeVO model;
  947. if (redisUtil.exists(reidsKey)) {
  948. model = JsonUtil.getJsonToBean(redisUtil.getString(reidsKey).toString(), AuthorizeVO.class);
  949. } else {
  950. model = this.getAuthorize(false, systemCode, 0);
  951. redisUtil.insert(reidsKey, JsonUtil.getObjectToString(model), time);
  952. }
  953. if (model == null) {
  954. return new ArrayList<>();
  955. }
  956. List<ResourceModel> resourceListAll = model.getResourceList().stream().filter(m -> m.getModuleId().equals(moduleId)).collect(Collectors.toList());
  957. //先遍历一次 查找其中有没有全部方案
  958. boolean isAll = resourceListAll.stream().filter(item -> "jnpf_alldata".equals(item.getEnCode()) || item.getEnCode().startsWith("jnpf_alldata")).count() > 0;
  959. //未分配权限方案
  960. if (isAll || userInfo.getIsAdministrator()) {
  961. SuperJsonModel superJsonModel = new SuperJsonModel();
  962. list.add(superJsonModel);
  963. return list;
  964. }
  965. Map<String, List<ResourceModel>> authorizeMap = resourceListAll.stream().filter(t -> StringUtil.isNotEmpty(t.getObjectId())).collect(Collectors.groupingBy(ma -> ma.getObjectId()));
  966. int num = 0;
  967. //方案
  968. for (String key : authorizeMap.keySet()) {
  969. List<ResourceModel> resourceList = authorizeMap.get(key);
  970. boolean authorizeLogic = num == 0;
  971. for (ResourceModel item : resourceList) {
  972. AuthConditionModel authConditionModel = JsonUtil.getJsonToBean(item.getConditionJson(), AuthConditionModel.class);
  973. String matchLogic = authConditionModel.getMatchLogic();
  974. List<SuperQueryJsonModel> conditionList = new ArrayList<>();
  975. //分组
  976. for (AuthGroup group : authConditionModel.getConditionList()) {
  977. String logic = group.getLogic();
  978. List<FieLdsModel> groupList = new ArrayList<>();
  979. //条件
  980. for (AuthItem fieldItem : group.getGroups()) {
  981. FieLdsModel fieLdsModel = JsonUtil.getJsonToBean(fieldItem, FieLdsModel.class);
  982. String itemField = fieldItem.getField();
  983. String table = fieldItem.getTableName();
  984. String vModel = "";
  985. if (itemField.contains("_jnpf_")) {
  986. vModel = itemField.split("_jnpf_")[1];
  987. } else if (itemField.toLowerCase().startsWith("tablefield")) {
  988. vModel = itemField.split("-")[1];
  989. } else {
  990. vModel = itemField;
  991. }
  992. ConfigModel config = fieLdsModel.getConfig();
  993. String jnpfKey = fieldItem.getJnpfKey();
  994. if (AuthorizeConditionEnum.CURRENTTIME.getCondition().equals(jnpfKey)) {
  995. jnpfKey = AuthorizeConst.DATE_PICKER;
  996. }
  997. config.setJnpfKey(jnpfKey);
  998. config.setTableName(table);
  999. fieLdsModel.setConfig(config);
  1000. fieLdsModel.setSymbol(fieldItem.getSymbol());
  1001. fieLdsModel.setVModel(vModel);
  1002. fieLdsModel.setId(itemField);
  1003. fieLdsModel.setFieldValue(fieldItem.getFieldValue());
  1004. groupList.add(fieLdsModel);
  1005. }
  1006. //搜索条件
  1007. SuperQueryJsonModel queryJsonModel = new SuperQueryJsonModel();
  1008. queryJsonModel.setGroups(groupList);
  1009. queryJsonModel.setLogic(logic);
  1010. conditionList.add(queryJsonModel);
  1011. }
  1012. if (conditionList.size() > 0) {
  1013. SuperJsonModel superJsonModel = new SuperJsonModel();
  1014. superJsonModel.setMatchLogic(matchLogic);
  1015. superJsonModel.setConditionList(conditionList);
  1016. superJsonModel.setAuthorizeLogic(authorizeLogic);
  1017. list.add(superJsonModel);
  1018. }
  1019. }
  1020. num += list.size() > 0 ? 1 : 0;
  1021. }
  1022. return list;
  1023. }
  1024. @Override
  1025. public void removeAuthByUserOrMenu(List<String> userIds, List<String> menuIds) {
  1026. userIds = userIds == null ? new ArrayList<>() : userIds;
  1027. menuIds = menuIds == null ? new ArrayList<>() : menuIds;
  1028. Set<String> allKeys = redisUtil.getAllKeys();
  1029. for (String cacheKey : allKeys) {
  1030. for (String user : userIds) {
  1031. if (cacheKey.startsWith(cacheKeyUtil.getUserAuthorize()) && cacheKey.contains(user)) {
  1032. redisUtil.remove(cacheKey);
  1033. }
  1034. }
  1035. for (String menuId : menuIds) {
  1036. if (cacheKey.startsWith(cacheKeyUtil.getUserAuthorize()) && cacheKey.contains(menuId)) {
  1037. redisUtil.remove(cacheKey);
  1038. }
  1039. }
  1040. }
  1041. }
  1042. @Override
  1043. public boolean getUserCurrentStanding(String userId, Integer standType) {
  1044. if (UserProvider.getUser() == null || UserProvider.getUser().getUserId() == null) return false;
  1045. List<UserSystemVO> userStanding = this.getUserStanding(false, UserProvider.getUser().getUserId());
  1046. if (standType != null && userStanding.stream().anyMatch(t -> standType.toString().equals(t.getId()))) {
  1047. return true;
  1048. }
  1049. return false;
  1050. }
  1051. }