| | |
| | | |
| | | private static final Set<String> ACTIONS = Set.of("material_import.prepare", "material_import.read", |
| | | "material_import.review", "material_import.preflight", "material_import.commit", |
| | | "material_import.read_receipt"); |
| | | "material_import.read_receipt", |
| | | "conversation.create", "conversation.read", "conversation.message", |
| | | "conversation.action", "conversation.report"); |
| | | |
| | | /** module_id → 业务导入权限;Python 以模块 ID 作为授权 resource。 */ |
| | | private static final Map<String, String> MODULE_PERMISSIONS = Map.of( |
| | | "mdm.item", SystemAgentBridgeGuard.PERMISSION_ITEM_IMPORT, |
| | | "srm.supplier", "srm:supplier:create", |
| | | "mes.bom", "mes:md-item:create", |
| | | "mes.qc-template", "mes:qc-template:create", |
| | | "mes.mps", "mes:pro-mps:create", |
| | | "mes.route", "mes:pro-route:create"); |
| | | |
| | | @Resource |
| | | private SystemAgentImportBridgeService bridgeService; |
| | | @Resource |
| | | private SystemAgentBridgeGuard bridgeGuard; |
| | | |
| | | private static final String IMPORT_PERMISSIONS_EXPR = |
| | | "@ss.hasAnyPermissions('mdm:item:import','srm:supplier:create','mes:md-item:create'," |
| | | + "'mes:qc-template:create','mes:pro-mps:create','mes:pro-route:create')"; |
| | | |
| | | @PostMapping("/import-sessions") |
| | | @PreAuthorize("@ss.hasPermission('mdm:item:import')") |
| | | @PreAuthorize(IMPORT_PERMISSIONS_EXPR) |
| | | public CommonResult<Long> createSession() { |
| | | return success(bridgeService.createSession()); |
| | | } |
| | | |
| | | @PostMapping("/import-sessions/{sessionId}/attachments") |
| | | @PreAuthorize("@ss.hasPermission('mdm:item:import')") |
| | | @PreAuthorize(IMPORT_PERMISSIONS_EXPR) |
| | | public CommonResult<Long> bindAttachment(@PathVariable Long sessionId, @Valid @RequestBody BindRequest request) { |
| | | return success(bridgeService.bindAttachment(sessionId, request.getBlobId())); |
| | | } |
| | | |
| | | @PostMapping("/authorize") |
| | | @PreAuthorize("@ss.hasPermission('mdm:item:import')") |
| | | @PreAuthorize(IMPORT_PERMISSIONS_EXPR) |
| | | public CommonResult<Map<String, String>> authorize( |
| | | @RequestHeader(value = "X-Agent-Service-Token", required = false) String serviceToken, |
| | | @Valid @RequestBody AuthorizeRequest request) { |
| | | AgentPrincipal principal = bridgeGuard.require(serviceToken, SystemAgentBridgeGuard.PERMISSION_ITEM_IMPORT); |
| | | if (!ACTIONS.contains(request.getAction())) { |
| | | throw new AgentBridgeException(HttpStatus.FORBIDDEN, "ACTION_FORBIDDEN", "不支持的智能体操作"); |
| | | } |
| | | AgentPrincipal principal; |
| | | if (request.getAction().startsWith("conversation.")) { |
| | | String permission = MODULE_PERMISSIONS.get(request.getResource()); |
| | | principal = permission == null |
| | | ? bridgeGuard.requireAny(serviceToken) |
| | | : bridgeGuard.require(serviceToken, permission); |
| | | } else { |
| | | principal = bridgeGuard.require(serviceToken, SystemAgentBridgeGuard.PERMISSION_ITEM_IMPORT); |
| | | } |
| | | if (requiresAttachmentOwnership(request.getAction())) { |
| | | bridgeService.requireOwnedAttachmentRef(parseId(request.getResource())); |
| | |
| | | } |
| | | |
| | | @GetMapping("/attachments/{attachmentId}") |
| | | @PreAuthorize("@ss.hasPermission('mdm:item:import')") |
| | | @PreAuthorize(IMPORT_PERMISSIONS_EXPR) |
| | | public ResponseEntity<FileSystemResource> downloadAttachment( |
| | | @RequestHeader(value = "X-Agent-Service-Token", required = false) String serviceToken, |
| | | @PathVariable Long attachmentId) { |
| | | bridgeGuard.require(serviceToken, SystemAgentBridgeGuard.PERMISSION_ITEM_IMPORT); |
| | | bridgeGuard.requireAny(serviceToken); |
| | | SystemAgentAttachmentRef ref = bridgeService.requireOwnedAttachmentRef(attachmentId); |
| | | return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM) |
| | | .contentLength(ref.byteSize()) |