Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
126 lines
5.3 KiB
Go
126 lines
5.3 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func (s *Server) handlePublicationApprovalAPI(w http.ResponseWriter, r *http.Request) {
|
|
domain := s.domainUUIDForRequest(r)
|
|
principal, err := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
|
|
if err != nil {
|
|
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": "authentication required"})
|
|
return
|
|
}
|
|
if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domain, PrincipalUUID: principal.UUID, Capability: "manage", TargetType: "domain", TargetID: domain}) != nil {
|
|
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "review authority required"})
|
|
return
|
|
}
|
|
path := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/publication/requests"), "/")
|
|
if path == "" {
|
|
if r.Method != http.MethodPost {
|
|
breakPublicationMethod(w)
|
|
return
|
|
}
|
|
s.handleCreatePublicationRequest(w, r, domain, principal.UUID)
|
|
return
|
|
}
|
|
parts := strings.Split(path, "/")
|
|
if len(parts) == 2 && parts[1] == "decisions" && r.Method == http.MethodPost {
|
|
s.handleDecidePublicationRequest(w, r, domain, parts[0], principal.UUID)
|
|
return
|
|
}
|
|
writeEventJSON(w, http.StatusNotFound, map[string]string{"error": "publication request not found"})
|
|
}
|
|
|
|
func breakPublicationMethod(w http.ResponseWriter) {
|
|
w.Header().Set("Allow", "POST")
|
|
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported publication operation"})
|
|
}
|
|
|
|
func (s *Server) handleCreatePublicationRequest(w http.ResponseWriter, r *http.Request, domain, actor string) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, 256<<10)
|
|
var input struct {
|
|
ScopeKind string `json:"scope_kind"`
|
|
ScopeUUID string `json:"scope_uuid"`
|
|
PolicyVersion int64 `json:"policy_version"`
|
|
IdempotencyKey string `json:"idempotency_key"`
|
|
ExpiresAtMS int64 `json:"expires_at_ms"`
|
|
Subject json.RawMessage `json:"subject"`
|
|
ReviewerUUIDs []string `json:"reviewer_uuids"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil || len(input.Subject) == 0 {
|
|
writeEventJSON(w, http.StatusBadRequest, map[string]string{"error": "valid subject is required"})
|
|
return
|
|
}
|
|
hash, subject, err := store.ApprovalSubjectHashJSON(input.Subject)
|
|
if err != nil || strings.TrimSpace(input.IdempotencyKey) == "" {
|
|
writeEventJSON(w, http.StatusBadRequest, map[string]string{"error": "scope and idempotency_key are required"})
|
|
return
|
|
}
|
|
for _, recipient := range input.ReviewerUUIDs {
|
|
recipient = strings.TrimSpace(recipient)
|
|
if recipient == "" || s.store.Authorize(store.AuthorizationRequest{DomainUUID: domain, PrincipalUUID: recipient, Capability: "manage", TargetType: "domain", TargetID: domain}) != nil {
|
|
writeEventJSON(w, http.StatusBadRequest, map[string]string{"error": "reviewer lacks review authority"})
|
|
return
|
|
}
|
|
}
|
|
tx, err := s.store.DB.BeginTx(r.Context(), nil)
|
|
if err != nil {
|
|
writeEventJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
defer tx.Rollback()
|
|
request, inserted, err := s.store.CreateApprovalRequestTx(tx, store.ApprovalRequest{DomainUUID: domain, ScopeKind: input.ScopeKind, ScopeUUID: input.ScopeUUID, SubjectHash: hash, SubjectJSON: subject, PolicyVersion: input.PolicyVersion, RequesterUUID: actor, IdempotencyKey: input.IdempotencyKey, ExpiresAtMS: input.ExpiresAtMS})
|
|
if err != nil {
|
|
writeEventJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
if inserted {
|
|
for _, recipient := range input.ReviewerUUIDs {
|
|
recipient = strings.TrimSpace(recipient)
|
|
if _, _, err := s.store.CreateNotificationTx(tx, store.Notification{DomainUUID: domain, RecipientUUID: recipient, EventUUID: request.UUID, Kind: "approval.request", RequestUUID: request.UUID, Href: "/-/inbox/" + request.UUID, Message: "A publication request needs review."}); err != nil {
|
|
writeEventJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
writeEventJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
status := http.StatusOK
|
|
if inserted {
|
|
status = http.StatusCreated
|
|
}
|
|
writeEventJSON(w, status, map[string]any{"request": request, "created": inserted})
|
|
}
|
|
|
|
func (s *Server) handleDecidePublicationRequest(w http.ResponseWriter, r *http.Request, domain, requestUUID, reviewer string) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, 32<<10)
|
|
var input struct {
|
|
Decision string `json:"decision"`
|
|
Justification string `json:"justification"`
|
|
Generation int64 `json:"generation"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
writeEventJSON(w, http.StatusBadRequest, map[string]string{"error": "valid decision is required"})
|
|
return
|
|
}
|
|
err := s.store.DecideApprovalRequestScoped(r.Context(), domain, requestUUID, reviewer, input.Decision, input.Justification, input.Generation)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
if errors.Is(err, store.ErrApprovalTransition) || errors.Is(err, sql.ErrNoRows) {
|
|
code = http.StatusConflict
|
|
}
|
|
writeEventJSON(w, code, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeEventJSON(w, http.StatusOK, map[string]bool{"decided": true})
|
|
}
|