TenantAtlas/app/Support/Tenants/TenantLifecycle.php
ahmido 417df4f9aa feat: central tenant operability policy (#177)
## Summary
- centralize tenant operability into a lane-aware, actor-aware policy boundary
- align selector eligibility, administrative discoverability, remembered context, tenant-bound routes, and canonical run viewers
- add focused Pest coverage plus Spec 148 artifacts and final polish task completion

## Validation
- `vendor/bin/sail artisan test --compact tests/Unit/Tenants/TenantOperabilityServiceTest.php tests/Unit/Tenants/TenantOperabilityOutcomeTest.php tests/Feature/Workspaces/ChooseTenantPageTest.php tests/Feature/Workspaces/SelectTenantControllerTest.php tests/Feature/TenantRBAC/ArchivedTenantRouteAccessTest.php tests/Feature/TenantRBAC/TenantRouteDenyAsNotFoundTest.php tests/Feature/Operations/TenantlessOperationRunViewerTest.php tests/Feature/OpsUx/OperateHubShellTest.php tests/Feature/Rbac/TenantLifecycleActionVisibilityTest.php tests/Feature/TenantRBAC/TenantSwitcherScopeTest.php tests/Feature/Rbac/TenantResourceAuthorizationTest.php tests/Feature/Filament/ManagedTenantsLandingLifecycleTest.php tests/Feature/Filament/TenantGlobalSearchLifecycleScopeTest.php tests/Feature/Onboarding/OnboardingDraftLifecycleTest.php tests/Feature/Onboarding/OnboardingDraftAuthorizationTest.php`
- `vendor/bin/sail bin pint --dirty --format agent`
- manual browser smoke checks on `/admin/choose-tenant`, `/admin/tenants`, `/admin/onboarding`, `/admin/onboarding/{draft}`, and `/admin/operations/{run}`

## Filament / platform notes
- Livewire v4 compliance preserved
- panel provider registration unchanged in `bootstrap/providers.php`
- Tenant resource global search remains backed by existing view/edit pages and is now separated from active-only selector eligibility
- destructive actions remain action closures with confirmation and authorization enforcement
- no asset pipeline changes and no new `filament:assets` deployment requirement

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #177
2026-03-17 11:48:55 +00:00

169 lines
5.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Support\Tenants;
use App\Models\Tenant;
use BackedEnum;
use Stringable;
enum TenantLifecycle: string
{
case Draft = 'draft';
case Onboarding = 'onboarding';
case Active = 'active';
case Archived = 'archived';
/**
* @return list<string>
*/
public static function values(): array
{
return array_map(
static fn (self $lifecycle): string => $lifecycle->value,
self::cases(),
);
}
public static function fromTenant(Tenant $tenant): self
{
if ($tenant->trashed()) {
return self::Archived;
}
return self::fromValue($tenant->status);
}
public static function fromValue(mixed $value, self $default = self::Active): self
{
return self::tryFromValue($value) ?? $default;
}
public static function tryFromValue(mixed $value): ?self
{
$normalized = self::normalize($value);
return $normalized === null ? null : self::tryFrom($normalized);
}
public static function normalize(mixed $value): ?string
{
if ($value === null) {
return null;
}
if ($value instanceof BackedEnum) {
$value = $value->value;
}
if ($value instanceof Stringable) {
$value = (string) $value;
}
if (! is_string($value)) {
return null;
}
$normalized = strtolower(trim($value));
$normalized = str_replace([' ', '-'], '_', $normalized);
return $normalized !== '' ? $normalized : null;
}
public function label(): string
{
return match ($this) {
self::Draft => 'Draft',
self::Onboarding => 'Onboarding',
self::Active => 'Active',
self::Archived => 'Archived',
};
}
public function isSelectableInStandardLane(): bool
{
return $this === self::Active;
}
public function isAdministrativelyDiscoverable(): bool
{
return true;
}
public function isViewableInLane(TenantInteractionLane $lane): bool
{
return match ($lane) {
TenantInteractionLane::StandardActiveOperating => $this->isSelectableInStandardLane(),
TenantInteractionLane::OnboardingWorkflow => in_array($this, [self::Draft, self::Onboarding], true),
TenantInteractionLane::AdministrativeManagement, TenantInteractionLane::CanonicalWorkspaceRecord => true,
};
}
public function supportsQuestion(TenantOperabilityQuestion $question, TenantInteractionLane $lane): bool
{
return match ($question) {
TenantOperabilityQuestion::SelectorEligibility,
TenantOperabilityQuestion::RememberedContextValidity => $lane === TenantInteractionLane::StandardActiveOperating && $this->isSelectableInStandardLane(),
TenantOperabilityQuestion::TenantBoundViewability => $lane === TenantInteractionLane::AdministrativeManagement,
TenantOperabilityQuestion::CanonicalLinkedRecordViewability => $lane === TenantInteractionLane::CanonicalWorkspaceRecord,
TenantOperabilityQuestion::ArchiveEligibility => $lane === TenantInteractionLane::AdministrativeManagement && $this->canArchive(),
TenantOperabilityQuestion::RestoreEligibility => $lane === TenantInteractionLane::AdministrativeManagement && $this->canRestore(),
TenantOperabilityQuestion::ResumeOnboardingEligibility => in_array($lane, [TenantInteractionLane::AdministrativeManagement, TenantInteractionLane::OnboardingWorkflow], true) && $this->canResumeOnboarding(),
TenantOperabilityQuestion::OnboardingCompletionEligibility => $lane === TenantInteractionLane::OnboardingWorkflow && in_array($this, [self::Draft, self::Onboarding], true),
TenantOperabilityQuestion::VerificationReadinessEligibility => match ($lane) {
TenantInteractionLane::StandardActiveOperating => false,
TenantInteractionLane::OnboardingWorkflow => in_array($this, [self::Draft, self::Onboarding], true),
TenantInteractionLane::AdministrativeManagement => $this === self::Active,
TenantInteractionLane::CanonicalWorkspaceRecord => false,
},
TenantOperabilityQuestion::AdministrativeDiscoverability => $this->isAdministrativelyDiscoverable(),
};
}
public function canSelectAsContext(): bool
{
return $this->isSelectableInStandardLane();
}
public function canViewTenantSurface(): bool
{
return $this->isAdministrativelyDiscoverable();
}
public function canOperate(): bool
{
return $this === self::Active;
}
public function canArchive(): bool
{
return $this === self::Active;
}
public function canRestore(): bool
{
return $this === self::Archived;
}
public function canResumeOnboarding(): bool
{
return in_array($this, [self::Draft, self::Onboarding], true);
}
public function canReferenceInWorkspaceMonitoring(): bool
{
return true;
}
public function allowsManagementAction(string $actionKey): bool
{
return match ($actionKey) {
'resume_onboarding' => $this->canResumeOnboarding(),
'archive' => $this->canArchive(),
'restore' => $this->canRestore(),
default => false,
};
}
}