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>
This commit is contained in:
1 parent
12e4079126
commit
e6d4d89141
24 files changed
+1123
-135
No files matched your search
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tenancy;
|
||||
|
||||
use App\Models\Tenant;
|
||||
use App\Support\Settings;
|
||||
use Illuminate\Mail\MailManager;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Lays the operating data of the current landlord over config('fewo.*') and the mail sender, so e-mails,
|
||||
* contracts and theme views keep reading the same keys. The values from config/.env stay the baseline:
|
||||
* infrastructure, the fallback while no landlord is known, and the limits a landlord may tighten but not exceed.
|
||||
*/
|
||||
class TenantConfig
|
||||
{
|
||||
/**
|
||||
* Retention periods and bot protection: the landlord may be stricter, never more lax than the baseline.
|
||||
* Fewer days/attempts is stricter.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const AT_MOST = [
|
||||
'fewo.guest_retention_years' => 'guest_retention_years',
|
||||
'fewo.retention.inquiry_months' => 'retention_inquiry_months',
|
||||
'fewo.retention.portal_email_excerpt_days' => 'retention_portal_email_excerpt_days',
|
||||
'fewo.retention.attribution_months' => 'retention_attribution_months',
|
||||
'fewo.spam.per_ip_per_hour' => 'spam_per_ip_per_hour',
|
||||
'fewo.spam.per_email_per_day' => 'spam_per_email_per_day',
|
||||
'fewo.spam.open_requests_per_email' => 'spam_open_requests_per_email',
|
||||
'fewo.spam.max_links' => 'spam_max_links',
|
||||
];
|
||||
|
||||
/**
|
||||
* The time trap works the other way round: a longer minimum time is stricter.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const AT_LEAST = [
|
||||
'fewo.spam.min_seconds' => 'spam_min_seconds',
|
||||
];
|
||||
|
||||
/**
|
||||
* Values the landlord chooses freely; empty means the baseline.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const FREE = [
|
||||
'fewo.deposit_due_days' => 'deposit_due_days',
|
||||
'fewo.review_request_after_days' => 'review_request_after_days',
|
||||
'fewo.channels.stale_after_minutes' => 'channel_stale_after_minutes',
|
||||
'fewo.channels.task_reminder_after_minutes' => 'channel_task_reminder_after_minutes',
|
||||
];
|
||||
|
||||
/**
|
||||
* Data of the landlord: an empty value stays empty instead of falling back to another landlord's.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const OWNED = [
|
||||
'fewo.owner.name' => 'owner_name',
|
||||
'fewo.owner.street' => 'owner_street',
|
||||
'fewo.owner.city' => 'owner_city',
|
||||
'fewo.owner.phone' => 'owner_phone',
|
||||
'fewo.owner.email' => 'contact_email',
|
||||
'fewo.bank.account_holder' => 'bank_account_holder',
|
||||
'fewo.bank.iban' => 'bank_iban',
|
||||
'fewo.bank.bic' => 'bank_bic',
|
||||
'fewo.bank.bank_name' => 'bank_name',
|
||||
];
|
||||
|
||||
/** @var array<string, mixed>|null */
|
||||
private ?array $baseline = null;
|
||||
|
||||
/**
|
||||
* What this class wrote into the config last time. A key that now holds something else was changed from
|
||||
* outside (a test, a command) – that value becomes the new baseline instead of being overwritten again.
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
private array $written = [];
|
||||
|
||||
/**
|
||||
* The landlord of the current context, else the only landlord there is (console, boot), else none.
|
||||
*/
|
||||
public function effectiveTenant(): ?Tenant
|
||||
{
|
||||
$current = app(TenantContext::class)->get();
|
||||
|
||||
if ($current !== null) {
|
||||
return $current;
|
||||
}
|
||||
|
||||
try {
|
||||
$tenants = Tenant::query()->limit(2)->get();
|
||||
} catch (Throwable) {
|
||||
return null; // before the migrations ran
|
||||
}
|
||||
|
||||
return $tenants->count() === 1 ? $tenants->first() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the effective landlord, or the baseline when there is none.
|
||||
*/
|
||||
public function refresh(): void
|
||||
{
|
||||
$this->apply($this->effectiveTenant());
|
||||
}
|
||||
|
||||
public function apply(?Tenant $tenant): void
|
||||
{
|
||||
$baseline = $this->baseline();
|
||||
$values = $baseline;
|
||||
|
||||
if ($tenant !== null && ! array_key_exists('owner_name', $tenant->getAttributes())) {
|
||||
$tenant = null; // the migration that adds the columns has not run yet; the baseline stands
|
||||
}
|
||||
|
||||
if ($tenant !== null) {
|
||||
foreach (self::OWNED as $key => $column) {
|
||||
$values[$key] = $tenant->{$column};
|
||||
}
|
||||
|
||||
foreach (self::FREE as $key => $column) {
|
||||
$values[$key] = $tenant->{$column} ?? $baseline[$key];
|
||||
}
|
||||
|
||||
foreach (self::AT_MOST as $key => $column) {
|
||||
$values[$key] = min($tenant->{$column} ?? $baseline[$key], $baseline[$key]);
|
||||
}
|
||||
|
||||
foreach (self::AT_LEAST as $key => $column) {
|
||||
$values[$key] = max($tenant->{$column} ?? $baseline[$key], $baseline[$key]);
|
||||
}
|
||||
|
||||
$values['mail.from.address'] = $tenant->sender_email ?: $baseline['mail.from.address'];
|
||||
$values['mail.from.name'] = $tenant->sender_name ?: $baseline['mail.from.name'];
|
||||
}
|
||||
|
||||
config($values);
|
||||
$this->written = $values;
|
||||
$this->applyMailRouting($tenant);
|
||||
}
|
||||
|
||||
/**
|
||||
* The mailer sends everything to one address while the landlord is in test mode.
|
||||
*/
|
||||
public function applyMailRouting(?Tenant $tenant): void
|
||||
{
|
||||
$manager = Mail::getFacadeRoot();
|
||||
|
||||
if (! $manager instanceof MailManager) {
|
||||
return; // Mail::fake() in tests
|
||||
}
|
||||
|
||||
$manager->purge();
|
||||
$redirect = Settings::mailRedirectFor($tenant);
|
||||
|
||||
if (filled($redirect)) {
|
||||
$manager->alwaysTo($redirect);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The value of a config key as the installation configured it, before any landlord was laid over it.
|
||||
*/
|
||||
public function baselineValue(string $key): mixed
|
||||
{
|
||||
return $this->baseline()[$key] ?? config($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* The highest value the landlord may enter for a column, or null when it is free.
|
||||
*/
|
||||
public function ceilingFor(string $column): ?int
|
||||
{
|
||||
$key = array_search($column, self::AT_MOST, true);
|
||||
|
||||
return $key === false ? null : (int) $this->baseline()[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* The lowest value the landlord may enter for a column, or null when it is free.
|
||||
*/
|
||||
public function floorFor(string $column): ?int
|
||||
{
|
||||
$key = array_search($column, self::AT_LEAST, true);
|
||||
|
||||
return $key === false ? null : (int) $this->baseline()[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function baseline(): array
|
||||
{
|
||||
$keys = [...array_keys(self::OWNED), ...array_keys(self::FREE), ...array_keys(self::AT_MOST), ...array_keys(self::AT_LEAST), 'mail.from.address', 'mail.from.name'];
|
||||
|
||||
if ($this->baseline === null) {
|
||||
$this->baseline = array_combine($keys, array_map(fn (string $key): mixed => config($key), $keys));
|
||||
|
||||
return $this->baseline;
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (array_key_exists($key, $this->written) && config($key) !== $this->written[$key]) {
|
||||
$this->baseline[$key] = config($key);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->baseline;
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,14 @@ class TenantContext
|
||||
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
|
||||
@@ -57,11 +59,21 @@ 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tenancy;
|
||||
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
/**
|
||||
* Operating data of a landlord taken from what the single-landlord installation used: the admin settings
|
||||
* and config/.env. Used once by the migration and as the factory default in tests.
|
||||
*/
|
||||
class TenantDefaults
|
||||
{
|
||||
/**
|
||||
* @param array<string, ?string> $settings Rows of the former `settings` table (key => value)
|
||||
* @param bool $encryptBank true for raw database writes; the model's own cast encrypts otherwise
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function forLegacyLandlord(array $settings = [], bool $encryptBank = true): array
|
||||
{
|
||||
return [
|
||||
'owner_name' => self::baseline('fewo.owner.name'),
|
||||
'owner_street' => self::baseline('fewo.owner.street'),
|
||||
'owner_city' => self::baseline('fewo.owner.city'),
|
||||
'owner_phone' => self::baseline('fewo.owner.phone'),
|
||||
'contact_email' => self::setting($settings, 'addresses.contact') ?? self::baseline('fewo.owner.email'),
|
||||
'notification_emails' => self::setting($settings, 'addresses.notifications') ?? config('fewo.notification_email'),
|
||||
'sender_name' => self::setting($settings, 'addresses.sender_name') ?? self::baseline('mail.from.name'),
|
||||
'sender_email' => self::setting($settings, 'addresses.sender_email') ?? self::baseline('mail.from.address'),
|
||||
'bank_account_holder' => self::bank(self::baseline('fewo.bank.account_holder'), $encryptBank),
|
||||
'bank_iban' => self::bank(self::baseline('fewo.bank.iban'), $encryptBank),
|
||||
'bank_bic' => self::bank(self::baseline('fewo.bank.bic'), $encryptBank),
|
||||
'bank_name' => self::bank(self::baseline('fewo.bank.bank_name'), $encryptBank),
|
||||
'operating_mode' => ($settings['operations.mode'] ?? 'test') === 'live' ? 'live' : 'test',
|
||||
'test_recipient' => self::setting($settings, 'operations.test_recipient'),
|
||||
'deposit_due_days' => self::baseline('fewo.deposit_due_days'),
|
||||
'review_request_after_days' => self::baseline('fewo.review_request_after_days'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The value of the installation, not the one a landlord may already have laid over the configuration.
|
||||
*/
|
||||
private static function baseline(string $key): mixed
|
||||
{
|
||||
return app(TenantConfig::class)->baselineValue($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, ?string> $settings
|
||||
*/
|
||||
private static function setting(array $settings, string $key): ?string
|
||||
{
|
||||
return filled($settings[$key] ?? null) ? (string) $settings[$key] : null;
|
||||
}
|
||||
|
||||
private static function bank(mixed $value, bool $encrypt): ?string
|
||||
{
|
||||
if (blank($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $encrypt ? Crypt::encryptString((string) $value) : (string) $value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user