Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(httpapi): extract workspaceEditAllowed from duplicated workspace edit checks (#334)

fx-kurumi8d1569f3b48a
diff --git a/internal/httpapi/run_workspace_edit.go b/internal/httpapi/run_workspace_edit.go
index 6c0e66a7..fb3e0e95 100644
--- a/internal/httpapi/run_workspace_edit.go
+++ b/internal/httpapi/run_workspace_edit.go
@@ -29,14 +29,7 @@ func (h *runsHandlers) workspaceEditActor(c *gin.Context) (domain.Run, int64, bo
// Conversation tools inherit project access from their authenticated entry
// point; the separate task-panel editor policy does not apply to them.
if mainBotWorkspaceRun(c.Request.Context()) != run.ID {
- project, ok := h.repo.Project(run.ExecutionContext.ProjectID)
- allowed := h.isPlatformAdmin(c.Request) || ok && (project.OwnerID == user.ID || project.OwnerID == 0 && len(project.Members) == 0)
- for _, member := range project.Members {
- if member.UserID == user.ID && !strings.EqualFold(member.Role, "viewer") {
- allowed = true
- }
- }
- if !allowed {
+ if !h.workspaceEditAllowed(c.Request, run, user.ID) {
c.JSON(403, gin.H{"error": "无权编辑该任务工作区"})
return run, 0, false
}
@@ -134,6 +127,28 @@ func (h *runsHandlers) workspaceWrite(c *gin.Context) {
c.JSON(200, gin.H{"sha256": sha})
}
+// workspaceEditAllowed reports whether userID may edit run's task workspace
+// from the task panel: platform admins, the project owner, anyone on an
+// unowned project without members, and non-viewer project members.
+func (h *runsHandlers) workspaceEditAllowed(r *http.Request, run domain.Run, userID int64) bool {
+ if h.isPlatformAdmin(r) {
+ return true
+ }
+ project, ok := h.repo.Project(run.ExecutionContext.ProjectID)
+ if !ok {
+ return false
+ }
+ if project.OwnerID == userID || project.OwnerID == 0 && len(project.Members) == 0 {
+ return true
+ }
+ for _, member := range project.Members {
+ if member.UserID == userID && !strings.EqualFold(member.Role, "viewer") {
+ return true
+ }
+ }
+ return false
+}
+
func (h *runsHandlers) workspaceWritable(c *gin.Context, run domain.Run) bool {
// Read-only probe: never write HTTP errors here (browse handlers still return the payload).
if engine.WorkspaceRunID(run) != run.ID {
@@ -143,13 +158,7 @@ func (h *runsHandlers) workspaceWritable(c *gin.Context, run domain.Run) bool {
if !ok {
return false
}
- project, pok := h.repo.Project(run.ExecutionContext.ProjectID)
- allowed := mainBotWorkspaceRun(c.Request.Context()) == run.ID || h.isPlatformAdmin(c.Request) || pok && (project.OwnerID == user.ID || project.OwnerID == 0 && len(project.Members) == 0)
- for _, member := range project.Members {
- if member.UserID == user.ID && !strings.EqualFold(member.Role, "viewer") {
- allowed = true
- }
- }
+ allowed := mainBotWorkspaceRun(c.Request.Context()) == run.ID || h.workspaceEditAllowed(c.Request, run, user.ID)
if mainBotWorkspaceRun(c.Request.Context()) == run.ID {
return run.LiveWorkspace() || (run.Status == "waiting" && run.PauseRequested && !run.CancelRequested) || domain.TerminalRunStatus(run.Status)
}
diff --git a/internal/httpapi/run_workspace_edit_access_test.go b/internal/httpapi/run_workspace_edit_access_test.go
new file mode 100644
index 00000000..e60a4820
--- /dev/null
+++ b/internal/httpapi/run_workspace_edit_access_test.go
@@ -0,0 +1,55 @@
+package httpapi
+
+import (
+ "agentflow/internal/domain"
+ "bytes"
+ "net/http"
+ "testing"
+)
+
+func TestWorkspaceEditAllowedCoversProjectRoles(t *testing.T) {
+ h, owner := workbenchFixture(t)
+ users := map[string]domain.User{"owner": owner}
+ for _, name := range []string{"editor", "viewer", "outsider", "admin"} {
+ if err := h.repo.PutUser(domain.User{LoginName: "ws-" + name}); err != nil {
+ t.Fatal(err)
+ }
+ users[name], _ = h.repo.UserByLoginName("ws-" + name)
+ }
+ if _, err := h.repo.GrantPlatformAdmin(users["admin"].LoginName); err != nil {
+ t.Fatal(err)
+ }
+ project, _ := h.repo.Project("workbench")
+ project.Members = []domain.ProjectMember{{UserID: users["editor"].ID, Role: "member"}, {UserID: users["viewer"].ID, Role: "Viewer"}}
+ if err := h.repo.PutProject(project); err != nil {
+ t.Fatal(err)
+ }
+ if err := h.repo.PutProject(domain.Project{ID: "unowned", Name: "Unowned"}); err != nil {
+ t.Fatal(err)
+ }
+ runs := &runsHandlers{deps: h.deps}
+ runIn := func(projectID string) domain.Run {
+ return domain.Run{ID: "run-" + projectID, ExecutionContext: domain.TelemetryContext{ProjectID: projectID}}
+ }
+ cases := []struct {
+ name, user, project string
+ want bool
+ }{
+ {"owner", "owner", "workbench", true},
+ {"editor member", "editor", "workbench", true},
+ {"viewer member is case-insensitive", "viewer", "workbench", false},
+ {"outsider", "outsider", "workbench", false},
+ {"platform admin", "admin", "workbench", true},
+ {"unowned project without members", "outsider", "unowned", true},
+ {"missing project", "outsider", "missing", false},
+ {"platform admin on missing project", "admin", "missing", true},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ c, _ := workbenchContext(users[tc.user], http.MethodGet, "/", bytes.NewBuffer(nil))
+ if got := runs.workspaceEditAllowed(c.Request, runIn(tc.project), users[tc.user].ID); got != tc.want {
+ t.Fatalf("workspaceEditAllowed = %v, want %v", got, tc.want)
+ }
+ })
+ }
+}