Files
archie/internal/webui/api_workflow_enablement.go
T
sam e8b765c369 fix(webui): the workflow handlers act in the caller's org
The definitions query was built by hand, so it carried no org and the server
resolved the default one, while the enablement document read beside it and the
enablement write went to the default org outright. A caller in a non-default
org therefore read one org's definitions beside another org's enablement, and
toggled that other org's workflows.

workflowCollection now builds its query with controlplanerpc.QueryRequest, and
the three enablement reads and the write use org.OrgFromContext, so every one of
them names the org the principal put on the request context.
2026-09-30 07:33:16 +10:00

91 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 caller's org
// (org.OrgFromContext).
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.OrgFromContext(r.Context()), 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})
}