Optimierung Phase 0: Korrektheit & Security

Konsolidierter Entwicklungsplan aus den zwei Analyse-Dokumenten plus
Umsetzung der Phase-0-Massnahmen (Korrektheit/Security, je mit Tests):

0.1 BasicAuth: timing-sicherer Vergleich (hash_equals, beide Felder ohne
    Short-Circuit); Enabled-Check zur Laufzeit via config('auth.basic.*')
    statt env() im Bootstrap-Closure (greift jetzt unter config:cache);
    Config-Block + .env.example. Fund: auth.basic.* war nie als Config
    definiert, Gate war faktisch unkonfigurierbar.
0.2 Legacy-Invoice-"latest"-Abfrage zentralisiert (LegacyInvoice::
    scopeLatestFirst + User::latestLegacyInvoice); beide Consumer N+1-frei
    umgestellt; unterschiedliches null-Verhalten bewusst erhalten.
0.3 Payment-Option-Eligibility vereinheitlicht (period-aware, strikt):
    UserPaymentOption::scopeActiveNow/grandfatheredNow + User::
    hasActivePaymentOption; hasActiveBooking/hasUnlimitedPressReleaseQuota
    und ApiAccessEligibilityService darauf umgestellt; $hasGrandfathered-
    Mislabel behoben.
0.4 Theme->Portal-Vokabular: totes, projektfremdes ThemeMiddleware
    geloescht; Mapping in Portal::fromTheme() zentralisiert; fremden
    b2in-Default im ThemeServiceProvider auf Portal-Default korrigiert.
0.5 InvoiceNumberGenerator: Erst-Insert race-sicher via insertOrIgnore
    (Unique-Index auf circle existiert bereits). CreditWalletService war
    bereits abgesichert (Unique-Index + createOrFirst).

Tests: 4 neue Dateien, 5 bestehende auf gueltiges grandfathered_until
angepasst. Volle Suite gruen (793 passed, 1 skipped).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-06-19 08:09:02 +00:00
parent 6cb7b79dae
commit c7bd940f28
27 changed files with 990 additions and 133 deletions

View file

@ -2,7 +2,6 @@
namespace App\Services\Api;
use App\Enums\UserPaymentOptionStatus;
use App\Models\User;
class ApiAccessEligibilityService
@ -18,40 +17,16 @@ class ApiAccessEligibilityService
return 'Ihr Benutzerkonto ist nicht aktiv. API-Tokens können nur für aktive Benutzer erstellt werden.';
}
if ($this->hasActivePaymentOption($user) || $this->hasPaidLegacyInvoice($user)) {
if ($user->hasActivePaymentOption() || $this->hasPaidLegacyInvoice($user)) {
return null;
}
return 'API-Tokens werden erst freigeschaltet, wenn ein aktiver Zahlungsstatus oder freigegebener Bestandsschutz vorliegt.';
}
private function hasActivePaymentOption(User $user): bool
{
return $user->userPaymentOptions()
->where(function ($query): void {
$query
->where(function ($active): void {
$active
->where('status', UserPaymentOptionStatus::Active->value)
->whereDate('current_period_start', '<=', now())
->whereDate('current_period_end', '>=', now());
})
->orWhere(function ($grandfathered): void {
$grandfathered
->where('status', UserPaymentOptionStatus::Grandfathered->value)
->whereDate('grandfathered_until', '>=', now());
});
})
->exists();
}
private function hasPaidLegacyInvoice(User $user): bool
{
$latestInvoice = $user->legacyInvoices()
->latest('invoice_date')
->latest('id')
->first(['id', 'status']);
return $latestInvoice?->status === 'paid';
// Bewusstes Verhalten dieses Gates: ohne Legacy-Rechnung kein Zugang.
return $user->latestLegacyInvoice()?->status === 'paid';
}
}

View file

@ -73,8 +73,7 @@ class LegacyApiCustomerReporter
'paid_at',
'total_cents',
])
->latest('invoice_date')
->latest('id'),
->latestFirst(),
])
->where(function ($query): void {
$query->where('registration_type', RegistrationType::ApiUser->value)

View file

@ -39,22 +39,21 @@ class InvoiceNumberGenerator
}
$number = DB::transaction(function () use ($circle): int {
// Sequenz-Zeile race-sicher sicherstellen: insertOrIgnore nutzt den
// Unique-Index auf `circle`, sodass parallele Erstaufrufe nicht in
// doppelte Zeilen (und damit doppelte Nummern) laufen.
DB::table('invoice_number_sequences')->insertOrIgnore([
'circle' => $circle,
'next_number' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
$sequence = DB::table('invoice_number_sequences')
->where('circle', $circle)
->lockForUpdate()
->first();
if (! $sequence) {
DB::table('invoice_number_sequences')->insert([
'circle' => $circle,
'next_number' => 2,
'created_at' => now(),
'updated_at' => now(),
]);
return 1;
}
DB::table('invoice_number_sequences')
->where('id', $sequence->id)
->update(['next_number' => $sequence->next_number + 1, 'updated_at' => now()]);

View file

@ -3,7 +3,6 @@
namespace App\Services\Customer;
use App\Enums\Tier;
use App\Enums\UserPaymentOptionStatus;
use App\Models\Plan;
use App\Models\User;
@ -45,10 +44,7 @@ class CustomerStatusSummary
}
$hasGrandfathered = $user->userPaymentOptions()
->whereIn('status', [
UserPaymentOptionStatus::Active->value,
UserPaymentOptionStatus::Grandfathered->value,
])
->grandfatheredNow()
->exists();
return new self(