Browse Source

角色权限设置功能

chenw 7 years ago
parent
commit
4d74d05a7e

+ 44 - 0
base-servers/account/account-dto/src/main/java/com/usoftchina/saas/account/dto/PowerSetDTO.java

@@ -0,0 +1,44 @@
+package com.usoftchina.saas.account.dto;
+
+import java.io.Serializable;
+
+public class PowerSetDTO implements Serializable {
+
+    private Long groupId;
+    private String classify;
+    private Long resourceId;
+    private boolean checked;
+
+    public Long getGroupId() {
+        return groupId;
+    }
+
+    public void setGroupId(Long groupId) {
+        this.groupId = groupId;
+    }
+
+    public String getClassify() {
+        return classify;
+    }
+
+    public void setClassify(String classify) {
+        this.classify = classify;
+    }
+
+    public Long getResourceId() {
+        return resourceId;
+    }
+
+    public void setResourceId(Long resourceId) {
+        this.resourceId = resourceId;
+    }
+
+    public boolean isChecked() {
+        return checked;
+    }
+
+    public void setChecked(boolean checked) {
+        this.checked = checked;
+    }
+
+}

+ 25 - 0
base-servers/account/account-dto/src/main/java/com/usoftchina/saas/account/dto/PowerSetListDTO.java

@@ -0,0 +1,25 @@
+package com.usoftchina.saas.account.dto;
+
+import java.io.Serializable;
+import java.util.List;
+
+public class PowerSetListDTO implements Serializable {
+    private Long roleId;
+    private List<PowerSetDTO> powerSetList;
+
+    public Long getRoleId() {
+        return roleId;
+    }
+
+    public void setRoleId(Long roleId) {
+        this.roleId = roleId;
+    }
+
+    public List<PowerSetDTO> getPowerSetList() {
+        return powerSetList;
+    }
+
+    public void setPowerSetList(List<PowerSetDTO> powerSetList) {
+        this.powerSetList = powerSetList;
+    }
+}

+ 13 - 0
base-servers/account/account-server/src/main/java/com/usoftchina/saas/account/controller/RoleController.java

@@ -1,6 +1,7 @@
 package com.usoftchina.saas.account.controller;
 package com.usoftchina.saas.account.controller;
 
 
 import com.usoftchina.saas.account.cache.AccountCache;
 import com.usoftchina.saas.account.cache.AccountCache;
+import com.usoftchina.saas.account.dto.PowerSetListDTO;
 import com.usoftchina.saas.account.dto.RoleDTO;
 import com.usoftchina.saas.account.dto.RoleDTO;
 import com.usoftchina.saas.account.dto.RoleSaveDTO;
 import com.usoftchina.saas.account.dto.RoleSaveDTO;
 import com.usoftchina.saas.account.dto.RoleUpdateDTO;
 import com.usoftchina.saas.account.dto.RoleUpdateDTO;
@@ -116,6 +117,13 @@ public class RoleController {
         return Result.success();
         return Result.success();
     }
     }
 
 
