## Summary - introduce a canonical admin tenant filter-state helper and route all in-scope workspace-admin tenant resolution through `OperateHubShell::activeEntitledTenant()` - align operations monitoring, operation-run deep links, Entra group admin list/view/search behavior, and shared context-bar rendering with the documented scope contract - add the Spec 135 design artifacts, architecture note, focused guardrail coverage, and non-regression tests for filter persistence, direct-record access, and global search safety ## Validation - `vendor/bin/sail bin pint --dirty --format agent` - `vendor/bin/sail artisan test --compact tests/Feature/Monitoring/OperationsKpiHeaderTenantContextTest.php tests/Feature/Monitoring/OperationsTenantScopeTest.php tests/Feature/Monitoring/OperationsCanonicalUrlsTest.php tests/Feature/Spec085/OperationsIndexHeaderTest.php tests/Feature/Spec085/RunDetailBackAffordanceTest.php tests/Feature/Filament/OperationRunListFiltersTest.php tests/Feature/Filament/EntraGroupAdminScopeTest.php tests/Feature/Filament/EntraGroupGlobalSearchScopeTest.php tests/Feature/DirectoryGroups/BrowseGroupsTest.php tests/Feature/Filament/EntraGroupEnterpriseDetailPageTest.php tests/Feature/Filament/PolicyVersionResolvedReferenceLinksTest.php tests/Feature/Filament/EntraGroupResolvedReferencePresentationTest.php tests/Feature/Guards/AdminTenantResolverGuardTest.php tests/Feature/OpsUx/OperateHubShellTest.php tests/Feature/Filament/Alerts/AlertsKpiHeaderTest.php tests/Feature/Alerts/AlertDeliveryDeepLinkFiltersTest.php` - `vendor/bin/sail artisan test --compact tests/Feature/Filament/TableStatePersistenceTest.php tests/Feature/Filament/TenantScopingTest.php tests/Feature/Filament/Alerts/AlertDeliveryViewerTest.php tests/Unit/Support/References/CapabilityAwareReferenceResolverTest.php` ## Notes - Filament v5 remains on Livewire v4.0+ compliant surfaces only. - No provider registration changes were needed; Laravel 12 provider registration remains in `bootstrap/providers.php`. - Entra group global search remains enabled and is now scoped to the canonical admin tenant contract. Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #164
167 lines
4.4 KiB
PHP
167 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Support\OperateHub;
|
|
|
|
use App\Filament\Pages\TenantDashboard;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Auth\CapabilityResolver;
|
|
use App\Support\Workspaces\WorkspaceContext;
|
|
use Filament\Actions\Action;
|
|
use Filament\Facades\Filament;
|
|
use Illuminate\Http\Request;
|
|
|
|
final class OperateHubShell
|
|
{
|
|
public function __construct(
|
|
private WorkspaceContext $workspaceContext,
|
|
private CapabilityResolver $capabilityResolver,
|
|
) {}
|
|
|
|
public function scopeLabel(?Request $request = null): string
|
|
{
|
|
$activeTenant = $this->activeEntitledTenant($request);
|
|
|
|
if ($activeTenant instanceof Tenant) {
|
|
return 'Filtered by tenant: '.$activeTenant->name;
|
|
}
|
|
|
|
return 'All tenants';
|
|
}
|
|
|
|
/**
|
|
* @return array{label: string, url: string}|null
|
|
*/
|
|
public function returnAffordance(?Request $request = null): ?array
|
|
{
|
|
$activeTenant = $this->activeEntitledTenant($request);
|
|
|
|
if ($activeTenant instanceof Tenant) {
|
|
return [
|
|
'label' => 'Back to '.$activeTenant->name,
|
|
'url' => TenantDashboard::getUrl(panel: 'tenant', tenant: $activeTenant),
|
|
];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @return array<Action>
|
|
*/
|
|
public function headerActions(
|
|
string $scopeActionName = 'operate_hub_scope',
|
|
string $returnActionName = 'operate_hub_return',
|
|
?Request $request = null,
|
|
): array {
|
|
$actions = [
|
|
Action::make($scopeActionName)
|
|
->label($this->scopeLabel($request))
|
|
->color('gray')
|
|
->disabled(),
|
|
];
|
|
|
|
$returnAffordance = $this->returnAffordance($request);
|
|
|
|
if (is_array($returnAffordance)) {
|
|
$actions[] = Action::make($returnActionName)
|
|
->label($returnAffordance['label'])
|
|
->icon('heroicon-o-arrow-left')
|
|
->color('gray')
|
|
->url($returnAffordance['url']);
|
|
}
|
|
|
|
return $actions;
|
|
}
|
|
|
|
public function activeEntitledTenant(?Request $request = null): ?Tenant
|
|
{
|
|
return $this->resolveActiveTenant($request);
|
|
}
|
|
|
|
private function resolveActiveTenant(?Request $request = null): ?Tenant
|
|
{
|
|
$routeTenant = $this->resolveRouteTenant($request);
|
|
|
|
if ($request?->route()?->hasParameter('tenant')) {
|
|
return $routeTenant;
|
|
}
|
|
|
|
if ($routeTenant instanceof Tenant) {
|
|
return $routeTenant;
|
|
}
|
|
|
|
$tenant = Filament::getTenant();
|
|
|
|
if ($tenant instanceof Tenant && $this->isEntitled($tenant, $request)) {
|
|
return $tenant;
|
|
}
|
|
|
|
$rememberedTenantId = $this->workspaceContext->lastTenantId($request);
|
|
|
|
if ($rememberedTenantId === null) {
|
|
return null;
|
|
}
|
|
|
|
$rememberedTenant = Tenant::query()->whereKey($rememberedTenantId)->first();
|
|
|
|
if (! $rememberedTenant instanceof Tenant) {
|
|
$this->workspaceContext->clearLastTenantId($request);
|
|
|
|
return null;
|
|
}
|
|
|
|
if (! $this->isEntitled($rememberedTenant, $request)) {
|
|
$this->workspaceContext->clearLastTenantId($request);
|
|
|
|
return null;
|
|
}
|
|
|
|
return $rememberedTenant;
|
|
}
|
|
|
|
private function resolveRouteTenant(?Request $request = null): ?Tenant
|
|
{
|
|
$route = $request?->route();
|
|
|
|
if (! $route?->hasParameter('tenant')) {
|
|
return null;
|
|
}
|
|
|
|
$routeTenant = $route->parameter('tenant');
|
|
|
|
$tenant = $routeTenant instanceof Tenant
|
|
? $routeTenant
|
|
: Tenant::query()->withTrashed()->where('external_id', (string) $routeTenant)->first();
|
|
|
|
if (! $tenant instanceof Tenant || ! $this->isEntitled($tenant, $request)) {
|
|
return null;
|
|
}
|
|
|
|
return $tenant;
|
|
}
|
|
|
|
private function isEntitled(Tenant $tenant, ?Request $request = null): bool
|
|
{
|
|
if (! $tenant->isActive()) {
|
|
return false;
|
|
}
|
|
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
return false;
|
|
}
|
|
|
|
$workspaceId = $this->workspaceContext->currentWorkspaceId($request);
|
|
|
|
if ($workspaceId !== null && (int) $tenant->workspace_id !== (int) $workspaceId) {
|
|
return false;
|
|
}
|
|
|
|
return $this->capabilityResolver->isMember($user, $tenant);
|
|
}
|
|
}
|