Postgres-RLS-Policies auf allen Tabellen mit echten Mandanten- Geschäftsdaten (antrag und alles darüber verkettete, abteilung, werkzeug/werkzeug_sperre, genehmiger_rolle/freigabe_regel, loeschfrist_einstellung). Kritischer Fund vor der Umsetzung: die Anwendung verbindet als postgres-Superuser, der RLS immer umgeht - Migration 0021 legt deshalb zusätzlich eine eingeschränkte Rolle "deklarix_app" an, nur für die greifen die Policies tatsächlich. internal/store/tenant_scope.go: WithTenantScope öffnet eine Transaktion und setzt Sitzungsvariablen (app.account_id/app.is_betreiber) per set_config mit Parameterbindung; alle Store-Methoden laufen jetzt über s.db(ctx) statt direkt s.Pool. Jede require*-Middleware umschließt die komplette Handler-Ausführung damit - jeder Request läuft dadurch auch atomar in einer Transaktion (positiver Nebeneffekt). Live end-to-end verifiziert (echter HTTP-Server + DATABASE_URL_APP auf die eingeschränkte Rolle gesetzt): zwei Firmen registriert, Isolation über Abteilung/Antrag/Bewertung bestätigt, zentraler NULL-Katalog für beide sichtbar. Produktivbetrieb braucht noch einen manuellen Schritt (Passwort für deklarix_app setzen + DATABASE_URL_APP konfigurieren, siehe CLAUDE.md) - die Migration allein aktiviert noch nichts, solange die App weiter als Superuser verbindet. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// Abteilung ist Stammdatum für den Fragebogen (Feld A.abteilung).
|
|
type Abteilung struct {
|
|
ID string
|
|
AccountID string
|
|
Name string
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// CreateAbteilung legt eine Abteilung für einen Mandanten an.
|
|
func (s *Store) CreateAbteilung(ctx context.Context, accountID, name string) (Abteilung, error) {
|
|
var a Abteilung
|
|
err := s.db(ctx).QueryRow(ctx, `
|
|
INSERT INTO abteilung (account_id, name) VALUES ($1, $2)
|
|
RETURNING id, account_id, name, created_at
|
|
`, accountID, name).Scan(&a.ID, &a.AccountID, &a.Name, &a.CreatedAt)
|
|
if err != nil {
|
|
return Abteilung{}, fmt.Errorf("store: create abteilung: %w", err)
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// ListAbteilungenForAccount liefert alle Abteilungen eines Mandanten,
|
|
// alphabetisch — als Auswahlliste für den Fragebogen.
|
|
func (s *Store) ListAbteilungenForAccount(ctx context.Context, accountID string) ([]Abteilung, error) {
|
|
rows, err := s.db(ctx).Query(ctx, `
|
|
SELECT id, account_id, name, created_at FROM abteilung
|
|
WHERE account_id = $1 ORDER BY name
|
|
`, accountID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list abteilungen: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []Abteilung
|
|
for rows.Next() {
|
|
var a Abteilung
|
|
if err := rows.Scan(&a.ID, &a.AccountID, &a.Name, &a.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("store: scan abteilung: %w", err)
|
|
}
|
|
out = append(out, a)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("store: list abteilungen: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// GetAbteilung liest eine Abteilung anhand ihrer ID.
|
|
func (s *Store) GetAbteilung(ctx context.Context, id string) (Abteilung, error) {
|
|
var a Abteilung
|
|
err := s.db(ctx).QueryRow(ctx, `
|
|
SELECT id, account_id, name, created_at FROM abteilung WHERE id = $1
|
|
`, id).Scan(&a.ID, &a.AccountID, &a.Name, &a.CreatedAt)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return Abteilung{}, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return Abteilung{}, fmt.Errorf("store: get abteilung: %w", err)
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// DeleteAbteilung entfernt eine Abteilung (z. B. versehentlich doppelt
|
|
// angelegt).
|
|
func (s *Store) DeleteAbteilung(ctx context.Context, id string) error {
|
|
tag, err := s.db(ctx).Exec(ctx, `DELETE FROM abteilung WHERE id = $1`, id)
|
|
if err != nil {
|
|
return fmt.Errorf("store: delete abteilung: %w", err)
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|