+    @PostMapping("/save/bind/resource")
+    public Result updateResource(@RequestBody PowerSetListDTO powerSetListDTO){
+        roleService.saveRoleResource(powerSetListDTO);
+        clearAccountCacheByRoleId(powerSetListDTO.getRoleId());
+        return Result.success();
+    }
+
     /**
     /**
      * 角色解除绑定资源
      * 角色解除绑定资源
      *
      *
@@ -130,6 +138,11 @@ public class RoleController {
         return Result.success();
         return Result.success();
     }
     }
 
 
+    /**
+     * 获取所有资源 + 角色资源 绑定状态
+     * @param id
+     * @return
+     */
     @GetMapping("/power/resource")
     @GetMapping("/power/resource")
     public Result getResourcePower(Long id){
     public Result getResourcePower(Long id){
         return Result.success(roleService.getResourcePowerById(id));
         return Result.success(roleService.getResourcePowerById(id));

+ 9 - 3
base-servers/account/account-server/src/main/java/com/usoftchina/saas/account/mapper/RoleResourceMapper.java

@@ -63,9 +63,15 @@ public interface RoleResourceMapper {
     List<Map<String, Object>> selectClassifyByRoleId(@Param("id") Long id);
     List<Map<String, Object>> selectClassifyByRoleId(@Param("id") Long id);
 
 
     /**
     /**
-     * 通过资源groupId和角色ID查询 角色是否具有改资源的权限
-     * @param roleId
+     * 查询其他权限
+     * @return
+     */
+    List<OtherPower> selectOthersPower();
+
+    /**
+     * 批量插入
+     * @param roleResourceList
      * @return
      * @return
      */
      */
-    List<OtherPower> selectOthersPower(@Param("roleId") Long roleId);
+    boolean batchInsert(List<RoleResource> roleResourceList);
 }
 }

+ 9 - 0
base-servers/account/account-server/src/main/java/com/usoftchina/saas/account/po/OtherPower.java

@@ -8,6 +8,15 @@ public class OtherPower implements Serializable {
     private String name;
     private String name;
     private boolean checked;
     private boolean checked;
     private Long groupId;
     private Long groupId;
+    private Long roleId;
+
+    public Long getRoleId() {
+        return roleId;
+    }
+
+    public void setRoleId(Long roleId) {
+        this.roleId = roleId;
+    }
 
 
     public Long getResourceId() {
     public Long getResourceId() {
         return resourceId;
         return resourceId;

+ 19 - 9
base-servers/account/account-server/src/main/java/com/usoftchina/saas/account/po/RoleResource.java

@@ -9,18 +9,18 @@ import java.io.Serializable;
  * @date 2018/10/2
  * @date 2018/10/2
  */
  */
 public class RoleResource implements Serializable {
 public class RoleResource implements Serializable {
-    private long roleId;
+    private Long roleId;
     /**
     /**
      * 资源所属应用
      * 资源所属应用
      * <p>冗余字段,方便查询</p>
      * <p>冗余字段,方便查询</p>
      */
      */
     private String appId;
     private String appId;
-    private long resourceId;
+    private Long resourceId;
     /**
     /**
      * 角色所属公司
      * 角色所属公司
      * <p>冗余字段,方便查询</p>
      * <p>冗余字段,方便查询</p>
      */
      */
-    private long companyId;
+    private Long companyId;
 
 
     public String getAppId() {
     public String getAppId() {
         return appId;
         return appId;
@@ -30,27 +30,37 @@ public class RoleResource implements Serializable {
         this.appId = appId;
         this.appId = appId;
     }
     }
 
 
-    public long getCompanyId() {
+    public Long getCompanyId() {
         return companyId;
         return companyId;
     }
     }
 
 
-    public void setCompanyId(long companyId) {
+    public void setCompanyId(Long companyId) {
         this.companyId = companyId;
         this.companyId = companyId;
     }
     }
 
 
-    public long getRoleId() {
+    public Long getRoleId() {
         return roleId;
         return roleId;
     }
     }
 
 
-    public void setRoleId(long roleId) {
+    public void setRoleId(Long roleId) {
         this.roleId = roleId;
         this.roleId = roleId;
     }
     }
 
 
-    public long getResourceId() {
+    public Long getResourceId() {
         return resourceId;
         return resourceId;
     }
     }
 
 
-    public void setResourceId(long resourceId) {
+    public void setResourceId(Long resourceId) {
         this.resourceId = resourceId;
         this.resourceId = resourceId;
     }
     }
+
+    public RoleResource(Long roleId, String appId, Long resourceId, Long companyId) {
+        this.roleId = roleId;
+        this.appId = appId;
+        this.resourceId = resourceId;
+        this.companyId = companyId;
+    }
+
+    public RoleResource() {
+    }
 }
 }

+ 8 - 0
base-servers/account/account-server/src/main/java/com/usoftchina/saas/account/service/RoleService.java

@@ -1,6 +1,7 @@
 package com.usoftchina.saas.account.service;
 package com.usoftchina.saas.account.service;
 
 
 import com.usoftchina.saas.account.dto.PowerDTO;
 import com.usoftchina.saas.account.dto.PowerDTO;
+import com.usoftchina.saas.account.dto.PowerSetListDTO;
 import com.usoftchina.saas.account.dto.RoleBaseDTO;
 import com.usoftchina.saas.account.dto.RoleBaseDTO;
 import com.usoftchina.saas.account.mapper.RoleMapper;
 import com.usoftchina.saas.account.mapper.RoleMapper;
 import com.usoftchina.saas.account.po.Role;
 import com.usoftchina.saas.account.po.Role;
@@ -44,4 +45,11 @@ public interface RoleService extends CommonBaseService<RoleMapper, Role> {
      * @return
      * @return
      */
      */
     List<PowerDTO> getResourcePowerById(Long id);
     List<PowerDTO> getResourcePowerById(Long id);
+
+    /**
+     * 保存角色权限
+     * @param powerSetListDTO
+     * @return
+     */
+    boolean saveRoleResource(PowerSetListDTO powerSetListDTO);
 }
 }

+ 52 - 9
base-servers/account/account-server/src/main/java/com/usoftchina/saas/account/service/impl/RoleServiceImpl.java

@@ -1,9 +1,7 @@
 package com.usoftchina.saas.account.service.impl;
 package com.usoftchina.saas.account.service.impl;
 
 
 import com.usoftchina.saas.account.constant.RoleType;
 import com.usoftchina.saas.account.constant.RoleType;
-import com.usoftchina.saas.account.dto.OtherPowerDTO;
-import com.usoftchina.saas.account.dto.PowerDTO;
-import com.usoftchina.saas.account.dto.RoleBaseDTO;
+import com.usoftchina.saas.account.dto.*;
 import com.usoftchina.saas.account.mapper.*;
 import com.usoftchina.saas.account.mapper.*;
 import com.usoftchina.saas.account.po.*;
 import com.usoftchina.saas.account.po.*;
 import com.usoftchina.saas.account.service.ResourceService;
 import com.usoftchina.saas.account.service.ResourceService;
@@ -94,7 +92,7 @@ public class RoleServiceImpl extends CommonBaseServiceImpl<RoleMapper, Role> imp
     public List<PowerDTO> getResourcePowerById(Long id) {
     public List<PowerDTO> getResourcePowerById(Long id) {
         List<PowerDTO> powerDTOList = new ArrayList<PowerDTO>();
         List<PowerDTO> powerDTOList = new ArrayList<PowerDTO>();
         List<Map<String, Object>> classifyList = roleResourceMapper.selectClassifyByRoleId(id);
         List<Map<String, Object>> classifyList = roleResourceMapper.selectClassifyByRoleId(id);
-        List<OtherPower> otherPowerList = roleResourceMapper.selectOthersPower(id);
+        List<OtherPower> otherPowerList = roleResourceMapper.selectOthersPower();
         //查询所有模块
         //查询所有模块
         List<ResourceModule> modules = resourceModuleMapper.selectByAppId(BaseContextHolder.getAppId());
         List<ResourceModule> modules = resourceModuleMapper.selectByAppId(BaseContextHolder.getAppId());
         for (ResourceModule module : modules){
         for (ResourceModule module : modules){
@@ -110,9 +108,7 @@ public class RoleServiceImpl extends CommonBaseServiceImpl<RoleMapper, Role> imp
                     if (group.getId().equals(map.get("group_id"))){
                     if (group.getId().equals(map.get("group_id"))){
                         String classify = String.valueOf(map.get("classify"));
                         String classify = String.valueOf(map.get("classify"));
                         setPowerValue(powerDTO, classify);
                         setPowerValue(powerDTO, classify);
-                        if (classify.contains("OTHER")){
-                            setOtherPower(powerDTO, group.getId(), otherPowerList);
-                        }
+                        setOtherPower(powerDTO, group.getId(), id, otherPowerList);
                     }
                     }
                 }
                 }
                 powerDTOList.add(powerDTO);
                 powerDTOList.add(powerDTO);
@@ -121,10 +117,56 @@ public class RoleServiceImpl extends CommonBaseServiceImpl<RoleMapper, Role> imp
         return powerDTOList;
         return powerDTOList;
     }
     }
 
 
-    private void setOtherPower(PowerDTO powerDTO, Long groupId, List<OtherPower> otherPowerList) {
+    @Override
+    public boolean saveRoleResource(PowerSetListDTO powerSetListDTO) {
+        Long roleId = powerSetListDTO.getRoleId();
+        Long companyId = BaseContextHolder.getCompanyId();
+        String appId = BaseContextHolder.getAppId();
+        List<RoleResource> addRoleResourceList = new ArrayList<RoleResource>();
+        List<RoleResource> delRoleResourceList = new ArrayList<RoleResource>();
+
+        for (PowerSetDTO powerSetDTO : powerSetListDTO.getPowerSetList()){
+            List<Resource> resourceList = resourceMapper.selectByGroupId(powerSetDTO.getGroupId());
+            if (powerSetDTO.isChecked()) {
+                if (powerSetDTO.getGroupId() != null && powerSetDTO.getResourceId() == null) {
+                    for (Resource resource : resourceList){
+                        if (resource.getClassify().equalsIgnoreCase(powerSetDTO.getClassify())){
+                            RoleResource roleResource = new RoleResource(roleId, appId, resource.getId(), companyId);
+                            addRoleResourceList.add(roleResource);
+                        }
+                    }
+                } else if (powerSetDTO.getResourceId() != null && powerSetDTO.getGroupId() == null) {
+                    roleResourceMapper.delete(roleId, powerSetDTO.getResourceId());
+                    RoleResource roleResource = new RoleResource(roleId, appId, powerSetDTO.getResourceId(), companyId);
+                    addRoleResourceList.add(roleResource);
+                }
+            }else{
+                if (powerSetDTO.getGroupId() != null && powerSetDTO.getResourceId() == null) {
+                    for (Resource resource : resourceList){
+                        if (resource.getClassify().equalsIgnoreCase(powerSetDTO.getClassify())){
+                            RoleResource roleResource = new RoleResource(roleId, appId, resource.getId(), companyId);
+                            delRoleResourceList.add(roleResource);
+                        }
+                    }
+                } else if (powerSetDTO.getResourceId() != null && powerSetDTO.getGroupId() == null) {
+                    RoleResource roleResource = new RoleResource(roleId, appId, powerSetDTO.getResourceId(), companyId);
+                    delRoleResourceList.add(roleResource);
+                }
+            }
+        }
+        for (RoleResource roleResource : delRoleResourceList) {
+            roleResourceMapper.delete(roleResource.getRoleId(), roleResource.getResourceId());
+        }
+        if (addRoleResourceList.size() > 0) {
+            roleResourceMapper.batchInsert(addRoleResourceList);
+        }
+        return true;
+    }
+
+    private void setOtherPower(PowerDTO powerDTO, Long groupId, Long roleId, List<OtherPower> otherPowerList) {
         List<OtherPower> otherPowers = new ArrayList<OtherPower>();
         List<OtherPower> otherPowers = new ArrayList<OtherPower>();
         for (OtherPower otherPower : otherPowerList){
         for (OtherPower otherPower : otherPowerList){
-            if (otherPower.getGroupId().equals(groupId)){
+            if (groupId.equals(otherPower.getGroupId())){
                 otherPowers.add(otherPower);
                 otherPowers.add(otherPower);
             }
             }
         }
         }
@@ -138,6 +180,7 @@ public class RoleServiceImpl extends CommonBaseServiceImpl<RoleMapper, Role> imp
             switch (array[i]){
             switch (array[i]){
                 case "QUERY":
                 case "QUERY":
                     powerDTO.setQuery(true);
                     powerDTO.setQuery(true);
+                    powerDTO.setFileExport(true);
                     break;
                     break;
                 case "ADD":
                 case "ADD":
                     powerDTO.setAdd(true);
                     powerDTO.setAdd(true);

+ 13 - 2
base-servers/account/account-server/src/main/resources/mapper/RoleResourceMapper.xml

@@ -36,10 +36,21 @@
         <result column="id" property="resourceId" jdbcType="BIGINT" />
         <result column="id" property="resourceId" jdbcType="BIGINT" />
         <result column="group_id" property="groupId" jdbcType="BIGINT" />
         <result column="group_id" property="groupId" jdbcType="BIGINT" />
         <result column="checked" property="checked" jdbcType="BOOLEAN" />
         <result column="checked" property="checked" jdbcType="BOOLEAN" />
+        <result column="role_id" property="roleId" jdbcType="BIGINT" />
     </resultMap>
     </resultMap>
     <select id="selectOthersPower" resultMap="OtherPowerResultMap">
     <select id="selectOthersPower" resultMap="OtherPowerResultMap">
-      SELECT name,id,group_id,CASE WHEN ROLE_ID IS NULL THEN false ELSE true end checked FROM AC_RESOURCE
+      SELECT name,id,group_id,role_id,CASE WHEN ROLE_ID IS NULL THEN false ELSE true end checked FROM AC_RESOURCE
       LEFT JOIN ac_role_resource ON ID = RESOURCE_ID
       LEFT JOIN ac_role_resource ON ID = RESOURCE_ID
-      WHERE CLASSIFY='OTHER' AND ROLE_ID=#{roleId}
+      WHERE CLASSIFY='OTHER'
     </select>
     </select>
+    <insert id="batchInsert" parameterType="java.util.List">
+        INSERT INTO AC_ROLE_RESOURCE(ROLE_ID, RESOURCE_ID, COMPANY_ID, APP_ID)
+        VALUES
+        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
+            (
+              #{item.roleId, jdbcType=BIGINT}, #{item.resourceId,jdbcType=BIGINT},
+              #{item.companyId, jdbcType=BIGINT}, #{item.appId,jdbcType=VARCHAR}
+            )
+        </foreach>
+    </insert>
 </mapper>
 </mapper>