99 lines
2.6 KiB
PHP
99 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Pages\ManagedTenants;
|
|
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Auth\CapabilityResolver;
|
|
use App\Support\Auth\Capabilities;
|
|
use App\Support\ManagedTenants\ManagedTenantContext;
|
|
use Filament\Actions;
|
|
use Filament\Pages\Page;
|
|
|
|
class Current extends Page
|
|
{
|
|
protected static bool $isDiscovered = false;
|
|
|
|
protected static bool $shouldRegisterNavigation = false;
|
|
|
|
protected static ?string $slug = 'managed-tenants/current';
|
|
|
|
protected static ?string $title = 'Current managed tenant';
|
|
|
|
protected string $view = 'filament.pages.managed-tenants.current';
|
|
|
|
public ?Tenant $tenant = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
if (! $user->tenantMemberships()->exists()) {
|
|
abort(404);
|
|
}
|
|
|
|
$currentTenantId = ManagedTenantContext::currentTenantId();
|
|
|
|
if (is_int($currentTenantId)) {
|
|
$selectedTenant = Tenant::withTrashed()->find($currentTenantId);
|
|
|
|
if (! $selectedTenant instanceof Tenant) {
|
|
ManagedTenantContext::clearCurrentTenant();
|
|
} elseif (! $selectedTenant->isActive()) {
|
|
ManagedTenantContext::clearCurrentTenant();
|
|
ManagedTenantContext::setArchivedTenant($selectedTenant);
|
|
|
|
$this->redirect(ArchivedStatus::getUrl());
|
|
|
|
return;
|
|
} else {
|
|
$this->tenant = $selectedTenant;
|
|
}
|
|
}
|
|
|
|
/** @var CapabilityResolver $resolver */
|
|
$resolver = app(CapabilityResolver::class);
|
|
|
|
$canViewAny = Tenant::query()
|
|
->whereIn('id', $user->tenants()->withTrashed()->pluck('tenants.id'))
|
|
->cursor()
|
|
->contains(fn (Tenant $tenant): bool => $resolver->can($user, $tenant, Capabilities::TENANT_MANAGED_TENANTS_VIEW));
|
|
|
|
if (! $canViewAny) {
|
|
abort(403);
|
|
}
|
|
|
|
if (! $this->tenant instanceof Tenant) {
|
|
return;
|
|
}
|
|
|
|
if (! $resolver->isMember($user, $this->tenant)) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! $resolver->can($user, $this->tenant, Capabilities::TENANT_MANAGED_TENANTS_VIEW)) {
|
|
abort(403);
|
|
}
|
|
|
|
// The active status is already verified above.
|
|
}
|
|
|
|
/**
|
|
* @return array<Actions\Action>
|
|
*/
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Actions\Action::make('back_to_managed_tenants')
|
|
->label('Back to managed tenants')
|
|
->url(Index::getUrl()),
|
|
];
|
|
}
|
|
}
|