feat(ai): require admin for AI agent mutation/action endpoints
AiAgentController's endpoints relied only on the SecurityFilterChain (prod: /api/** authenticated; dev: permitAll), so any authenticated user could trigger AI writes/actions (auto-develop, local-apply, change-sets, deployment packages) - a lower bar than the comparable overlay/standard-page write endpoints which require admin. Gate the 14 mutating/action POST endpoints with requireAdmin() (UserSession CURRENTUSER == ADMIN), aligned with PageSchemaController.requireAdmin / AdminWriteController. Read GETs and the pure validate-schema/access-check endpoints stay open. Actual three-truth writes are now admin + localhost double-gated. Tests: AiAgentControllerTest + AiAgentApiPipelineTest run under an ADMIN session (+ AfterEach clear); added a 403 test for non-admin. 61 platform-ai tests green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,13 +3,17 @@ package com.eip4.platform.ai.web;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import com.dsasp.server.common.user.service.LoggedInUser;
|
||||
import com.dsasp.server.common.util.UserSession;
|
||||
import com.eip4.platform.ai.model.AiAuditEntry;
|
||||
import com.eip4.platform.ai.model.AiAutoDevelopmentRequest;
|
||||
import com.eip4.platform.ai.model.AiAutoDevelopmentRun;
|
||||
@@ -50,21 +54,42 @@ public class AiAgentController {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* 变更/动作类端点要求管理员,与 {@code PageSchemaController.requireAdmin} /
|
||||
* {@code AdminWriteController.requireAdmin} 对齐:AI 开发工具的变更入口(建任务/草稿/
|
||||
* 变更包/本地落库/部署包/修复执行)会写 sys_ai_* 与触发三真相落库,不能让任意已登录
|
||||
* 用户触发。读类(各 GET)与纯校验/判定端点(validate-schema / access-check)不门控。
|
||||
*
|
||||
* <p>判定口径:{@code UserSession.CURRENTUSER == "ADMIN"}(JWT 过滤器按 token 注入)。
|
||||
* prod 链 /api/** 已 authenticated,本门把 AI 变更入口进一步收敛到管理员,消除「同级写
|
||||
* 端点要 admin、AI 写端点只要登录」的鉴权档位差。
|
||||
*/
|
||||
private static void requireAdmin() {
|
||||
java.util.Properties ctx = UserSession.get();
|
||||
String cu = ctx == null ? "" : ctx.getProperty(LoggedInUser.CURRENTUSER, "").trim();
|
||||
if (!"ADMIN".equalsIgnoreCase(cu)) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "AI 开发工具变更入口仅管理员可用");
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "创建 Agent 开发任务", description = "只创建受控任务和初始计划,不执行线上变更")
|
||||
@PostMapping("/tasks")
|
||||
public AiTask createTask(@RequestBody CreateTaskRequest req) {
|
||||
requireAdmin();
|
||||
return service.createTask(req.objective());
|
||||
}
|
||||
|
||||
@Operation(summary = "自动开发功能草稿", description = "创建任务并生成实体/页面/服务流/菜单/测试草稿,门禁通过后生成待人审变更包")
|
||||
@PostMapping("/features/develop")
|
||||
public AiFeatureDevelopment developFeature(@RequestBody AiFeatureRequest req) {
|
||||
requireAdmin();
|
||||
return service.developFeature(req);
|
||||
}
|
||||
|
||||
@Operation(summary = "一键自动开发功能", description = "生成草稿、变更包、发布计划,执行本地落库和 smoke,并生成线上人工部署包")
|
||||
@PostMapping("/features/auto-develop")
|
||||
public AiAutoDevelopmentRun autoDevelopFeature(@RequestBody(required = false) AiAutoDevelopmentRequest req) {
|
||||
requireAdmin();
|
||||
return service.autoDevelopFeature(req);
|
||||
}
|
||||
|
||||
@@ -77,6 +102,7 @@ public class AiAgentController {
|
||||
@Operation(summary = "保存 Agent 草稿产物", description = "PAGE_SCHEMA 草稿会自动跑 amis schema 校验")
|
||||
@PostMapping("/tasks/{taskId}/drafts")
|
||||
public AiDraft addDraft(@PathVariable String taskId, @RequestBody CreateDraftRequest req) {
|
||||
requireAdmin();
|
||||
return service.addDraft(taskId, req.artifactType(), req.targetId(), req.payload(), req.metadata());
|
||||
}
|
||||
|
||||
@@ -89,12 +115,14 @@ public class AiAgentController {
|
||||
@Operation(summary = "在既有任务下自动开发功能草稿")
|
||||
@PostMapping("/tasks/{taskId}/develop-feature")
|
||||
public AiFeatureDevelopment developFeature(@PathVariable String taskId, @RequestBody AiFeatureRequest req) {
|
||||
requireAdmin();
|
||||
return service.developFeature(taskId, req);
|
||||
}
|
||||
|
||||
@Operation(summary = "生成待人审变更包", description = "任何草稿门禁失败时拒绝生成")
|
||||
@PostMapping("/tasks/{taskId}/change-sets")
|
||||
public AiChangeSet createChangeSet(@PathVariable String taskId) {
|
||||
requireAdmin();
|
||||
return service.createChangeSet(taskId);
|
||||
}
|
||||
|
||||
@@ -107,6 +135,7 @@ public class AiAgentController {
|
||||
@Operation(summary = "生成变更包发布计划", description = "按实体→服务流→页面→菜单→smoke 排序;线上发布仍要求人工批准")
|
||||
@PostMapping("/tasks/{taskId}/change-sets/{changeSetId}/release-plan")
|
||||
public AiReleasePlan releasePlan(@PathVariable String taskId, @PathVariable String changeSetId) {
|
||||
requireAdmin();
|
||||
return service.createReleasePlan(taskId, changeSetId);
|
||||
}
|
||||
|
||||
@@ -115,6 +144,7 @@ public class AiAgentController {
|
||||
public AiSmokeRun runSmoke(@PathVariable String taskId,
|
||||
@PathVariable String changeSetId,
|
||||
@RequestBody(required = false) SmokeRunRequest req) {
|
||||
requireAdmin();
|
||||
return service.runLocalSmoke(taskId, changeSetId, req == null ? null : req.baseUrl());
|
||||
}
|
||||
|
||||
@@ -123,6 +153,7 @@ public class AiAgentController {
|
||||
public AiApplyRun localApply(@PathVariable String taskId,
|
||||
@PathVariable String changeSetId,
|
||||
@RequestBody(required = false) SmokeRunRequest req) {
|
||||
requireAdmin();
|
||||
return service.applyLocalRelease(taskId, changeSetId, req == null ? null : req.baseUrl());
|
||||
}
|
||||
|
||||
@@ -141,12 +172,14 @@ public class AiAgentController {
|
||||
@Operation(summary = "生成线上人工部署包", description = "输出 manifest、SQL 草案、回滚 SQL 和本地执行证据;不连接线上环境执行")
|
||||
@PostMapping("/tasks/{taskId}/change-sets/{changeSetId}/deployment-package")
|
||||
public AiDeploymentPackage deploymentPackage(@PathVariable String taskId, @PathVariable String changeSetId) {
|
||||
requireAdmin();
|
||||
return service.createDeploymentPackage(taskId, changeSetId);
|
||||
}
|
||||
|
||||
@Operation(summary = "生成 AI 功能包快照", description = "封装部署包、manifest、本地 evidence 和回滚 SQL;用于人审、导出和里程碑版本")
|
||||
@PostMapping("/tasks/{taskId}/change-sets/{changeSetId}/feature-package")
|
||||
public AiFeaturePackage featurePackage(@PathVariable String taskId, @PathVariable String changeSetId) {
|
||||
requireAdmin();
|
||||
return service.createFeaturePackage(taskId, changeSetId);
|
||||
}
|
||||
|
||||
@@ -161,6 +194,7 @@ public class AiAgentController {
|
||||
public AiRevision createRevision(@PathVariable String taskId,
|
||||
@PathVariable String packageId,
|
||||
@RequestBody(required = false) CreateRevisionRequest req) {
|
||||
requireAdmin();
|
||||
return service.createRevision(taskId, packageId, req == null ? "" : req.note());
|
||||
}
|
||||
|
||||
@@ -173,6 +207,7 @@ public class AiAgentController {
|
||||
@Operation(summary = "生成自动修复计划", description = "读取最近失败的自动开发/本地应用/smoke 证据,输出结构化修复动作并写入 run history")
|
||||
@PostMapping("/tasks/{taskId}/change-sets/{changeSetId}/repair-plan")
|
||||
public AiRepairPlan repairPlan(@PathVariable String taskId, @PathVariable String changeSetId) {
|
||||
requireAdmin();
|
||||
return service.createRepairPlan(taskId, changeSetId);
|
||||
}
|
||||
|
||||
@@ -181,6 +216,7 @@ public class AiAgentController {
|
||||
public AiRepairExecution executeRepairPlan(@PathVariable String taskId,
|
||||
@PathVariable String changeSetId,
|
||||
@RequestBody(required = false) AiRepairExecutionRequest req) {
|
||||
requireAdmin();
|
||||
return service.executeRepairPlan(taskId, changeSetId, req == null ? null : req.baseUrl());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user