From 95ae0c83d2aa58d86b4039e3f7f16ee93afb8f00 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期五, 23 十月 2026 17:01:00 +0800
Subject: [PATCH] feat(ai): 完善模型管理和智能体导入功能

---
 yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/framework/agent/SystemAgentBridgeGuard.java |   76 ++++++++++++++++++++++++++++++++++++--
 1 files changed, 72 insertions(+), 4 deletions(-)

diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/framework/agent/SystemAgentBridgeGuard.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/framework/agent/SystemAgentBridgeGuard.java
index f79576d..b50e361 100644
--- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/framework/agent/SystemAgentBridgeGuard.java
+++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/framework/agent/SystemAgentBridgeGuard.java
@@ -2,7 +2,6 @@
 
 import cn.iocoder.yudao.framework.security.core.service.SecurityFrameworkService;
 import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
-import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
 import jakarta.annotation.Resource;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpStatus;
@@ -10,6 +9,7 @@
 
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
+import java.util.List;
 
 /** 妗ユ帴缁熶竴閴存潈锛氭湇鍔′护鐗� + 褰撳墠鐧诲綍鐢ㄦ埛 + 涓氬姟鏉冮檺锛岀己涓�涓嶅彲銆� */
 @Component
@@ -17,11 +17,22 @@
 
     public static final String PERMISSION_ITEM_IMPORT = "mdm:item:import";
 
+    /** 鍚勪笟鍔℃ā鍧楃殑瀵煎叆鏉冮檺锛涗細璇濆垱寤轰笌闄勪欢璇诲彇鍙渶鍛戒腑鍏朵腑浠绘剰涓�椤广�� */
+    public static final List<String> IMPORT_PERMISSIONS = List.of(
+            PERMISSION_ITEM_IMPORT,
+            "srm:supplier:create",
+            "mes:md-item:create",
+            "mes:qc-template:create",
+            "mes:pro-mps:create",
+            "mes:pro-route:create");
+
     @Value("${yudao.agent.bridge-service-token:}")
     private String bridgeServiceToken;
 
     @Resource
     private SecurityFrameworkService securityFrameworkService;
+    @Resource
+    private SystemAgentDelegationService delegationService;
 
     public void verifyServiceToken(String provided) {
         if (bridgeServiceToken == null || bridgeServiceToken.isBlank() || provided == null
@@ -32,6 +43,16 @@
         }
     }
 
+    /** 鍙‘璁ゆ湇鍔¤韩浠戒笌褰撳墠鐧诲綍鐢ㄦ埛锛涘叿浣撹兘鍔涚敱璋冪敤鏂归�愰」鏀剁揣銆� */
+    public AgentPrincipal requireAuthenticated(String providedServiceToken) {
+        verifyServiceToken(providedServiceToken);
+        Long userId = SecurityFrameworkUtils.getLoginUserId();
+        if (userId == null) {
+            throw new AgentBridgeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED", "鏈櫥褰曟垨鐧诲綍宸茶繃鏈�");
+        }
+        return principalOf(userId);
+    }
+
     public AgentPrincipal require(String providedServiceToken, String permission) {
         verifyServiceToken(providedServiceToken);
         Long userId = SecurityFrameworkUtils.getLoginUserId();
@@ -40,9 +61,56 @@
         }
         if (!securityFrameworkService.hasPermission(permission)) {
             throw new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN",
-                    "褰撳墠鐢ㄦ埛缂哄皯鐗╂枡瀵煎叆鏉冮檺锛�" + permission + "锛�");
+                    "褰撳墠鐢ㄦ埛缂哄皯鏁版嵁瀵煎叆鏉冮檺锛�" + permission + "锛�");
         }
-        Long tenantId = TenantContextHolder.getTenantId();
-        return new AgentPrincipal(userId, tenantId == null ? 0L : tenantId, "live");
+        return principalOf(userId);
+    }
+
+    public AgentPrincipal require(String providedServiceToken, String delegationToken, String taskId,
+                                  String operation, String permission, String moduleId, Long attachmentId) {
+        verifyServiceToken(providedServiceToken);
+        if (delegationToken != null && !delegationToken.isBlank()) {
+            return delegationService.require(delegationToken, taskId, operation, permission, moduleId, attachmentId);
+        }
+        return require(providedServiceToken, permission);
+    }
+
+    /** 浼氳瘽涓庨檮浠跺叆鍙f棤娉曢鐭ユā鍧楋紝鍙姹傚叿澶囦换鎰忎竴椤瑰鍏ユ潈闄愩�� */
+    public AgentPrincipal requireAny(String providedServiceToken) {
+        verifyServiceToken(providedServiceToken);
+        Long userId = SecurityFrameworkUtils.getLoginUserId();
+        if (userId == null) {
+            throw new AgentBridgeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED", "鏈櫥褰曟垨鐧诲綍宸茶繃鏈�");
+        }
+        if (!securityFrameworkService.hasAnyPermissions(IMPORT_PERMISSIONS.toArray(String[]::new))) {
+            throw new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN", "褰撳墠鐢ㄦ埛缂哄皯鏁版嵁瀵煎叆鏉冮檺");
+        }
+        return principalOf(userId);
+    }
+
+
+    public AgentPrincipal requireAny(String providedServiceToken, String delegationToken, String taskId,
+                                     String operation, String moduleId, Long attachmentId) {
+        if (delegationToken == null || delegationToken.isBlank()) {
+            return requireAny(providedServiceToken);
+        }
+        verifyServiceToken(providedServiceToken);
+        AgentBridgeException last = null;
+        for (String permission : IMPORT_PERMISSIONS) {
+            try {
+                return delegationService.require(delegationToken, taskId, operation, permission, moduleId, attachmentId);
+            } catch (AgentBridgeException ex) {
+                last = ex;
+                if (!"DELEGATION_SCOPE_MISMATCH".equals(ex.getErrorCode())
+                        && !"DELEGATION_PERMISSION_REVOKED".equals(ex.getErrorCode())) {
+                    throw ex;
+                }
+            }
+        }
+        throw last == null ? new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN", "濮旀墭绁ㄦ嵁鏉冮檺涓嶈冻") : last;
+    }
+
+    private AgentPrincipal principalOf(Long userId) {
+        return new AgentPrincipal(userId, "live");
     }
 }

--
Gitblit v1.9.3