ソースを参照

提取角色配置到环境变量

ximinghao 2 週間 前
コミット
0e9dc1820a

+ 7 - 0
application.yml

@@ -0,0 +1,7 @@
+#
+app:
+  dms:
+    column:
+      data: '{"INDUSTRIAL_PARK":{"id":"1580","modelId":"1525"},"ENTERPRISE":{"id":"1593","modelId":"1537"},"ENTERPRISE_ECONOMIC":{"id":"1594","modelId":"1538"},"ORDER":{"id":"1587","modelId":"1531"},"LEASE_DETAIL":{"id":"1574","modelId":"1520"},"INVESTMENT_TARGET":{"id":"1578","modelId":"1523"},"CLUE":{"id":"1576","modelId":"1521"},"CLUE_FOLLOW":{"id":"1577","modelId":"1522"},"ENTERPRISE_HEALTH":{"id":"1643","modelId":"1643"},"WECHAT_ARTICLE":{"id":"1599","modelId":"1544"},"MESSAGE":{"id":"1646","modelId":"1649"},"USER":{"id":"1579","modelId":"1524"},"MESSAGE_TARGET":{"id":"1661","modelId":"1671"},"LCXM":{"id":"1660","modelId":"1669"},"INSPECTION_TASK":{"id":"1616","modelId":"1576"},"ENTERPRISE_PRODUCT":{"id":"1592","modelId":"1536"},"ENTERPRISE_PURCHASE":{"id":"1591","modelId":"1535"},"INDUSTRIAL_MANAGE":{"id":"1645","modelId":"1647"}}'
+  oauth:
+    role-config: '{"ENTERPRISE_ROLE":[{"roleId":"40","serviceId":"11","comment":"企业用户权限,pc端"},{"roleId":"44","serviceId":"12","comment":"企业用户权限,wx小程序端"}],"INIT_ROLE":[{"roleId":"41","serviceId":"11","comment":"普通游客权限,pc端"},{"roleId":"45","serviceId":"12","comment":"普通游客权限,wx小程序端"},{"roleId":"48","serviceId":"2","comment":"徐泾一般用户,dms"}]}'

+ 7 - 7
src/main/java/com/skyversation/xjcy/controller/LoginController.java

@@ -72,11 +72,11 @@ public class LoginController {
             return MessageManage.getInstance().getResultContent(500, e.getMessage(), "未知错误");
         }
     }
-    @RequestMapping("/test")
-    public String testLogin(@RequestParam(required = false) String uniCode){
-        if (uniCode == null|| uniCode.isEmpty()) {
-            uniCode = "123456";
-        }
-        return authService.logOrRegTestAccount(uniCode);
-    }
+//    @RequestMapping("/test")
+//    public String testLogin(@RequestParam(required = false) String uniCode){
+//        if (uniCode == null|| uniCode.isEmpty()) {
+//            uniCode = "123456";
+//        }
+//        return authService.logOrRegTestAccount(uniCode);
+//    }
 }

+ 77 - 7
src/main/java/com/skyversation/xjcy/service/AuthService.java

@@ -9,6 +9,7 @@ import com.skyversation.xjcy.util.HashBasedLockManager;
 import com.skyversation.xjcy.util.HttpUtil;
 import com.skyversation.xjcy.util.MessageManage;
 import com.skyversation.xjcy.util.ObfuscationUtils;
+import lombok.AllArgsConstructor;
 import lombok.Getter;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -128,6 +129,11 @@ public class AuthService {
      */
     private static final String RESPONSE_FIELD_ROLE_ID = "roleId";
 
+    @Value("${app.oauth.role-config}")
+    private String roleConfigJson;
+
+    private final Map<String, List<Role>> ROLE_CONFIG_MAP = new HashMap<>();
+
     /**
      * yyyy-MM-dd HH:mm:ss
      */
@@ -175,6 +181,13 @@ public class AuthService {
         }
     }
 
+    @Getter
+    @AllArgsConstructor
+    public final static class Role{
+        private final String roleId;
+        private final String serviceId;
+    }
+
     // ============================ 实例变量 ============================
 
     /**
@@ -235,6 +248,29 @@ public class AuthService {
             throw new IllegalStateException("OAuth配置不完整: oauth.path必须配置");
         }
         account = new Account(loginName, password);
+        
+        initRoleConfigMap();
+    }
+
+    /**
+     * 从JSON配置初始化角色映射
+     */
+    private void initRoleConfigMap() {
+        try {
+            JSONObject config = JSON.parseObject(roleConfigJson);
+            for (String key : config.keySet()) {
+                List<JSONObject> roleList = config.getJSONArray(key).toJavaList(JSONObject.class);
+                List<Role> roles = new ArrayList<>();
+                for (JSONObject roleJson : roleList) {
+                    String roleId = roleJson.getString("roleId");
+                    String serviceId = roleJson.getString("serviceId");
+                    roles.add(new Role(roleId, serviceId));
+                }
+                ROLE_CONFIG_MAP.put(key, roles);
+            }
+        } catch (Exception e) {
+            throw new IllegalStateException("角色配置JSON解析失败: " + e.getMessage(), e);
+        }
     }
 
     // ============================ 公共方法 ============================
