droboticflow/internal/api/project_deletion_guard.go

82 lines
2.0 KiB
Go

package api
import (
"net/http"
"strings"
)
// Resolve from the path rather than chi params: this middleware runs before routing.
func (s *server) projectDeletionWriteGuard(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" || r.Method == "HEAD" || r.Method == "OPTIONS" {
next.ServeHTTP(w, r)
return
}
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
projectID := ""
if len(parts) >= 3 && parts[0] == "api" {
switch parts[1] {
case "projects":
if r.Method == "DELETE" && len(parts) == 3 {
next.ServeHTTP(w, r)
return
}
projectID = parts[2]
case "workflows":
item, e := s.store.GetWorkflow(r.Context(), parts[2])
if e != nil {
renderError(w, 500, e)
return
}
if item != nil {
projectID = item.ProjectID
}
case "runs":
item, e := s.store.GetRun(r.Context(), parts[2])
if e != nil {
renderError(w, 500, e)
return
}
if item != nil {
projectID = item.ProjectID
}
case "human-items":
item, e := s.store.GetHumanItem(r.Context(), parts[2])
if e != nil {
renderError(w, 500, e)
return
}
if item != nil {
projectID = item.ProjectID
}
case "plugins":
if len(parts) >= 5 && parts[2] == "annotation-qa" && parts[3] == "tasks" {
item, e := s.store.GetAnnotationTask(r.Context(), parts[4])
if e != nil {
renderError(w, 500, e)
return
}
if item != nil {
projectID = item.ProjectID
}
}
}
}
if projectID != "" {
p, e := s.store.GetProject(r.Context(), projectID)
if e != nil {
renderError(w, 500, e)
return
}
if p != nil && p.DeletionTaskID != "" {
if !s.ensureTenantAccess(w, r, p.OrganizationID, "Project") {
return
}
renderJSON(w, 409, map[string]string{"error": "项目正在删除或删除未完成,已暂停写入。请在删除记录中查看或重试。", "deletionTaskId": p.DeletionTaskID})
return
}
}
next.ServeHTTP(w, r)
})
}