Files
archie/internal/webui/api_workflow_enablement.go
T
sam 96408c8e6a feat(controlplane): carry the caller org from the request principal
The storage and wire contract already key resources by (org, kind), but no
caller sent an org, so every request acted in the default org. The org now
travels on the request context: webui's authorize middleware attaches the
verified principal's org, and one request-constructor per wire shape
(controlplanerpc's Query/History/Watch/CommandRequest) stamps it back, so a
call site cannot send the wrong org and a new one cannot forget it.

No behaviour change today: the shared token's principal is the default org's
owner, no production path creates another org, and a context with no principal
yields DefaultOrgID (what the server already derived from an empty org_id).
2026-09-30 04:01:52 +10:00

92 lines
3.0 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"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
)
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, controlplanerpc.QueryRequest(ctx, 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(), controlplanerpc.CommandRequest(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})
}