Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website. Co-authored-by: Cursor <cursoragent@cursor.com>
119 lines
3.2 KiB
PHP
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::query()->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;
|
|
}
|
|
}
|