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>
This commit is contained in:
1 parent
220ec6bfdb
commit
fd2a7f7ded
15 files changed
+598
-55
No files matched your search
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Enums\TenantServiceKey;
|
||||
use App\Enums\UserRole;
|
||||
use App\Models\Booking;
|
||||
use App\Models\Channel;
|
||||
use App\Models\ChannelTask;
|
||||
use App\Models\PortalEmail;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Tenancy\TenantContext;
|
||||
use App\Tenancy\TenantFeatures;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* A service the operator has locked must not be reachable by any route: not by the scheduler, not by the
|
||||
* public feed, and not by a user whose role would otherwise allow it.
|
||||
*/
|
||||
class TenantServiceGateTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_a_locked_portal_service_stops_the_import_and_the_feed(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
app(TenantContext::class)->set($tenant);
|
||||
$channel = Channel::factory()->create();
|
||||
|
||||
$this->assertTrue($channel->importsOccupations());
|
||||
$this->get(route('channels.feed', ['token' => $channel->export_token]))->assertOk();
|
||||
|
||||
$this->lock($tenant, TenantServiceKey::PortalSync);
|
||||
|
||||
$this->assertFalse($channel->fresh()->importsOccupations());
|
||||
$this->get(route('channels.feed', ['token' => $channel->export_token]))->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_the_sync_command_skips_a_landlord_without_the_service(): void
|
||||
{
|
||||
$allowed = Tenant::factory()->create(['name' => 'Mit Dienst']);
|
||||
$locked = Tenant::factory()->create(['name' => 'Ohne Dienst']);
|
||||
|
||||
app(TenantContext::class)->set($allowed);
|
||||
$allowedChannel = Channel::factory()->create();
|
||||
|
||||
app(TenantContext::class)->set($locked);
|
||||
$lockedChannel = Channel::factory()->create();
|
||||
$this->lock($locked, TenantServiceKey::PortalSync);
|
||||
|
||||
app(TenantContext::class)->forget();
|
||||
|
||||
$this->assertTrue($allowedChannel->fresh()->importsOccupations());
|
||||
$this->assertFalse($lockedChannel->fresh()->importsOccupations());
|
||||
}
|
||||
|
||||
public function test_a_locked_service_closes_the_admin_screens_whatever_the_role_says(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
app(TenantContext::class)->set($tenant);
|
||||
|
||||
$admin = User::factory()->role(UserRole::Admin)->create();
|
||||
$this->actingAs($admin);
|
||||
|
||||
$this->assertTrue(Gate::forUser($admin)->allows('viewAny', ChannelTask::class));
|
||||
|
||||
$this->lock($tenant, TenantServiceKey::PortalSync);
|
||||
$this->lock($tenant, TenantServiceKey::PortalMail);
|
||||
|
||||
$this->assertFalse(Gate::forUser($admin)->allows('viewAny', ChannelTask::class));
|
||||
$this->assertFalse(Gate::forUser($admin)->allows('viewAny', PortalEmail::class));
|
||||
|
||||
// The role itself is untouched – the landlord simply does not have the service.
|
||||
$this->assertTrue(Gate::forUser($admin)->allows('viewAny', Booking::class));
|
||||
}
|
||||
|
||||
public function test_the_mailbox_fast_path_stays_open_while_only_the_sync_is_locked(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
app(TenantContext::class)->set($tenant);
|
||||
|
||||
$admin = User::factory()->role(UserRole::Admin)->create();
|
||||
|
||||
$this->lock($tenant, TenantServiceKey::PortalSync);
|
||||
|
||||
$this->assertTrue(Gate::forUser($admin)->allows('viewAny', PortalEmail::class));
|
||||
}
|
||||
|
||||
public function test_the_system_page_shows_the_limits_and_stays_closed_for_a_landlords_administrator(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create(['name' => 'Beispiel GbR']);
|
||||
app(TenantContext::class)->set($tenant);
|
||||
config(['fewo.spam.total_per_hour' => 30, 'fewo.guest_retention_years' => 10]);
|
||||
|
||||
$admin = User::factory()->role(UserRole::Admin)->create();
|
||||
$this->actingAs($admin)->get('/betreiber/system')->assertForbidden();
|
||||
|
||||
$admin->forceFill(['is_operator' => true])->saveQuietly();
|
||||
|
||||
$this->actingAs($admin)->get('/betreiber/system')
|
||||
->assertOk()
|
||||
->assertSee('Beispiel GbR')
|
||||
->assertSee('30 je Stunde')
|
||||
->assertSee('10 Jahre');
|
||||
}
|
||||
|
||||
private function lock(Tenant $tenant, TenantServiceKey $service): void
|
||||
{
|
||||
$tenant->services()->updateOrCreate(['service' => $service->value], ['is_enabled' => false]);
|
||||
app(TenantFeatures::class)->forget($tenant);
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,9 @@
|
||||
use App\Support\Settings;
|
||||
use App\Tenancy\TenantConfig;
|
||||
use App\Tenancy\TenantContext;
|
||||
use App\Tenancy\TenantFeatures;
|
||||
use Filament\Facades\Filament;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -76,29 +76,27 @@ public function test_without_a_landlord_the_configuration_of_the_installation_ap
|
||||
$this->assertNull(Settings::tenant());
|
||||
}
|
||||
|
||||
public function test_the_operator_switches_services_per_landlord_and_an_empty_key_is_allowed(): void
|
||||
public function test_only_services_the_application_really_asks_about_are_offered(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$this->assertSame(
|
||||
[TenantServiceKey::PortalSync, TenantServiceKey::PortalMail],
|
||||
TenantServiceKey::wired(),
|
||||
);
|
||||
|
||||
$tenant->services()->create(['service' => TenantServiceKey::PortalSync->value, 'is_enabled' => true, 'api_key' => null]);
|
||||
$tenant->services()->create(['service' => TenantServiceKey::AiOffers->value, 'is_enabled' => true, 'api_key' => 'sk-beispiel']);
|
||||
|
||||
$sync = $tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole();
|
||||
$ai = $tenant->services()->where('service', TenantServiceKey::AiOffers->value)->sole();
|
||||
|
||||
$this->assertTrue($sync->is_enabled);
|
||||
$this->assertNull($sync->api_key);
|
||||
$this->assertSame('sk-beispiel', $ai->api_key);
|
||||
$this->assertSame(0, Tenant::factory()->create()->services()->count());
|
||||
foreach (TenantServiceKey::cases() as $service) {
|
||||
if (! $service->isWired()) {
|
||||
$this->assertFalse($service->enabledByDefault(), $service->value.' darf nicht vorgegeben an sein, solange nichts danach fragt.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function test_an_own_service_key_is_stored_encrypted(): void
|
||||
public function test_a_landlord_without_a_decision_keeps_the_portal_services(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$service = $tenant->services()->create(['service' => TenantServiceKey::AiMarketing->value, 'is_enabled' => true, 'api_key' => 'sk-geheim']);
|
||||
|
||||
$this->assertNotSame('sk-geheim', DB::table('tenant_services')->where('id', $service->id)->value('api_key'));
|
||||
$this->assertSame('sk-geheim', $service->fresh()->api_key);
|
||||
$this->assertSame(0, $tenant->services()->count());
|
||||
$this->assertTrue(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $tenant));
|
||||
$this->assertFalse(app(TenantFeatures::class)->enabled(TenantServiceKey::AiOffers, $tenant));
|
||||
}
|
||||
|
||||
public function test_the_operator_switches_a_service_on_the_tenant_form(): void
|
||||
@@ -112,17 +110,17 @@ public function test_the_operator_switches_a_service_on_the_tenant_form(): void
|
||||
|
||||
Livewire::test(EditTenant::class, ['record' => $tenant->getRouteKey()])
|
||||
->fillForm([
|
||||
'services.'.TenantServiceKey::PortalSync->value.'.is_enabled' => true,
|
||||
'services.'.TenantServiceKey::AiOffers->value.'.is_enabled' => true,
|
||||
'services.'.TenantServiceKey::AiOffers->value.'.api_key' => 'sk-beispiel',
|
||||
'services.'.TenantServiceKey::PortalSync->value.'.is_enabled' => false,
|
||||
'services.'.TenantServiceKey::PortalMail->value.'.is_enabled' => true,
|
||||
])
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
$this->assertTrue($tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole()->is_enabled);
|
||||
$this->assertNull($tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole()->api_key);
|
||||
$this->assertSame('sk-beispiel', $tenant->services()->where('service', TenantServiceKey::AiOffers->value)->sole()->api_key);
|
||||
$this->assertFalse($tenant->services()->where('service', TenantServiceKey::AiGuestMail->value)->sole()->is_enabled);
|
||||
$this->assertFalse($tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole()->is_enabled);
|
||||
$this->assertTrue($tenant->services()->where('service', TenantServiceKey::PortalMail->value)->sole()->is_enabled);
|
||||
$this->assertSame(0, $tenant->services()->where('service', TenantServiceKey::AiOffers->value)->count());
|
||||
|
||||
$this->assertFalse(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $tenant));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user