Files
fewo-stommer/app/Tenancy/TenantConfig.php
LarsandClaude Opus 5.5 0c10880e4f
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m10s
Testbetrieb: eine Schranke fuer jede ausgehende E-Mail
EnforceMailTestMode prueft jede Mail unmittelbar vor dem Versand (MessageSending,
jeder Mailer): im Testbetrieb gehen An/Cc/Bcc nur an die Testadresse, ohne
Testadresse wird nichts versendet; der Modus wird je Mail frisch gelesen.
Ersetzt alwaysTo am Standard-Mailer. Tests inkl. Pruefung, dass kein Code am
Mailer vorbei versendet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 21:03:24 +01:00

213 lines
6.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Tenancy;
use App\Listeners\EnforceMailTestMode;
use App\Models\Tenant;
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 = [
'app.name' => 'name',
'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);
}
/**
* Rebuilds the mailers so that they use the sender of the current landlord. Where mails go in test operation is
* decided for every single mail by the one gate {@see EnforceMailTestMode}, not here.
*/
public function applyMailRouting(?Tenant $tenant): void
{
$manager = Mail::getFacadeRoot();
if (! $manager instanceof MailManager) {
return; // Mail::fake() in tests
}
$manager->purge();
}
/**
* 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;
}
}