Bislang gab es keine vom Nutzer-Rollenmodell (creator/agentur/marke/
kanzlei) getrennte Betreiber-Rolle — jede Verwaltungsaufgabe (welche
Kanzlei darf im öffentlichen Verzeichnis stehen, wer sind unsere
Accounts) wäre nur per Hand in der Datenbank möglich gewesen. Admin
ist von Anfang an als fünfte app_user-Rolle im Datenmodell verankert,
nicht nachträglich aufgesetzt.
Migration 0004:
- app_user.role erlaubt zusätzlich 'admin' (kein Self-Service-Weg
dorthin — /register bietet die Rolle nicht an, erster Admin wird
einmalig per SQL angelegt, siehe CLAUDE.md).
- account.verified: Freigabe fürs kostenlose Kanzlei-Verzeichnis
(§ 49b Abs. 3 BRAO: reine Auflistung, kein Routing/keine Vermittlung).
- audit_log: append-only-Protokoll jeder Admin-Aktion (gleicher Trigger
wie finding/extraction/evidence_package).
Neue Routen:
- GET /admin, /admin/accounts, /admin/accounts/{id}: Accounts-Übersicht
und -Detail (Logins je Account), requireAdmin (404 statt 403 für
angemeldete Nicht-Admins, wie beim bestehenden Mandanten-404-Muster).
- POST /admin/accounts/{id}/verifizieren: Kanzlei-Freigabe umschalten,
schreibt einen Audit-Log-Eintrag.
- GET /admin/audit-log: Protokoll ansehen.
- GET /kanzleien: öffentliches Verzeichnis (kein Login), zeigt nur
Accounts, die sowohl verified sind als auch einen Nutzer der Rolle
"kanzlei" haben.
Volle Testsuite inkl. echter Postgres-Tests grün; End-to-End manuell
gegen einen laufenden Server verifiziert (Admin-Login, Verify-Toggle,
Erscheinen im öffentlichen Verzeichnis, Audit-Log-Eintrag, 404 für
Nicht-Admin-Zugriff).
123 lines
3.7 KiB
Go
123 lines
3.7 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// Account ist ein Mandant (Creator, Agentur, Marke oder Kanzlei als
|
|
// eigene Organisation). Jeder Beitrag gehört genau einem Account.
|
|
// Verified ist nur für Kanzlei-Accounts relevant: ob sie im öffentlichen
|
|
// Kanzlei-Verzeichnis gelistet werden (siehe Migration 0004).
|
|
type Account struct {
|
|
ID string
|
|
Name string
|
|
Verified bool
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// CreateAccount legt einen neuen Mandanten an (verified startet false —
|
|
// jede Freigabe fürs Kanzlei-Verzeichnis ist eine bewusste Admin-Aktion).
|
|
func (s *Store) CreateAccount(ctx context.Context, name string) (Account, error) {
|
|
var a Account
|
|
err := s.Pool.QueryRow(ctx, `
|
|
INSERT INTO account (name) VALUES ($1)
|
|
RETURNING id, name, verified, created_at
|
|
`, name).Scan(&a.ID, &a.Name, &a.Verified, &a.CreatedAt)
|
|
if err != nil {
|
|
return Account{}, fmt.Errorf("store: create account: %w", err)
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// GetAccount liest einen Mandanten anhand seiner ID.
|
|
func (s *Store) GetAccount(ctx context.Context, id string) (Account, error) {
|
|
var a Account
|
|
err := s.Pool.QueryRow(ctx, `
|
|
SELECT id, name, verified, created_at FROM account WHERE id = $1
|
|
`, id).Scan(&a.ID, &a.Name, &a.Verified, &a.CreatedAt)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return Account{}, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return Account{}, fmt.Errorf("store: get account: %w", err)
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// ListAccounts liefert alle Mandanten, neueste zuerst — für den
|
|
// Admin-Bereich (Accounts-Verwaltung).
|
|
func (s *Store) ListAccounts(ctx context.Context) ([]Account, error) {
|
|
rows, err := s.Pool.Query(ctx, `
|
|
SELECT id, name, verified, created_at FROM account ORDER BY created_at DESC
|
|
`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list accounts: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []Account
|
|
for rows.Next() {
|
|
var a Account
|
|
if err := rows.Scan(&a.ID, &a.Name, &a.Verified, &a.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("store: scan account: %w", err)
|
|
}
|
|
out = append(out, a)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("store: list accounts: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ListVerifiedKanzleien liefert alle Accounts, die fürs öffentliche
|
|
// Kanzlei-Verzeichnis freigegeben sind UND mindestens einen Nutzer der
|
|
// Rolle "kanzlei" haben — verified allein reicht nicht, falls ein Admin
|
|
// versehentlich einen Nicht-Kanzlei-Account markiert.
|
|
func (s *Store) ListVerifiedKanzleien(ctx context.Context) ([]Account, error) {
|
|
rows, err := s.Pool.Query(ctx, `
|
|
SELECT DISTINCT a.id, a.name, a.verified, a.created_at
|
|
FROM account a
|
|
JOIN app_user u ON u.account_id = a.id
|
|
WHERE a.verified = true AND u.role = 'kanzlei'
|
|
ORDER BY a.name
|
|
`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list verified kanzleien: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []Account
|
|
for rows.Next() {
|
|
var a Account
|
|
if err := rows.Scan(&a.ID, &a.Name, &a.Verified, &a.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("store: scan account: %w", err)
|
|
}
|
|
out = append(out, a)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("store: list verified kanzleien: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// SetAccountVerified setzt die Freigabe fürs Kanzlei-Verzeichnis.
|
|
func (s *Store) SetAccountVerified(ctx context.Context, id string, verified bool) (Account, error) {
|
|
var a Account
|
|
err := s.Pool.QueryRow(ctx, `
|
|
UPDATE account SET verified = $2 WHERE id = $1
|
|
RETURNING id, name, verified, created_at
|
|
`, id, verified).Scan(&a.ID, &a.Name, &a.Verified, &a.CreatedAt)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return Account{}, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return Account{}, fmt.Errorf("store: set account verified: %w", err)
|
|
}
|
|
return a, nil
|
|
}
|