> */ private const REQUIRES_ANY = [ Permission::Channels->value => [TenantServiceKey::PortalSync, TenantServiceKey::PortalMail], ]; /** * Unlocked services per landlord id, for the length of one request. * * @var array> */ 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 */ 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]; } }