Przeglądaj źródła

使用用户角色列表接口获取用户的角色信息

欧阳劲驰 3 dni temu
rodzic
commit
2a4f197aee

+ 52 - 13
custom-gateway-zydma/src/main/java/com/shkpr/service/customgateway/zydma/components/InfoSynchronizer.java

@@ -17,6 +17,7 @@ import com.shkpr.service.customgateway.zydma.domain.po.PersonnelInfo;
 import com.shkpr.service.customgateway.zydma.service.FunctionInfoService;
 import com.shkpr.service.customgateway.zydma.service.PersonnelInfoService;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
 import org.apache.http.Header;
 import org.apache.http.message.BasicHeader;
 import org.springframework.http.HttpMethod;
@@ -26,6 +27,7 @@ import java.util.*;
 import java.util.function.Consumer;
 import java.util.function.Function;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * 信息同步器
@@ -64,9 +66,8 @@ public class InfoSynchronizer {
     /**
      * 同步用户信息
      *
-     * @param userId 用户ID
      */
-    public void syncUserInfo(Long userId) {
+    public void syncUserInfo() {
         LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
                 , "开始同步用户信息,开始滚动拉取数据");
         long begin = System.currentTimeMillis();
@@ -76,6 +77,8 @@ public class InfoSynchronizer {
 
         //获取用户
         List<MiddlePlatformUser> users = getUsers(endpoint);
+        //获取用户角色
+        List<MiddlePlatformUserRole> userRoles = getUserRoles(endpoint);
 
         //转换用户对象
         List<PersonnelInfo> dates = users.stream()
@@ -83,10 +86,18 @@ public class InfoSynchronizer {
 
         //遍历用户,并设置职能信息
         dates.forEach(personnelInfo -> {
-            //获取启用角色id
-            List<String> ids = getRoles(endpoint, personnelInfo.getUid()).stream()
-                    .filter(MiddlePlatformRole::getIsChecked)
-                    .map(MiddlePlatformRole::getRoleId).collect(Collectors.toList());
+            //获取当前用户角色id集合
+            List<String> ids = userRoles.stream()
+                    //过滤无效用户角色
+                    .filter(userRole -> ObjectUtils.allNotNull(userRole, userRole.getUserId()))
+                    //匹配当前用户
+                    .filter(userRole -> Objects.equals(userRole.getUserId() + "", personnelInfo.getUid()))
+                    //获取角色集合
+                    .map(MiddlePlatformUserRole::getRoles).flatMap(roles -> roles != null ? roles.stream() : Stream.empty())
+                    //获取角色id
+                    .filter(role -> role.getRoleId() != null).mapToLong(MiddlePlatformUserRole.Role::getRoleId)
+                    //去重复,排序,转字符串
+                    .distinct().sorted().mapToObj(String::valueOf).collect(Collectors.toList());
             try {
                 //设置职能信息
                 personnelInfo.setPostId(objectMapper.writeValueAsString(ids));
@@ -97,7 +108,6 @@ public class InfoSynchronizer {
             }
         });
 
-
         //批量写入用户
         Boolean upserted = personnelInfoService.upsertAll(dates);
         long end = System.currentTimeMillis();
@@ -123,7 +133,7 @@ public class InfoSynchronizer {
         CallingProperties.CallingEndpoint endpoint = callingProperties.getEndpoints().get(MiddlePlatformMetadata.NAME);
 
         //获取角色
-        List<MiddlePlatformRole> roles = getRoles(endpoint, null);
+        List<MiddlePlatformRole> roles = getRoles(endpoint);
 
         //转换职能对象
         List<FunctionInfo> dates = roles.stream()
@@ -179,16 +189,15 @@ public class InfoSynchronizer {
      * 获取角色
      *
      * @param endpoint 对接点
-     * @param userId   用户id
      * @return 角色列表
      */
-    public List<MiddlePlatformRole> getRoles(CallingProperties.CallingEndpoint endpoint, String userId) {
+    private List<MiddlePlatformRole> getRoles(CallingProperties.CallingEndpoint endpoint) {
         //结果
         List<MiddlePlatformRole> result = new ArrayList<>();
         //请求地址
-        String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.ROLE_GROUP_LIST;
+        String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.GET_GROUP_ROLES;
         //参数
-        Map<String, String> params = Collections.singletonMap(MiddlePlatformMetadata.Params.USER_ID, (userId != null ? userId : "-1"));
+        Map<String, String> params = Collections.singletonMap(MiddlePlatformMetadata.Params.USER_ID, MiddlePlatformMetadata.DefaultValues.USER_ID);
         //密钥
         IntegrationKey key = MiddlePlatformMetadata.getKey(endpoint.getAccessKey(), endpoint.getSecretKey(), params);
         //请求头
@@ -205,7 +214,7 @@ public class InfoSynchronizer {
             });
         } catch (SelfException e) {
             LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, BIZ_TYPE, CLASS_NAME
-                    , String.format("拉取数据失败 error:%s", e)
+                    , String.format("拉取角色失败 error:%s", e)
             );
         }
 
@@ -233,5 +242,35 @@ public class InfoSynchronizer {
                 )).values());
     }
 
+    /**
+     * 获取用户角色
+     *
+     * @param endpoint 对接点
+     * @return 角色列表
+     */
+    private List<MiddlePlatformUserRole> getUserRoles(CallingProperties.CallingEndpoint endpoint) {
+        //请求地址
+        String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.GET_USER_ROLES;
+        //参数
+        Map<String, String> params = Collections.singletonMap(MiddlePlatformMetadata.Params.USER_ID, MiddlePlatformMetadata.DefaultValues.USER_ID);
+        //密钥
+        IntegrationKey key = MiddlePlatformMetadata.getKey(endpoint.getAccessKey(), endpoint.getSecretKey(), params);
+        //请求头
+        List<Header> headers = Arrays.asList(
+                new BasicHeader(MiddlePlatformMetadata.Headers.APP_KEY, key.getAccessKey()),
+                new BasicHeader(MiddlePlatformMetadata.Headers.TIMESTAMP, key.getTimestamp() + ""),
+                new BasicHeader(MiddlePlatformMetadata.Headers.SIGN, key.getSecretKey())
+        );
 
+        //请求获取用户角色
+        try {
+            return callingUtil.requestObject(url, HttpMethod.GET, params, headers, new TypeReference<MiddlePlatformResult<List<MiddlePlatformUserRole>>>() {
+            });
+        } catch (SelfException e) {
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, BIZ_TYPE, CLASS_NAME
+                    , String.format("拉取用户角色失败 error:%s", e)
+            );
+            return Collections.emptyList();
+        }
+    }
 }

+ 7 - 3
custom-gateway-zydma/src/main/java/com/shkpr/service/customgateway/zydma/constants/MiddlePlatformMetadata.java

@@ -45,8 +45,10 @@ public abstract class MiddlePlatformMetadata extends IntegrationMetadata {
     public interface Uri {
         //获取用户列表
         String GET_USERS = "/PandaCore/GCK/IntegrationAuth/GetUsers";
-        //获取用户角色信息
-        String ROLE_GROUP_LIST = "/PandaOMS/OMS/UserCenter/RoleGroupList";
+        //获取用户角色列表
+        String GET_USER_ROLES = "/PandaOMS/OMS/UserCenter/GetUserAndRoleList";
+        //获取分组角色列表
+        String GET_GROUP_ROLES = "/PandaOMS/OMS/UserCenter/RoleGroupList";
         //根据Ticket获取用户
         String GET_USER_BY_TICKET = "/PandaOMS/OMS/GetUserByTicket";
     }
@@ -80,7 +82,7 @@ public abstract class MiddlePlatformMetadata extends IntegrationMetadata {
     /**
      * 结果
      */
-    public interface Results{
+    public interface Results {
         //id
         String ID = "id";
         //角色列表
@@ -98,5 +100,7 @@ public abstract class MiddlePlatformMetadata extends IntegrationMetadata {
         String ORG = "group";
         //密码
         String PASSWORD = "12345";
+        //用户id
+        String USER_ID = "-1";
     }
 }

+ 49 - 0
custom-gateway-zydma/src/main/java/com/shkpr/service/customgateway/zydma/domain/MiddlePlatformUserRole.java

@@ -0,0 +1,49 @@
+package com.shkpr.service.customgateway.zydma.domain;
+
+import com.fasterxml.jackson.annotation.JsonAlias;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * 中台用户
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Data
+public class MiddlePlatformUserRole {
+    /**
+     * 用户id
+     */
+    private Long userId;
+    /**
+     * 用户名
+     */
+    private String userName;
+    /**
+     * 登陆名
+     */
+    @JsonAlias("userCode")
+    private String loginName;
+    /**
+     * 角色集合
+     */
+    @JsonAlias("conRoleList")
+    private List<Role> roles;
+
+    /**
+     * 角色
+     */
+    @Data
+    public static class Role {
+        /**
+         * 角色id
+         */
+        private Long roleId;
+        /**
+         * 角色名
+         */
+        private String roleName;
+    }
+}

+ 2 - 2
custom-gateway-zydma/src/main/java/com/shkpr/service/customgateway/zydma/manager/InfoSyncManager.java

@@ -31,7 +31,7 @@ public class InfoSyncManager {
     @PostConstruct
     public void init() {
         //同步用户信息
-        taskScheduler.execute(() -> infoSynchronizer.syncUserInfo(null));
+        taskScheduler.execute(infoSynchronizer::syncUserInfo);
         taskScheduler.execute(infoSynchronizer::syncFunctionInfo);
     }
 
@@ -41,7 +41,7 @@ public class InfoSyncManager {
     @Scheduled(cron = "0 */10 * * * *")
     public void minuteTask() {
         //同步用户信息
-        taskScheduler.execute(() -> infoSynchronizer.syncUserInfo(null));
+        taskScheduler.execute(infoSynchronizer::syncUserInfo);
         taskScheduler.execute(infoSynchronizer::syncFunctionInfo);
     }
 }