Files
archie/internal/webui/api_workflow_enablement.go
T
sam dc1b61d394 feat(eda): per-org workflow enable/disable gating bindings
A workflow-enablement control-plane resource lists, per org, the workflows
that org has disabled. A binding targeting a disabled workflow is skipped at
dispatch like an unarmed one, is refused on save, and is flagged on the
binding list; the workflows page toggles it. Bindings now carry the org_id
column they already had in storage, over the State Store wire too.

Org-owned workflow definitions are deferred to a follow-up bead: they need
the caller's org, which the access chain resolves.
2026-09-30 02:59:23 +10:00

91 lines
2.9 KiB
Go

package webui
import (
"context"
"encoding/json"
"errors"
"net/http"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
const workflowEnablementKind = "workflow-enablement"
// workflowEnablement returns which workflows each org has disabled, and the
// resource version a replacement must name.
func (s *Server) workflowEnablement(ctx context.Context) (task.WorkflowEnablement, int64, error) {
if s.ControlPlane == nil {
return task.WorkflowEnablement{}, 0, nil
}
response, err := s.ControlPlane.Query(ctx, &controlpb.QueryRequest{Kind: workflowEnablementKind})
if err != nil {
return task.WorkflowEnablement{}, 0, err
}
if response.Resource == nil {
return task.WorkflowEnablement{}, 0, errors.New("workflow enablement missing")
}
var enablement task.WorkflowEnablement
if err := json.Unmarshal(response.Resource.ValueJson, &enablement); err != nil {
return task.WorkflowEnablement{}, 0, err
}
return enablement, response.Resource.Version, nil
}
type workflowEnabledRequest struct {
Enabled bool `json:"enabled"`
}
// handleWorkflowEnabled enables or disables a workflow for the dashboard's
// org. Every dashboard caller acts in the default org until access resolves a
// caller's own.
func (s *Server) handleWorkflowEnabled(w http.ResponseWriter, r *http.Request) {
if !s.authorizeTaskMutation(w, r) {
return
}
if s.ControlPlane == nil {
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
return
}
var request workflowEnabledRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, "invalid request body", http.StatusBadRequest)
return
}
id := r.PathValue("id")
found, err := s.hasWorkflowDefinition(r.Context(), id)
if err != nil {
http.Error(w, "workflow definitions unavailable", http.StatusServiceUnavailable)
return
}
if !found {
http.Error(w, "workflow not found", http.StatusNotFound)
return
}
enablement, version, err := s.workflowEnablement(r.Context())
if err != nil {
http.Error(w, "workflow enablement unavailable", http.StatusServiceUnavailable)
return
}
value, err := json.Marshal(enablement.SetEnabled(org.DefaultOrgID, id, request.Enabled))
if err != nil {
http.Error(w, "encode workflow enablement", http.StatusInternalServerError)
return
}
audit, err := webAudit()
if err != nil {
http.Error(w, "cannot create request ID", http.StatusInternalServerError)
return
}
if _, err := s.ControlPlane.Command(r.Context(), &controlpb.CommandRequest{
Kind: workflowEnablementKind, Command: "replace", ValueJson: value,
ExpectedVersion: version, Actor: string(audit.ActorID),
Source: audit.Source, RequestId: audit.RequestID,
}); err != nil {
writeControlPlaneError(w, err)
return
}
writeJSON(w, map[string]any{"id": id, "enabled": request.Enabled})
}