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:
1 parent
d9e52c4789
commit
8ebc1a3878
93 files changed
+2506
-112
No files matched your search
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tenancy;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Points every existing row at the landlord "Stommer". Used once, when tenancy is introduced
|
||||
* on a database that already has the single-landlord data. Fresh databases have nothing to assign.
|
||||
*/
|
||||
class AssignLegacyData
|
||||
{
|
||||
/**
|
||||
* Tables that gain a tenant_id. Order is irrelevant; nothing is deleted.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public const TABLES = [
|
||||
'sites',
|
||||
'properties',
|
||||
'guests',
|
||||
'bookings',
|
||||
'channels',
|
||||
'portal_emails',
|
||||
'photos',
|
||||
'pages',
|
||||
'appearances',
|
||||
'notices',
|
||||
'faqs',
|
||||
'blocked_periods',
|
||||
'amenities',
|
||||
'reviews',
|
||||
'channel_tasks',
|
||||
'payments',
|
||||
'seasons',
|
||||
'extras',
|
||||
'special_offers',
|
||||
'channel_sync_logs',
|
||||
'content_blocks',
|
||||
];
|
||||
|
||||
public static function run(): void
|
||||
{
|
||||
$pending = false;
|
||||
|
||||
foreach (self::TABLES as $table) {
|
||||
if (DB::table($table)->whereNull('tenant_id')->exists()) {
|
||||
$pending = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (! $pending) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tenantId = DB::table('tenants')->where('slug', 'stommer')->value('id');
|
||||
|
||||
if ($tenantId === null) {
|
||||
$tenantId = DB::table('tenants')->insertGetId([
|
||||
'name' => 'Stommer',
|
||||
'slug' => 'stommer',
|
||||
'is_active' => true,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
foreach (self::TABLES as $table) {
|
||||
DB::table($table)->whereNull('tenant_id')->update(['tenant_id' => $tenantId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
public function forget(): void
|
||||
{
|
||||
$this->tenant = null;
|
||||
$this->required = false;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
try {
|
||||
return $callback();
|
||||
} finally {
|
||||
$this->tenant = $previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tenancy;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* A tenant-owned query or insert ran without a current landlord while one was required.
|
||||
*/
|
||||
class TenantContextMissing extends RuntimeException
|
||||
{
|
||||
public function __construct(string $model)
|
||||
{
|
||||
parent::__construct("No current tenant for [{$model}].");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user