Files
fewo-stommer/app/Tenancy/TenantContext.php
T
Lars StommerandClaude Opus 5 e6d4d89141 Mandanten Phase 3: Einstellungen, Texte und Dienste gehören dem Mandanten
Vermieterdaten, Bank, Steuerangabe, Adressen, Betriebsart und Fristen sind
Spalten von `tenants`. `App\Tenancy\TenantConfig` legt sie über
`config('fewo.*')` und `mail.from`, sobald der Mandantenkontext wechselt –
so lesen Vorlagen, Mails und der Mietvertrag weiter dieselben Schlüssel und
`resources/themes/**` bleibt unangetastet.

Die `.env` ist die Grundlinie: Rückfall, solange kein Mandant feststeht, und
Grenze für Löschfristen und Bot-Schutz (nur verschärfen, nicht lockern). Die
Flutgrenze bleibt global. Bank, Steuerangabe und eigene Dienst-Schlüssel
liegen verschlüsselt.

Die Seite System › Betrieb schreibt jetzt den Mandanten und enthält
zusätzlich Vermieterdaten, Bank und die Fristen. Tabelle `settings` und
Modell `Setting` entfallen; die Migration übernimmt die bisherigen Werte
aus `settings` und der Konfiguration in den Mandanten Stommer.

Neu: `tenant_services` (Freischaltung je Dienst mit optionalem eigenem
API-Schlüssel, nur im Betreiber-Bereich schaltbar) und `service_usages`.
Es zählt noch nichts, weil kein Dienst aufruft.

235 Feature-Tests grün. Der Umbauweg wurde zusätzlich auf einer Kopie der
lokalen Datenbank durchgespielt.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-23 20:49:22 +02:00

80 lines
1.6 KiB
PHP

<?php
namespace App\Tenancy;
use App\Models\Tenant;
/**
* The landlord of the current request, job or command. Empty means "not chosen yet".
* While presence is required, a query on a tenant-owned model without a landlord fails closed.
*/
class TenantContext
{
private ?Tenant $tenant = null;
private bool $required = false;
public function set(Tenant $tenant): void
{
$this->tenant = $tenant;
$this->applyConfig();
}
public function forget(): void
{
$this->tenant = null;
$this->required = false;
$this->applyConfig();
}
public function get(): ?Tenant
{
return $this->tenant;
}
public function id(): ?int
{
return $this->tenant?->id;
}
public function requirePresence(bool $required = true): void
{
$this->required = $required;
}
public function isRequired(): bool
{
return $this->required;
}
/**
* Run a callback as $tenant, then restore whoever was current before.
*
* @template T
*
* @param callable(): T $callback
* @return T
*/
public function run(Tenant $tenant, callable $callback): mixed
{
$previous = $this->tenant;
$this->tenant = $tenant;
$this->applyConfig();
try {
return $callback();
} finally {
$this->tenant = $previous;
$this->applyConfig();
}
}
/**
* Operating data of the landlord (addresses, periods, bank) is overlaid on config('fewo.*').
*/
private function applyConfig(): void
{
app(TenantConfig::class)->refresh();
}
}