Sfoglia il codice sorgente

添加修改角色时给角色配置必备权限

songxinlu 3 anni fa
parent
commit
90c6f34242

+ 42 - 6
security-center/src/main/java/com/lantone/security/facade/RoleManagementFacade.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.google.common.collect.Lists;
 import com.lantone.common.dto.GetCreateRoleDTO;
 import com.lantone.common.dto.GetDictionaryInfoByTypeDTO;
 import com.lantone.common.dto.GetRoleDTO;
@@ -51,6 +52,7 @@ import com.lantone.dblayermbg.service.impl.RoleSoftwareResourceServiceImpl;
 import com.lantone.common.enums.IsDeleteEnum;
 import com.lantone.security.enums.DictionaryEnum;
 import com.lantone.security.enums.RelationEnum;
+import com.lantone.security.enums.ReturnTypeEnum;
 import com.lantone.security.enums.SoftwareEnum;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -146,7 +148,7 @@ public class RoleManagementFacade {
      */
     public Boolean addRole(AddRoleVO addRoleVO) {
         //校验
-        checkRole(null,addRoleVO.getName(), addRoleVO.getSoftwares());
+        checkRole(null, addRoleVO.getName(), addRoleVO.getSoftwares());
 
         //1.角色表插入角色信息
         addRoleVO.setCreator(SysUserUtils.getCurrentPrincipleId());
@@ -184,7 +186,7 @@ public class RoleManagementFacade {
         } else {
             if (ListUtil.isNotEmpty(roles)) {
                 roles.stream().forEach(role -> {
-                    if (!id.equals( role.getId())) {
+                    if (!id.equals(role.getId())) {
                         Asserts.fail("该角色已存在请重新输入");
                     }
                 });
@@ -243,11 +245,30 @@ public class RoleManagementFacade {
     public Boolean saveRoleSoftwareMenu(List<AddRoleSoftwareVO> softwares, Long roleId) {
         //插入sys_role_software_menu,sys_role_software_resource
         AtomicReference<Boolean> out = new AtomicReference<>(false);
+        //获取角色必备的权限添加给用户
+        String hasMenuResourceStr = dictionaryInfoFacade.getDicMap(ReturnTypeEnum.INTERFACE.getKey(),
+                Lists.newArrayList(DictionaryEnum.MANAGE_HIDE_MENU.getKey() + ""))
+                .get(DictionaryEnum.MANAGE_HIDE_MENU.getKey() + "").get("hasMenuResource");
+
         softwares.stream().forEach(addRoleSoftwareVO -> {
+            JSONObject hasMenuResourceJSON = null;
+            if (StringUtil.isNotEmpty(hasMenuResourceStr)) {
+                hasMenuResourceJSON = JSONObject.parseObject(hasMenuResourceStr);
+            }
             if (ListUtil.isNotEmpty(addRoleSoftwareVO.getSoftwareMenuIds())) {
                 //1.添加该角色对应的系统功能菜单关系sys_role_software_menu
                 List<RoleSoftwareMenu> roleSoftwareMenus = new ArrayList<>();
-                addRoleSoftwareVO.getSoftwareMenuIds().stream().forEach(softwareMenuId -> {
+                //去重
+                Set<Long> softwareMenus = new HashSet(addRoleSoftwareVO.getSoftwareMenuIds());
+                try {
+                    if (hasMenuResourceJSON != null) {
+                        softwareMenus.addAll(hasMenuResourceJSON.getJSONObject(addRoleSoftwareVO.getId() + "")
+                                .getJSONArray("menu").toJavaList(Long.class));
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+                softwareMenus.stream().forEach(softwareMenuId -> {
                     RoleSoftwareMenu roleSoftwareMenu = new RoleSoftwareMenu();
                     roleSoftwareMenu.setRoleId(roleId);
                     roleSoftwareMenu.setSoftwareMenuId(softwareMenuId);
@@ -260,6 +281,14 @@ public class RoleManagementFacade {
                 List<RoleSoftwareResource> roleSoftwareResources = new ArrayList<>();
                 //资源去重
                 Set<Long> softwareResourceIds = new HashSet(addRoleSoftwareVO.getSoftwareResourceIds());
+                try {
+                    if (hasMenuResourceJSON != null) {
+                        softwareResourceIds.addAll(hasMenuResourceJSON.getJSONObject(addRoleSoftwareVO.getId() + "")
+                                .getJSONArray("resource").toJavaList(Long.class));
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
                 softwareResourceIds.stream().forEach(softwareResourceId -> {
                     RoleSoftwareResource roleSoftwareResource = new RoleSoftwareResource();
                     roleSoftwareResource.setRoleId(roleId);
@@ -331,7 +360,7 @@ public class RoleManagementFacade {
 
     /**
      * @param roleId
-     * @param type 区分组织管理添加角色、普通添加角色的功能菜单不同
+     * @param type   区分组织管理添加角色、普通添加角色的功能菜单不同
      * @Description通过角色id获取角色详情
      * @Return com.lantone.common.dto.GetRoleDTO
      */
@@ -464,8 +493,15 @@ public class RoleManagementFacade {
         //获取type下要隐藏的菜单
         List<GetDictionaryInfoByTypeDTO> configHideMenus = dictionaryInfoFacade.getBaseMapper()
                 .getDictionaryInfoByType(DictionaryEnum.MANAGE_HIDE_MENU.getKey());
-        if (ListUtil.isNotEmpty(configHideMenus)) {
-            hideMenus = JSONArray.parseArray(JSONObject.parseObject(configHideMenus.get(0).getVal()).getString(type));
+        String dictionaryInfoStr  = dictionaryInfoFacade.getDicMap(ReturnTypeEnum.INTERFACE.getKey(),
+                Lists.newArrayList(DictionaryEnum.MANAGE_HIDE_MENU.getKey() + ""))
+                .get(DictionaryEnum.MANAGE_HIDE_MENU.getKey() + "").get("manageHideMenu");
+        if (StringUtil.isNotEmpty(dictionaryInfoStr)) {
+            try {
+                hideMenus = JSONObject.parseObject(dictionaryInfoStr).getJSONArray(type).toJavaList(Long.class);
+            }catch (Exception e){
+                e.printStackTrace();
+            }
         }
         List<UserMenuResourceTreeDTO> userMenuResourceTree = roleSoftwareMenuFacade.getBaseMapper().getMenuResourceRelation(loginRoles, selectRoles, hideMenus);
         return getTree(userMenuResourceTree);