package webui import ( "net/http" "net/url" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/taskstate" ) // taskRecoveryView is the fix for a parked task's blocker: the dashboard page // that supplies what the task is missing. type taskRecoveryView struct { Blocker taskstate.Blocker `json:"blocker"` Label string `json:"label"` Link string `json:"link"` } // recoveryRoute is where a blocker is fixed: the page, and the API write that // page makes, which decides whether the viewer may act there. type recoveryRoute struct { label, page, method, api string instance bool } func blockerRoute(t *task.Task) (recoveryRoute, bool) { workflowPage := "/workflows?workflow=" + url.QueryEscape(t.Workflow) switch t.ParkBlocker { case taskstate.BlockerWorkflowDisabled: return recoveryRoute{label: "Enable workflow", page: workflowPage, method: http.MethodPut, api: "/api/workflows/" + url.PathEscape(t.Workflow) + "/enabled"}, true case taskstate.BlockerWorkflowIdentity: return recoveryRoute{label: "Set execution identity", page: workflowPage, method: http.MethodPost, api: "/api/control-plane/resources/workflow-definitions/commands/apply"}, true case taskstate.BlockerAgentAssignment: return recoveryRoute{label: "Assign agent", page: "/org/agents", method: http.MethodPut, api: "/api/orgs/" + url.PathEscape(t.OrgOrDefault()) + "/agents/_", instance: true}, true case taskstate.BlockerConnectionPermit: return recoveryRoute{label: "Edit connection permissions", page: "/org/connections", method: http.MethodPost, api: "/api/control-plane/resources/forge-connections/commands/replace"}, true case taskstate.BlockerRepository: return recoveryRoute{label: "Set up repository", page: "/org/repositories", method: http.MethodPost, api: "/api/control-plane/resources/repository-policies/commands/replace"}, true case taskstate.BlockerConnectionCredential: return recoveryRoute{label: "Fix connection credential", page: "/org/connections", method: http.MethodPost, api: "/api/control-plane/resources/forge-connections/commands/replace"}, true case taskstate.BlockerForgeExtension: return recoveryRoute{label: "Enable forge extension", page: "/settings/extensions", method: http.MethodPut, api: "/api/extensions/_/enabled", instance: true}, true case taskstate.BlockerModelCredential: return recoveryRoute{label: "Set up model", page: "/org/models", method: http.MethodPost, api: "/api/control-plane/resources/provider-settings/commands/replace"}, true case taskstate.BlockerPolicyDenied: return recoveryRoute{label: "Edit policies", page: "/org/policies", method: http.MethodPut, api: "/api/access/policies"}, true } return recoveryRoute{}, false } // taskRecovery returns the recovery link for a parked task, or nil when the // park names no blocker or the viewer may not make the fixing write. func (s *Server) taskRecovery(r *http.Request, t *task.Task) *taskRecoveryView { if t.Status != task.StatusParked || s.Access == nil { return nil } route, ok := blockerRoute(t) if !ok || (route.instance && !instanceViewer(r)) { return nil } if !s.mayCall(r, route.method, route.api) { return nil } return &taskRecoveryView{Blocker: t.ParkBlocker, Label: route.label, Link: route.page} } // mayCall reports whether the request's principal may make the API call, the // same decision the access middleware takes for it. func (s *Server) mayCall(r *http.Request, method, path string) bool { principal, ok := access.PrincipalFromContext(r.Context()) if !ok || s.Access == nil { return false } action, kind, id := accessRequest(&http.Request{Method: method, URL: &url.URL{Path: path}}) resource, err := s.recordResource(r.Context(), kind, id) return err == nil && s.Access.Authorize(principal, action, resource, access.Context{}).Allowed }