Assign the existing site to one landlord so stommer.info can run tenancy.

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>
This commit is contained in:
LarsandCursor committed 2026-10-23 12:00:11 +02:00
1 parent d9e52c4789
commit 8ebc1a3878
93 files changed
+2506 -112

No files matched your search

+10
View File
@@ -36,6 +36,16 @@ public function definition(): array
];
}
public function configure(): static
{
return $this->afterCreating(function (Booking $booking): void {
Guest::withoutGlobalScope('tenant')
->whereKey($booking->guest_id)
->where('tenant_id', '!=', $booking->tenant_id)
->update(['tenant_id' => $booking->tenant_id]);
});
}
public function reserved(?\DateTimeInterface $holdExpiresAt = null): static
{
return $this->state(fn (): array => [
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Database\Factories;
use App\Models\Tenant;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends Factory<Tenant>
*/
class TenantFactory extends Factory
{
/**
* @return array<string, mixed>
*/
public function definition(): array
{
$name = fake()->unique()->company();
return [
'name' => $name,
'slug' => Str::slug($name).'-'.Str::lower(Str::random(4)),
'is_active' => true,
];
}
}
+18
View File
@@ -3,7 +3,9 @@
namespace Database\Factories;
use App\Enums\UserRole;
use App\Models\Tenant;
use App\Models\User;
use App\Tenancy\TenantContext;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
@@ -18,6 +20,22 @@ class UserFactory extends Factory
*/
protected static ?string $password;
public function configure(): static
{
return $this->afterCreating(function (User $user): void {
if ($user->role === null || app(TenantContext::class)->id() !== null) {
return;
}
$tenant = Tenant::query()->count() === 1
? Tenant::query()->firstOrFail()
: Tenant::factory()->create();
app(TenantContext::class)->set($tenant);
$user->attachCurrentRole($tenant);
});
}
/**
* Define the model's default state.
*
@@ -0,0 +1,100 @@
<?php
use App\Tenancy\AssignLegacyData;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Add landlords and point every existing row at "Stommer". Columns start nullable so the
* assignment can run, then become required. Old code ignores the new columns.
*/
public function up(): void
{
Schema::create('tenants', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->boolean('is_active')->default(true);
$table->timestamps();
});
foreach (AssignLegacyData::TABLES as $name) {
Schema::table($name, function (Blueprint $table) {
$table->foreignId('tenant_id')->nullable()->constrained()->restrictOnDelete();
});
}
AssignLegacyData::run();
foreach (AssignLegacyData::TABLES as $name) {
Schema::table($name, function (Blueprint $table) {
$table->unsignedBigInteger('tenant_id')->nullable(false)->change();
});
}
Schema::table('bookings', function (Blueprint $table) {
$table->dropUnique(['reference']);
$table->unique(['tenant_id', 'reference']);
});
Schema::table('content_blocks', function (Blueprint $table) {
$table->dropUnique(['key']);
$table->unique(['tenant_id', 'key']);
});
Schema::table('portal_emails', function (Blueprint $table) {
$table->dropUnique(['message_id']);
$table->unique(['tenant_id', 'message_id']);
});
Schema::table('properties', function (Blueprint $table) {
$table->dropUnique(['slug']);
$table->unique(['tenant_id', 'slug']);
});
Schema::table('guests', function (Blueprint $table) {
$table->index(['tenant_id', 'email']);
});
}
/**
* Drops the landlord assignment. Rows stay; the link to a landlord does not.
*/
public function down(): void
{
Schema::table('guests', function (Blueprint $table) {
$table->dropIndex(['tenant_id', 'email']);
});
Schema::table('properties', function (Blueprint $table) {
$table->dropUnique(['tenant_id', 'slug']);
$table->unique('slug');
});
Schema::table('portal_emails', function (Blueprint $table) {
$table->dropUnique(['tenant_id', 'message_id']);
$table->unique('message_id');
});
Schema::table('content_blocks', function (Blueprint $table) {
$table->dropUnique(['tenant_id', 'key']);
$table->unique('key');
});
Schema::table('bookings', function (Blueprint $table) {
$table->dropUnique(['tenant_id', 'reference']);
$table->unique('reference');
});
foreach (array_reverse(AssignLegacyData::TABLES) as $name) {
Schema::table($name, function (Blueprint $table) {
$table->dropConstrainedForeignId('tenant_id');
});
}
Schema::dropIfExists('tenants');
}
};
@@ -0,0 +1,68 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Membership is the role inside one landlord. The operator flag stays off here: it is granted
* afterwards with users:promote-operator, never by copying the old global role.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_operator')->default(false);
});
Schema::create('tenant_user', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('role');
$table->timestamps();
$table->unique(['tenant_id', 'user_id']);
});
Schema::create('tenant_access_logs', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamp('created_at')->useCurrent();
});
$tenantId = DB::table('tenants')->where('slug', 'stommer')->value('id');
if ($tenantId === null) {
return;
}
$now = now();
foreach (DB::table('users')->whereNotNull('role')->get(['id', 'role']) as $user) {
DB::table('tenant_user')->insert([
'tenant_id' => $tenantId,
'user_id' => $user->id,
'role' => $user->role,
'created_at' => $now,
'updated_at' => $now,
]);
}
}
/**
* Drops memberships and the operator flag. Accounts and their old role column stay.
*/
public function down(): void
{
Schema::dropIfExists('tenant_access_logs');
Schema::dropIfExists('tenant_user');
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('is_operator');
});
}
};
@@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* A website can answer on more than one host. The existing sites.domain value is the first host.
*/
public function up(): void
{
Schema::create('site_domains', function (Blueprint $table) {
$table->id();
$table->foreignId('site_id')->constrained()->cascadeOnDelete();
$table->string('host')->unique();
$table->timestamps();
});
$now = now();
foreach (DB::table('sites')->whereNotNull('domain')->where('domain', '!=', '')->get(['id', 'domain']) as $site) {
DB::table('site_domains')->insert([
'site_id' => $site->id,
'host' => mb_strtolower($site->domain),
'created_at' => $now,
'updated_at' => $now,
]);
}
}
public function down(): void
{
Schema::dropIfExists('site_domains');
}
};
+6
View File
@@ -4,6 +4,8 @@
use App\Enums\ExtraPricing;
use App\Models\Property;
use App\Models\Tenant;
use App\Tenancy\TenantContext;
use Illuminate\Database\Seeder;
/**
@@ -16,7 +18,11 @@ class PropertySeeder extends Seeder
*/
public function run(): void
{
$tenant = Tenant::stommer();
app(TenantContext::class)->set($tenant);
$property = Property::create([
'tenant_id' => $tenant->id,
'name' => 'Ferienwohnung Stommer',
'slug' => 'ferienwohnung-stommer',
'address' => 'Strandstraße 15, 18225 Kühlungsborn',
+6
View File
@@ -5,6 +5,8 @@
use App\Models\Page;
use App\Models\Property;
use App\Models\Site;
use App\Models\Tenant;
use App\Tenancy\TenantContext;
use Illuminate\Database\Seeder;
use Illuminate\Support\Arr;
@@ -19,7 +21,11 @@ class SiteSeeder extends Seeder
*/
public function run(): void
{
$tenantId = Property::withoutGlobalScope('tenant')->value('tenant_id') ?? Tenant::stommer()->id;
app(TenantContext::class)->set(Tenant::query()->findOrFail($tenantId));
$site = Site::create([
'tenant_id' => $tenantId,
'name' => 'Ferienwohnung Stommer',
'domain' => null,
'is_default' => true,
+3 -1
View File
@@ -5,6 +5,7 @@
use App\Enums\AmenityArea;
use App\Models\Faq;
use App\Models\Property;
use App\Tenancy\TenantContext;
use Illuminate\Database\Seeder;
/**
@@ -17,7 +18,8 @@ class WebsiteContentSeeder extends Seeder
*/
public function run(): void
{
$property = Property::query()->firstOrFail();
$property = Property::withoutGlobalScope('tenant')->firstOrFail();
app(TenantContext::class)->set($property->tenant);
$amenities = [
AmenityArea::General->value => ['Schnelles WLAN kostenlos', 'Pkw-Stellplatz inklusive', 'Abschließbarer Fahrradraum für 2 Räder', 'Diele mit großem Kleiderschrank und Spiegeln'],