Browse Source

查询用户某应用下所有角色、用户所有角色增加应用条件

ZL 3 months ago
parent
commit
daa3e76f37

+ 10 - 2
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysRoleController.java

@@ -9,6 +9,7 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -139,6 +140,11 @@ public class SysRoleController {
 		result.setResult(pageList);
 		return result;
 	}
+
+	/*
+	* 查询用户所拥有的全部角色
+	* */
+
 	
 	/**
 	  *   添加
@@ -269,7 +275,7 @@ public class SysRoleController {
 		}
 		return result;
 	}
-	
+
 	/**
 	  * 通过id查询
 	 * @param id
@@ -313,6 +319,8 @@ public class SysRoleController {
 		return result;
 	}
 
+
+
 	/**
 	 * 查询全部系统角色(不做租户隔离)
 	 *
@@ -497,7 +505,7 @@ public class SysRoleController {
 	
 	/**
 	 * 用户角色授权功能,查询菜单权限树
-	 * @param request
+	 * @param
 	 * @return
 	 */
 	@RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)

+ 12 - 3
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysUserController.java

@@ -284,9 +284,20 @@ public class SysUserController {
 
     @RequiresPermissions("system:user:queryUserRole")
     @RequestMapping(value = "/queryUserRole", method = RequestMethod.GET)
-    public Result<List<String>> queryUserRole(@RequestParam(name = "userid", required = true) String userid) {
+    public Result<List<String>> queryUserRole(@RequestParam(name = "userid", required = true) String userid,
+                                              @RequestParam(name = "appId", required = false) String appId) {
         Result<List<String>> result = new Result<>();
         List<String> list = new ArrayList<String>();
+
+        if (appId != null){
+            List<String> roleIds = sysUserRoleService.listByAppId(appId, userid);
+            if (roleIds != null && roleIds.size() > 0) {
+                result.setSuccess(true);
+                result.setResult(roleIds);
+                return result;
+            }
+            result.error500("未找到应用下用户相关角色信息");
+        }
         List<SysUserRole> userRole = sysUserRoleService.list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userid));
         if (userRole == null || userRole.size() <= 0) {
             result.error500("未找到用户相关角色信息");
@@ -678,9 +689,7 @@ public class SysUserController {
                 SysUserRole one = sysUserRoleService.getOne(queryWrapper);
                 if(one==null){
                     sysUserRoleService.save(sysUserRole);
-
                 }
-
             }
             result.setMessage("添加成功!");
             result.setSuccess(true);

+ 3 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysRoleMapper.java

@@ -38,6 +38,7 @@ public interface SysRoleMapper extends BaseMapper<SysRole> {
     @InterceptorIgnore(tenantLine = "true")
     SysRole getRoleNoTenant(@Param("roleCode") String roleCode, @Param("appId") String appId);
 
+    
     /**
      * 根据用户id查询用户拥有的角色Code
      *
@@ -75,4 +76,6 @@ public interface SysRoleMapper extends BaseMapper<SysRole> {
     Long getRoleCountByTenantId(@Param("id") String id, @Param("tenantId") Integer tenantId);
 
     List<SysRole> listExport(@Param("role") SysRole role);
+
+    List<String> listRoleIdsByAppId(@Param("appId") String appId);
 }

+ 6 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysUserRoleMapper.java

@@ -40,4 +40,10 @@ public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {
 	@Select("select id from sys_role where id in (select role_id from sys_user_role where user_id = (select id from sys_user where username=#{username}))")
 	List<String> getRoleIdByUserName(@Param("username") String username);
 
+	/**
+	 * 通过应用ID查询角色Id集合
+	 * @return List<String>
+	 */
+	List<String> getRoleIdByAppId(@Param("appId") String appId,@Param("userId") String userId);
+
 }

+ 5 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysRoleMapper.xml

@@ -63,4 +63,9 @@
         order by r.create_time desc
     </select>
 
+<!--    查询应用下所有角色ids-->
+    <select id="listRoleIdsByAppId" parameterType="java.lang.String"  resultType="java.lang.String">
+            SELECT id from sys_role
+            WHERE app_id = #{appId}
+    </select>
 </mapper>

+ 24 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysUserRoleMapper.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="org.jeecg.modules.system.mapper.SysUserRoleMapper">
+
+    <!-- 通过应用ID查询角色Id集合 -->
+    <select id="getRoleIdByAppId" resultType="java.lang.String" parameterType="java.lang.String">
+        SELECT
+        id
+        FROM
+        sys_role
+        WHERE 1=1
+        <if test="appId!=null">
+            AND app_id = #{appId}
+        </if>
+        AND id IN (
+        SELECT
+        role_id
+        FROM
+        sys_user_role
+        WHERE
+        user_id = #{userId}
+        )
+    </select>
+</mapper>

+ 5 - 1
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/ISysRoleService.java

@@ -36,7 +36,11 @@ public interface ISysRoleService extends IService<SysRole> {
      * @return
      */
     SysRole getRoleNoTenant(@Param("roleCode") String roleCode, @Param("appId") String appId);
-    
+
+    /*
+     * 查询应用全部角色ids
+     * */
+    public String[] listRoleIdsByAppId(String appId);
     /**
      * 导入 excel ,检查 roleCode 的唯一性
      *

+ 2 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/ISysUserRoleService.java

@@ -1,5 +1,6 @@
 package org.jeecg.modules.system.service;
 
+import java.util.List;
 import java.util.Map;
 
 import org.jeecg.modules.system.entity.SysUserRole;
@@ -15,4 +16,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  * @since 2018-12-21
  */
 public interface ISysUserRoleService extends IService<SysUserRole> {
+    List<String> listByAppId(String appId, String userId);
 }

+ 13 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysRoleServiceImpl.java

@@ -22,6 +22,7 @@ import org.springframework.web.multipart.MultipartFile;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -49,6 +50,18 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
         return sysRoleMapper.getRoleNoTenant(roleCode,appId);
     }
 
+    @Override
+    public String[] listRoleIdsByAppId(String appId){
+        List<String> roleIds = sysRoleMapper.listRoleIdsByAppId(appId);
+        if (roleIds != null) {
+            //从列表中去除admin角色(暂不在此方法去除)
+//            roleIds.removeIf(id -> "admin".equals(id));
+            String[] ids = roleIds.toArray(new String[0]);
+            return ids;
+        }
+        return new String[0];
+    }
+
     @Override
     public Result importExcelCheckRoleCode(MultipartFile file, ImportParams params) throws Exception {
         List<Object> listSysRoles = ExcelImportUtil.importExcel(file.getInputStream(), SysRole.class, params);

+ 6 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysUserRoleServiceImpl.java

@@ -27,4 +27,10 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 @Service
 public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {
 
+    @Autowired
+    private SysUserRoleMapper sysUserRoleMapper;
+    @Override
+    public List<String> listByAppId(String appId, String userId) {
+        return sysUserRoleMapper.getRoleIdByAppId(appId,userId);
+    }
 }

+ 1 - 1
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/vo/SysUserRoleVO.java

@@ -13,7 +13,7 @@ import java.util.List;
 public class SysUserRoleVO implements Serializable{
 	private static final long serialVersionUID = 1L;
 
-	/**部门id*/
+	/**角色id*/
 	private String roleId;
 	/**对应的用户id集合*/
 	private List<String> userIdList;