TenantAtlas/app/Support/References/Resolvers/EntraGroupReferenceResolver.php
ahmido cc93329672 feat: canonical tenant context resolution (#164)
## 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
2026-03-11 21:24:28 +00:00

117 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Support\References\Resolvers;
use App\Filament\Resources\EntraGroupResource;
use App\Models\EntraGroup;
use App\Models\Tenant;
use App\Models\User;
use App\Services\Auth\CapabilityResolver;
use App\Services\Directory\EntraGroupLabelResolver;
use App\Support\Auth\Capabilities;
use App\Support\References\ReferenceClass;
use App\Support\References\ReferenceDescriptor;
use App\Support\References\ReferenceLinkTarget;
use App\Support\References\ResolvedReference;
final class EntraGroupReferenceResolver extends BaseReferenceResolver
{
public function __construct(
\App\Support\References\ReferenceTypeLabelCatalog $typeLabels,
private readonly CapabilityResolver $capabilityResolver,
private readonly EntraGroupLabelResolver $groupLabelResolver,
) {
parent::__construct($typeLabels);
}
public function referenceClass(): ReferenceClass
{
return ReferenceClass::Group;
}
public function resolve(ReferenceDescriptor $descriptor): ResolvedReference
{
$tenantId = $descriptor->tenantId;
if ($tenantId === null || $tenantId <= 0) {
return $this->unresolved($descriptor, primaryLabel: 'Group');
}
$tenant = Tenant::query()->whereKey($tenantId)->first();
if (! $tenant instanceof Tenant) {
return $this->unresolved($descriptor, primaryLabel: 'Group');
}
$cachedDisplayName = $this->contextString($descriptor, 'cached_display_name');
$resolvedFromCache = $descriptor->contextValue('resolved_from_cache');
$group = EntraGroup::query()
->where('tenant_id', $tenantId)
->where('entra_id', strtolower($descriptor->rawIdentifier))
->first();
if (! $group instanceof EntraGroup && $cachedDisplayName === null) {
$cachedDisplayName = $this->groupLabelResolver->lookupOne($tenant, $descriptor->rawIdentifier);
$resolvedFromCache = $cachedDisplayName !== null;
}
if ($group instanceof EntraGroup) {
if (! $this->canOpenTenantRecord($group->tenant, Capabilities::TENANT_VIEW)) {
return $this->inaccessible($descriptor, primaryLabel: 'Group');
}
return $this->resolved(
descriptor: $descriptor,
primaryLabel: (string) $group->display_name,
secondaryLabel: $this->groupTypeLabel($group),
linkTarget: new ReferenceLinkTarget(
targetKind: ReferenceClass::Group->value,
url: EntraGroupResource::scopedUrl('view', ['record' => $group], $group->tenant),
actionLabel: 'View group',
contextBadge: 'Tenant',
),
);
}
if ($cachedDisplayName !== null) {
return ($resolvedFromCache === true)
? $this->partiallyResolved($descriptor, primaryLabel: $cachedDisplayName, secondaryLabel: 'Cached group')
: $this->externalLimited($descriptor, primaryLabel: $cachedDisplayName, secondaryLabel: 'Captured group');
}
return $this->unresolved($descriptor, primaryLabel: 'Group');
}
private function canOpenTenantRecord(?Tenant $tenant, string $capability): bool
{
$user = auth()->user();
return $tenant instanceof Tenant
&& $user instanceof User
&& $this->capabilityResolver->isMember($user, $tenant)
&& $this->capabilityResolver->can($user, $tenant, $capability);
}
private function groupTypeLabel(EntraGroup $group): string
{
$groupTypes = $group->group_types;
if (is_array($groupTypes) && in_array('Unified', $groupTypes, true)) {
return 'Microsoft 365 group';
}
if ($group->security_enabled) {
return 'Security group';
}
if ($group->mail_enabled) {
return 'Mail-enabled group';
}
return 'Directory group';
}
}