Die Schalter aus Phase 3 schrieben nur in eine Tabelle, die niemand las. Ein gesperrter Portal-Abgleich lief trotzdem weiter. Das ist behoben. `App\Tenancy\TenantFeatures` ist die eine Stelle, die beantwortet, ob ein Mandant einen Dienst hat. Sie wird an allen Wegen abgefragt, die zum Dienst führen: - `RolePolicy::allows()` – damit greift die Mandanten-Ebene in *jeder* Richtlinie des Admins. Die Rolle sagt, was ein Benutzer darf; der Dienst sagt, was der Mandant überhaupt hat. Keine Rolle wiegt einen gesperrten Dienst auf. - `Channel::importsOccupations()` – Import und Scheduler. - Der öffentliche iCal-Feed antwortet mit 404. - `ProcessPortalMail` und `channels:fetch-mail` für den Postfach-Schnellweg. `TenantServiceKey::isWired()` trennt Gebautes von Geplantem. Nur verdrahtete Dienste erscheinen als Schalter; die drei KI-Dienste stehen als Hinweis da, bis die Anwendung sie abfragt. Eigene API-Schlüssel sind aus dem Betreiber- Formular verschwunden – sie gehören in den Admin des Mandanten, wenn es soweit ist (A16). Neu im Betreiber-Bereich: Seite „System" mit den Grenzen, die für alle Mandanten gelten (aus der .env, bewusst nicht änderbar), der Mandantenliste und dem Zugriffsprotokoll. Bisher war dort nur die Mandantenliste. Die Migration schaltet jedem vorhandenen Mandanten die Portal-Dienste frei, damit der Umbau laufenden Betrieb nicht stillschweigend stoppt. 240 Feature-Tests grün, davon fünf neue in `TenantServiceGateTest`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
101 lines
2.6 KiB
PHP
101 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Operator\Concerns;
|
||
|
||
use App\Enums\TenantServiceKey;
|
||
use App\Models\Tenant;
|
||
use App\Tenancy\TenantFeatures;
|
||
|
||
/**
|
||
* The service switches of the tenant form. They live in their own table, so the form state is read and
|
||
* written separately from the landlord's own columns. Only wired services are written – a switch for a
|
||
* service nothing asks about would promise something that does not happen.
|
||
*/
|
||
trait EditsTenantServices
|
||
{
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function mutateFormDataBeforeFill(array $data): array
|
||
{
|
||
$tenant = $this->record;
|
||
|
||
if (! $tenant instanceof Tenant) {
|
||
return $data; // creating: there is nothing to read yet
|
||
}
|
||
|
||
$stored = $tenant->services->keyBy('service');
|
||
|
||
$data['services'] = collect(TenantServiceKey::wired())
|
||
->mapWithKeys(fn (TenantServiceKey $service): array => [$service->value => [
|
||
'is_enabled' => $stored->get($service->value)?->is_enabled ?? $service->enabledByDefault(),
|
||
]])
|
||
->all();
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function mutateFormDataBeforeCreate(array $data): array
|
||
{
|
||
return $this->withoutServices($data);
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function mutateFormDataBeforeSave(array $data): array
|
||
{
|
||
return $this->withoutServices($data);
|
||
}
|
||
|
||
protected function afterCreate(): void
|
||
{
|
||
$this->saveServices();
|
||
}
|
||
|
||
protected function afterSave(): void
|
||
{
|
||
$this->saveServices();
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function withoutServices(array $data): array
|
||
{
|
||
$this->services = is_array($data['services'] ?? null) ? $data['services'] : [];
|
||
|
||
unset($data['services']);
|
||
|
||
return $data;
|
||
}
|
||
|
||
private function saveServices(): void
|
||
{
|
||
$tenant = $this->record;
|
||
|
||
if (! $tenant instanceof Tenant) {
|
||
return;
|
||
}
|
||
|
||
foreach (TenantServiceKey::wired() as $service) {
|
||
$state = $this->services[$service->value] ?? [];
|
||
|
||
$tenant->services()->updateOrCreate(
|
||
['service' => $service->value],
|
||
['is_enabled' => (bool) ($state['is_enabled'] ?? false)],
|
||
);
|
||
}
|
||
|
||
$tenant->unsetRelation('services');
|
||
app(TenantFeatures::class)->forget($tenant);
|
||
}
|
||
}
|