Files
nexarch/internal/tenantimportdirapi/tenantimportdirapi.go
patrickandClaude Sonnet 5.5 cd5ca8da53
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (pull_request) Canceled after 0s
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
Archive-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
Core-Schnittstellen-Vertragstests / contract-tests (pull_request) Canceled after 0s
Supply-Chain-Scan (npm) / npm-audit (pull_request) Canceled after 0s
Core-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (pull_request) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (pull_request) Canceled after 0s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (pull_request) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Canceled after 0s
CI / Web (dms-browser) (pull_request) Canceled after 0s
CI / Web (pdf-viewer) (pull_request) Canceled after 0s
CI / Web (shl) (pull_request) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (push) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (pull_request) Canceled after 0s
Mail-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
RBAC-Rollenhierarchie-Pflichtgate / rbac-policy-invariant (pull_request) Canceled after 0s
refactor: geteiltes Modul common/ (Pilot httpclient, apierror)
- common/httpclient: bisher byte-identisch in dms, mail und archive
- common/apierror: bisher byte-identisch in Root und archive
- Importpfade, go.mod (require + replace), archtest-Ausnahmen und
  CI-Pfadfilter der mail-/archive-Pflichttest-Gates angepasst
- Waechtertest fuer den Standard-Client prueft weiter dms, mail, archive
- dms/internal/apierror bleibt eigenstaendig (5xx-Fix in eigenem PR)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 13:52:24 +01:00

132 lines
4.4 KiB
Go

// Package tenantimportdirapi bindet TEN-12 (internal/tenantimportdir) als
// HTTP-Schnittstelle ein: Verwaltung (Add/Remove, nur Superadmin, ueber die
// Tenant-Admin-API) und Abfrage (List/Resolve, fuer Module mit dem
// bestehenden Core-Dienst-Token aus TEN-01/cmd/core) getrennt, wie im
// Ticket "Technische Grundlage" gefordert.
package tenantimportdirapi
import (
"encoding/json"
"errors"
"net/http"
"gitea.perlbach24.de/scripte/nexarch/common/apierror"
"gitea.perlbach24.de/scripte/nexarch/internal/tenantimportdir"
)
type Handler struct {
store *tenantimportdir.Store
}
func NewHandler(store *tenantimportdir.Store) *Handler {
return &Handler{store: store}
}
func writeErr(w http.ResponseWriter, err error) {
switch {
case errors.Is(err, tenantimportdir.ErrNotSuperadmin):
apierror.Write(w, http.StatusForbidden, apierror.CodeForStatus(http.StatusForbidden), err.Error())
case errors.Is(err, tenantimportdir.ErrTenantNotFound), errors.Is(err, tenantimportdir.ErrNotFound):
apierror.Write(w, http.StatusNotFound, apierror.CodeForStatus(http.StatusNotFound), err.Error())
case errors.Is(err, tenantimportdir.ErrPathTaken), errors.Is(err, tenantimportdir.ErrNestedPath),
errors.Is(err, tenantimportdir.ErrInvalidPath), errors.Is(err, tenantimportdir.ErrInvalidPurpose):
apierror.Write(w, http.StatusConflict, apierror.CodeForStatus(http.StatusConflict), err.Error())
default:
apierror.Write(w, http.StatusInternalServerError, apierror.CodeForStatus(http.StatusInternalServerError), err.Error())
}
}
func writeJSON(w http.ResponseWriter, status int, body any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(body)
}
// --- Verwaltung (Tenant-Admin-API, Superadmin per Query-/Body-Parameter,
// gleiches Muster wie internal/tenantadmin: Session-/Token-Authentifizierung
// ist IAM-01/IAM-02 und wird hier nicht dupliziert; svcauth schuetzt den
// Transport, requireSuperadmin in tenantimportdir.Store die Aktion.) ---
type addRequest struct {
Superadmin string `json:"superadmin"`
Path string `json:"path"`
Purpose string `json:"purpose"`
}
// AddHandler bindet POST /tenants/{slug}/import-directories.
func (h *Handler) AddHandler(w http.ResponseWriter, r *http.Request) {
slug := r.PathValue("slug")
var req addRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
apierror.Write(w, http.StatusBadRequest, apierror.CodeForStatus(http.StatusBadRequest), "ungueltiger anfrage-koerper")
return
}
d, err := h.store.Add(r.Context(), req.Superadmin, slug, req.Path, tenantimportdir.Purpose(req.Purpose))
if err != nil {
writeErr(w, err)
return
}
writeJSON(w, http.StatusCreated, directoryJSON(d))
}
// RemoveHandler bindet DELETE /tenants/{slug}/import-directories?path=...&superadmin=....
func (h *Handler) RemoveHandler(w http.ResponseWriter, r *http.Request) {
slug := r.PathValue("slug")
q := r.URL.Query()
if err := h.store.Remove(r.Context(), q.Get("superadmin"), slug, q.Get("path")); err != nil {
writeErr(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// --- Abfrage (Module mit Core-Dienst-Token) ---
// ListForTenantHandler bindet GET /tenants/{slug}/import-directories.
func (h *Handler) ListForTenantHandler(w http.ResponseWriter, r *http.Request) {
slug := r.PathValue("slug")
list, err := h.store.ListForTenant(r.Context(), slug)
if err != nil {
writeErr(w, err)
return
}
out := make([]directoryDTO, 0, len(list))
for _, d := range list {
out = append(out, directoryJSON(d))
}
writeJSON(w, http.StatusOK, out)
}
// ResolveHandler bindet GET /import-directories/resolve?path=....
func (h *Handler) ResolveHandler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
if path == "" {
apierror.Write(w, http.StatusBadRequest, apierror.CodeForStatus(http.StatusBadRequest), "path ist pflicht")
return
}
d, err := h.store.Resolve(r.Context(), path)
if err != nil {
writeErr(w, err)
return
}
writeJSON(w, http.StatusOK, directoryJSON(d))
}
type directoryDTO struct {
ID string `json:"id"`
TenantSlug string `json:"tenant_slug"`
Path string `json:"path"`
Purpose string `json:"purpose"`
CreatedAt string `json:"created_at"`
}
func directoryJSON(d tenantimportdir.Directory) directoryDTO {
return directoryDTO{
ID: d.ID,
TenantSlug: d.TenantSlug,
Path: d.Path,
Purpose: string(d.Purpose),
CreatedAt: d.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
}
}