Files
fewo-stommer/app/Models/Concerns/BelongsToTenant.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

119 lines
3.2 KiB
PHP

<?php
namespace App\Models\Concerns;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Guest;
use App\Models\Property;
use App\Models\Site;
use App\Models\Tenant;
use App\Tenancy\TenantContext;
use App\Tenancy\TenantContextMissing;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
/**
* Every row belongs to exactly one landlord. When a landlord is current, queries only see that
* landlord. Inserts copy the landlord from the parent row, then from the current context.
*
* @mixin Model
*/
trait BelongsToTenant
{
public function initializeBelongsToTenant(): void
{
$this->mergeFillable(['tenant_id']);
}
public static function bootBelongsToTenant(): void
{
static::addGlobalScope('tenant', function (Builder $builder): void {
$context = app(TenantContext::class);
if ($context->id() !== null) {
$builder->where($builder->getModel()->qualifyColumn('tenant_id'), $context->id());
return;
}
if ($context->isRequired()) {
throw new TenantContextMissing($builder->getModel()::class);
}
});
static::creating(function (Model $model): void {
$inherited = self::tenantIdFromParent($model);
if ($inherited !== null) {
$model->setAttribute('tenant_id', $inherited);
return;
}
if (filled($model->getAttribute('tenant_id'))) {
return;
}
$current = app(TenantContext::class)->id();
if ($current !== null) {
$model->setAttribute('tenant_id', $current);
return;
}
if (app()->runningUnitTests() && ! app(TenantContext::class)->isRequired()) {
$model->setAttribute('tenant_id', Tenant::factory()->create([
'name' => 'Testmandant',
'slug' => 'test-'.Str::lower(Str::random(10)),
])->id);
return;
}
throw new TenantContextMissing($model::class);
});
}
/**
* @return BelongsTo<Tenant, $this>
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
/**
* The parent row wins, so a booking cannot land on a different landlord than its apartment.
*/
private static function tenantIdFromParent(Model $model): ?int
{
$parents = [
'property_id' => Property::class,
'booking_id' => Booking::class,
'channel_id' => Channel::class,
'site_id' => Site::class,
'guest_id' => Guest::class,
];
foreach ($parents as $column => $class) {
$id = $model->getAttribute($column);
if (! $id) {
continue;
}
$tenantId = $class::withoutGlobalScope('tenant')->whereKey($id)->value('tenant_id');
if ($tenantId !== null) {
return (int) $tenantId;
}
}
return null;
}
}