## Summary - replace the legacy Tenant and TenantMembership core models with ManagedEnvironment and ManagedEnvironmentMembership - propagate the managed environment naming and key changes across Filament resources, pages, controllers, jobs, models, and supporting runtime paths - add feature 279 spec artifacts and focused managed-environment test coverage for model behavior, route binding, panel context, authorization, and legacy guardrails ## Validation - `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/ManagedEnvironment/LegacyTenantCoreGuardTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentAuthorizationTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentPanelContextTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentRouteBindingTest.php tests/Unit/ManagedEnvironment/ManagedEnvironmentContextResolverTest.php tests/Unit/ManagedEnvironment/ManagedEnvironmentModelTest.php` - `cd apps/platform && ./vendor/bin/sail bin pint --dirty --format agent` ## Notes - branch pushed from commit `1123b122` - browser smoke test file was added but not run in this pass Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #335
105 lines
3.8 KiB
PHP
105 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\TenantReviews;
|
|
|
|
use App\Models\ManagedEnvironment;
|
|
use App\Models\TenantReview;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use App\Models\WorkspaceMembership;
|
|
use App\Services\Auth\RoleCapabilityMap;
|
|
use App\Support\Auth\Capabilities;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class TenantReviewRegisterService
|
|
{
|
|
/**
|
|
* @return array<int, ManagedEnvironment>
|
|
*/
|
|
public function authorizedTenants(User $user, Workspace $workspace): array
|
|
{
|
|
$roles = RoleCapabilityMap::rolesWithCapability(Capabilities::TENANT_REVIEW_VIEW);
|
|
|
|
return $user->tenants()
|
|
->where('managed_environments.workspace_id', (int) $workspace->getKey())
|
|
->wherePivotIn('role', $roles)
|
|
->orderBy('managed_environments.name')
|
|
->get()
|
|
->keyBy(static fn (ManagedEnvironment $tenant): int => (int) $tenant->getKey())
|
|
->all();
|
|
}
|
|
|
|
public function query(User $user, Workspace $workspace): Builder
|
|
{
|
|
$tenantIds = array_keys($this->authorizedTenants($user, $workspace));
|
|
|
|
return TenantReview::query()
|
|
->with(['tenant', 'evidenceSnapshot', 'currentExportReviewPack'])
|
|
->forWorkspace((int) $workspace->getKey())
|
|
->whereIn('managed_environment_id', $tenantIds === [] ? [-1] : $tenantIds)
|
|
->latest('generated_at')
|
|
->latest('id');
|
|
}
|
|
|
|
public function latestPublishedQuery(User $user, Workspace $workspace): Builder
|
|
{
|
|
$tenantIds = array_keys($this->authorizedTenants($user, $workspace));
|
|
|
|
$rankedReviews = TenantReview::query()
|
|
->select([
|
|
'tenant_reviews.id',
|
|
'tenant_reviews.managed_environment_id',
|
|
'tenant_reviews.published_at',
|
|
'tenant_reviews.generated_at',
|
|
])
|
|
->selectRaw('ROW_NUMBER() OVER (PARTITION BY managed_environment_id ORDER BY published_at DESC, generated_at DESC, id DESC) as rn')
|
|
->forWorkspace((int) $workspace->getKey())
|
|
->whereIn('managed_environment_id', $tenantIds === [] ? [-1] : $tenantIds)
|
|
->published();
|
|
|
|
$latestPublishedIds = DB::query()
|
|
->fromSub($rankedReviews, 'ranked_tenant_reviews')
|
|
->where('rn', 1)
|
|
->select('id');
|
|
|
|
return TenantReview::query()
|
|
->with(['tenant', 'evidenceSnapshot', 'currentExportReviewPack'])
|
|
->forWorkspace((int) $workspace->getKey())
|
|
->whereIn('tenant_reviews.id', $latestPublishedIds)
|
|
->orderByDesc('published_at')
|
|
->orderByDesc('generated_at')
|
|
->orderByDesc('id');
|
|
}
|
|
|
|
public function customerWorkspaceTenantQuery(User $user, Workspace $workspace): Builder
|
|
{
|
|
$tenantIds = array_keys($this->authorizedTenants($user, $workspace));
|
|
|
|
return ManagedEnvironment::query()
|
|
->where('workspace_id', (int) $workspace->getKey())
|
|
->whereIn('id', $tenantIds === [] ? [-1] : $tenantIds)
|
|
->whereHas('tenantReviews', fn ($query) => $query->published())
|
|
->with([
|
|
'tenantReviews' => fn ($query) => $query
|
|
->with(['tenant', 'evidenceSnapshot', 'currentExportReviewPack'])
|
|
->published()
|
|
->orderByDesc('published_at')
|
|
->orderByDesc('generated_at')
|
|
->orderByDesc('id')
|
|
->limit(1),
|
|
])
|
|
->orderBy('name');
|
|
}
|
|
|
|
public function canAccessWorkspace(User $user, Workspace $workspace): bool
|
|
{
|
|
return WorkspaceMembership::query()
|
|
->where('workspace_id', (int) $workspace->getKey())
|
|
->where('user_id', (int) $user->getKey())
|
|
->exists();
|
|
}
|
|
}
|