Kaynağa Gözat

初始化权限组

ximinghao 3 ay önce
ebeveyn
işleme
7d84e08fbd

+ 65 - 12
src/main/java/com/skyversation/xjcy/oauth/AuthService.java

@@ -19,9 +19,7 @@ import org.springframework.util.StringUtils;
 import javax.annotation.PostConstruct;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * OAuth认证服务类
@@ -61,6 +59,14 @@ public class AuthService {
      * Token验证接口路径
      */
     private static final String USER_VALIDATE_TOKEN = "/user/validateToken";
+    /**
+     * 更新用户角色路径
+     */
+    private static final String USER_UPDATE_ROLE = "/user/updateUserRoles";
+    /**
+     * 删除用户路径
+     */
+    private static final String DELETE_USER = "/user/deleteUser";
 
     /**
      * 微信账号用户名前缀
@@ -107,6 +113,10 @@ public class AuthService {
      * 响应字段:用户名
      */
     private static final String RESPONSE_FIELD_USERNAME = "username";
+    /**
+     * 相应字段:用户角色Id
+     */
+    private static final String RESPONSE_FIELD_ROLE_ID = "roleId";
     private final DMSService dMSService;
 
 
@@ -119,13 +129,18 @@ public class AuthService {
     public final static class Account {
         private final String username;
         private final String password;
+        private final Integer serviceId;
 
-        public Account(String username, String password) {
+        public Account(String username, String password, Integer serviceId) {
             if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
                 throw new IllegalArgumentException("缺失必要参数");
             }
             this.username = username;
             this.password = password;
+            this.serviceId = serviceId;
+        }
+        public Account(String username, String password) {
+            this(username, password , null);
         }
     }
 
@@ -313,11 +328,39 @@ public class AuthService {
         return JSON.parseObject(response);
     }
 
+    private boolean updateRole(JSONObject userContent,Collection<String> roleIds,String serviceId){
+        String userId = userContent.getString(RESPONSE_FIELD_ID);
+        String roleId = userContent.getString(RESPONSE_FIELD_ROLE_ID);
+        String[] roleIdArr = roleId.split(",");
+        Set<String> roleIdSet = new HashSet<>(Arrays.asList(roleIdArr));
+        roleIdSet.addAll(roleIds);
+        String newRoleIds = String.join(",", roleIdSet);
+
+        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
+        params.add("userId", userId);
+        params.add("roleIds", newRoleIds);
+        params.add("serviceId", serviceId);
+        Map<String,String> header = new HashMap<>();
+        header.put("token", getTokenOfServiceAccount());
+        String response = HttpUtil.requestPost(oauthPath + USER_UPDATE_ROLE, params, header);
+        return isSuccess(JSONObject.parseObject(response));
+    }
+    private void deleteUser(String userId){
+        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
+        params.add("userId", userId);
+        Map<String,String> header = new HashMap<>();
+        header.put("token", getTokenOfServiceAccount());
+        HttpUtil.requestPost(oauthPath + DELETE_USER, params, header);
+    }
+
+
     /**
      * 初始化用户数据
      */
-    private boolean initUser(JSONObject userId) {
-        String usercode = userId.getJSONObject(RESPONSE_FIELD_CONTENT).getString(RESPONSE_FIELD_USERNAME);
+    private boolean initUser(JSONObject userJSON) {
+        boolean allSuccess = true;
+        JSONObject userContent = userJSON.getJSONObject(RESPONSE_FIELD_CONTENT);
+        String usercode = userContent.getString(RESPONSE_FIELD_USERNAME);
         if (usercode == null || usercode.isEmpty()) {
             return false;
         }
@@ -326,11 +369,12 @@ public class AuthService {
             user.setCUsercode(usercode);
             boolean insertSuccess = dMSService.insertToDms(user.toJSON(), getTokenOfServiceAccount(), DMSColumn.USER);
             if (!insertSuccess) {
-                return false;
+                allSuccess = false;
             }
         }
-
-        return true;
+        allSuccess =allSuccess&&updateRole(userContent, Collections.singletonList("41"),"11");
+        allSuccess =allSuccess&&updateRole(userContent, Collections.singletonList("45"),"12");
+        return allSuccess;
     }
 
     /**
@@ -341,7 +385,6 @@ public class AuthService {
         JSONObject result = login(account);
         String message = result.getString(RESPONSE_FIELD_MESSAGE);
         boolean success = isSuccess(result);
-
         if (success) {
             return result.toJSONString();
         } else {
@@ -381,9 +424,19 @@ public class AuthService {
             return false;
         }
 
-        Integer userId = userUnInitResult.getJSONObject(RESPONSE_FIELD_CONTENT).getInteger(RESPONSE_FIELD_ID);
+        String userId = userUnInitResult.getJSONObject(RESPONSE_FIELD_CONTENT).getString(RESPONSE_FIELD_ID);
         if (userId != null) {
-            return initUser(userUnInitResult);
+            boolean b = false;
+            try {
+                b = initUser(userUnInitResult);
+            } catch (Exception e) {
+                deleteUser(userId);
+                throw new RuntimeException(e);
+            }
+            if (!b){
+                deleteUser(userId);
+            }
+            return b;
         }
         return false;
     }