@@ -350,16 +386,18 @@ public class AuthService {
      * 升级用户为企业用户权限组
      */
     public void upgradeRoleEnterprise(JSONObject userContent) {
-        updateRole(userContent, Collections.singletonList("40"), Collections.emptyList(), "11");
-        updateRole(userContent, Collections.singletonList("44"), Collections.emptyList(), "12");
+        updateRoles(userContent, ROLE_CONFIG_MAP.get("ENTERPRISE_ROLE"), Collections.emptyList());
     }
 
     /**
      * 降级用户使其失去企业用户权限组
      */
     public void deUpgradeRoleEnterprise(JSONObject userContent) {
-        updateRole(userContent, Collections.emptyList(), Collections.singletonList("40"), "11");
-        updateRole(userContent, Collections.emptyList(), Collections.singletonList("44"), "12");
+        List<Role> removeRoles = Arrays.asList(
+            new Role("40", "11"),
+            new Role("44", "12")
+        );
+        updateRoles(userContent, ROLE_CONFIG_MAP.get("ENTERPRISE_ROLE"), removeRoles);
     }
 
     /**
@@ -450,6 +488,39 @@ public class AuthService {
         return isSuccess(JSONObject.parseObject(response));
     }
 
+    private boolean updateRoles(JSONObject userContent, Collection<Role> addRoleIds, Collection<Role> removeRoleIds){
+        Map<String, List<String>> addRolesByService = new HashMap<>();
+        Map<String, List<String>> removeRolesByService = new HashMap<>();
+        
+        if (addRoleIds != null) {
+            for (Role role : addRoleIds) {
+                addRolesByService.computeIfAbsent(role.getServiceId(), k -> new ArrayList<>())
+                        .add(role.getRoleId());
+            }
+        }
+        
+        if (removeRoleIds != null) {
+            for (Role role : removeRoleIds) {
+                removeRolesByService.computeIfAbsent(role.getServiceId(), k -> new ArrayList<>())
+                        .add(role.getRoleId());
+            }
+        }
+        
+        Set<String> allServiceIds = new HashSet<>();
+        allServiceIds.addAll(addRolesByService.keySet());
+        allServiceIds.addAll(removeRolesByService.keySet());
+        
+        boolean allSuccess = true;
+        for (String serviceId : allServiceIds) {
+            List<String> addIds = addRolesByService.getOrDefault(serviceId, Collections.emptyList());
+            List<String> removeIds = removeRolesByService.getOrDefault(serviceId, Collections.emptyList());
+            boolean success = updateRole(userContent, addIds, removeIds, serviceId);
+            allSuccess = allSuccess && success;
+        }
+        
+        return allSuccess;
+    }
+
     private void deleteUser(String userId) {
         MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
         params.add("userId", userId);
@@ -480,9 +551,8 @@ public class AuthService {
                 allSuccess = false;
             }
         }
-        allSuccess = allSuccess && updateRole(userContent, Collections.singletonList("41"), Collections.emptyList(), "11");
-        allSuccess = allSuccess && updateRole(userContent, Collections.singletonList("45"), Collections.emptyList(), "12");
-        allSuccess = allSuccess && updateRole(userContent, Collections.singletonList("48"), Collections.emptyList(), "2");
+
+        allSuccess = allSuccess && updateRoles(userContent, ROLE_CONFIG_MAP.get("INIT_ROLE"), Collections.emptyList());
         return allSuccess;
     }
 

+ 1 - 0
src/main/resources/application.yml

@@ -31,6 +31,7 @@ app:
     login-name: ${DMS_LOGIN_NAME:user_hj}
     password: ${DMS_PASSWORD:Hj@123456}
     path: ${OAUTH_LOGIN_PATH:http://121.43.55.7:10086/oauth}
+    role-config: '{"ENTERPRISE_ROLE":[{"roleId":"40","serviceId":"11","comment":"企业用户权限,pc端"},{"roleId":"44","serviceId":"12","comment":"企业用户权限,wx小程序端"}],"INIT_ROLE":[{"roleId":"41","serviceId":"11","comment":"普通游客权限,pc端"},{"roleId":"45","serviceId":"12","comment":"普通游客权限,wx小程序端"},{"roleId":"48","serviceId":"2","comment":"徐泾一般用户,dms"}]}'
   ows:
     path: ${OWS_PATH:http://121.43.55.7:8889/geoserver/xjxm/ows}
   wechat: