Files
Lars StommerandClaude Opus 5 fd2a7f7ded Dienste wirklich anschließen: zentrale Steuerung statt folgenloser Schalter
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>
2026-10-24 09:14:10 +02:00

106 lines
3.2 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\Enums\Permission;
use App\Enums\TenantServiceKey;
use App\Models\Tenant;
use App\Models\TenantService;
use Throwable;
/**
* The single place that answers "is this service unlocked for this landlord". Everything that acts on a
* service asks here: the policies of the admin, the console commands, the portal feed. Roles decide what a
* *user* may do, this decides what the *landlord* has at all – a landlord without the service keeps nobody
* out by role, so both questions have to be asked, and asking them in two places is how they drift apart.
*/
class TenantFeatures
{
/**
* Services a permission depends on. The permission is granted when at least one of them is unlocked,
* so a landlord with only the mailbox still reaches the portal screens.
*
* @var array<string, list<TenantServiceKey>>
*/
private const REQUIRES_ANY = [
Permission::Channels->value => [TenantServiceKey::PortalSync, TenantServiceKey::PortalMail],
];
/**
* Unlocked services per landlord id, for the length of one request.
*
* @var array<int, array<string, bool>>
*/
private array $cache = [];
/**
* Whether the landlord may use the service. Without a landlord (console before a context is chosen,
* boot) the default of the service applies, which keeps a single-landlord installation unchanged.
*/
public function enabled(TenantServiceKey $service, ?Tenant $tenant = null): bool
{
$tenant ??= app(TenantContext::class)->get();
if ($tenant === null) {
return $service->enabledByDefault();
}
return $this->forTenant($tenant)[$service->value] ?? $service->enabledByDefault();
}
/**
* Whether the landlord has what a permission needs. A permission that depends on no service is always
* granted here – the role still decides separately.
*/
public function permits(Permission $permission, ?Tenant $tenant = null): bool
{
$required = self::REQUIRES_ANY[$permission->value] ?? [];
if ($required === []) {
return true;
}
foreach ($required as $service) {
if ($this->enabled($service, $tenant)) {
return true;
}
}
return false;
}
/**
* Forgets what was read, so a switch takes effect without a new request.
*/
public function forget(?Tenant $tenant = null): void
{
if ($tenant === null) {
$this->cache = [];
return;
}
unset($this->cache[$tenant->id]);
}
/**
* @return array<string, bool>
*/
private function forTenant(Tenant $tenant): array
{
if (! array_key_exists($tenant->id, $this->cache)) {
try {
$this->cache[$tenant->id] = TenantService::query()
->where('tenant_id', $tenant->id)
->pluck('is_enabled', 'service')
->map(fn (mixed $enabled): bool => (bool) $enabled)
->all();
} catch (Throwable) {
return []; // before the migrations ran; the defaults apply
}
}
return $this->cache[$tenant->id];
}
}