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.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 } principal, ok := access.PrincipalFromContext(r.Context()) if !ok { return nil } action, kind, id := accessRequest(&http.Request{Method: route.method, URL: &url.URL{Path: route.api}}) resource, err := s.recordResource(r.Context(), kind, id) if err != nil || !s.Access.Authorize(principal, action, resource, access.Context{}).Allowed { return nil } return &taskRecoveryView{Blocker: t.ParkBlocker, Label: route.label, Link: route.page} }