40 lines
937 B
PHP
40 lines
937 B
PHP
<?php
|
|
|
|
namespace App\Actions\Projects;
|
|
|
|
use App\Actions\Permissions\PermissionService;
|
|
use App\Enums\Permission;
|
|
use App\Models\Project;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class ListVisibleProjects
|
|
{
|
|
public function __construct(
|
|
private readonly PermissionService $permissionService,
|
|
) {}
|
|
|
|
/**
|
|
* @return Collection<int, Project>
|
|
*/
|
|
public function handle(User $user): Collection
|
|
{
|
|
$projectQuery = Project::query()
|
|
->orderByDesc('id');
|
|
|
|
if ($this->permissionService->allows($user, Permission::GlobalAdmin)) {
|
|
return $projectQuery->get();
|
|
}
|
|
|
|
$viewableProjectIds = $this->permissionService->viewableProjectIds($user);
|
|
|
|
if ($viewableProjectIds === []) {
|
|
return new Collection;
|
|
}
|
|
|
|
return $projectQuery
|
|
->whereIn('id', $viewableProjectIds)
|
|
->get();
|
|
}
|
|
}
|