2025-08-26 08:27:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
|
|
use App\Models\Application;
|
|
|
|
|
use App\Models\Environment;
|
|
|
|
|
use App\Models\Project;
|
2026-07-02 11:05:27 +00:00
|
|
|
use App\Models\Server;
|
2025-08-26 08:27:31 +00:00
|
|
|
use App\Models\Service;
|
|
|
|
|
use App\Models\ServiceApplication;
|
|
|
|
|
use App\Models\ServiceDatabase;
|
|
|
|
|
use App\Models\StandaloneClickhouse;
|
|
|
|
|
use App\Models\StandaloneDragonfly;
|
|
|
|
|
use App\Models\StandaloneKeydb;
|
|
|
|
|
use App\Models\StandaloneMariadb;
|
|
|
|
|
use App\Models\StandaloneMongodb;
|
|
|
|
|
use App\Models\StandaloneMysql;
|
|
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
|
use App\Models\StandaloneRedis;
|
|
|
|
|
use Closure;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
|
|
class CanUpdateResource
|
|
|
|
|
{
|
2026-07-02 11:05:27 +00:00
|
|
|
/**
|
|
|
|
|
* @var array<string, list<class-string>>
|
|
|
|
|
*/
|
|
|
|
|
private const ROUTE_RESOURCE_MODELS = [
|
|
|
|
|
'application_uuid' => [Application::class],
|
|
|
|
|
'database_uuid' => [
|
|
|
|
|
StandalonePostgresql::class,
|
|
|
|
|
StandaloneMysql::class,
|
|
|
|
|
StandaloneMariadb::class,
|
|
|
|
|
StandaloneRedis::class,
|
|
|
|
|
StandaloneKeydb::class,
|
|
|
|
|
StandaloneDragonfly::class,
|
|
|
|
|
StandaloneClickhouse::class,
|
|
|
|
|
StandaloneMongodb::class,
|
|
|
|
|
],
|
|
|
|
|
'stack_service_uuid' => [ServiceApplication::class, ServiceDatabase::class],
|
|
|
|
|
'service_uuid' => [Service::class],
|
|
|
|
|
'server_uuid' => [Server::class],
|
|
|
|
|
'environment_uuid' => [Environment::class],
|
|
|
|
|
'project_uuid' => [Project::class],
|
|
|
|
|
];
|
|
|
|
|
|
2025-08-26 08:27:31 +00:00
|
|
|
public function handle(Request $request, Closure $next): Response
|
|
|
|
|
{
|
2026-07-02 11:05:27 +00:00
|
|
|
$resource = $this->resourceFromRoute($request);
|
|
|
|
|
|
|
|
|
|
if (! $resource) {
|
|
|
|
|
abort(404, 'Resource not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! Gate::allows('update', $resource)) {
|
|
|
|
|
abort(403, 'You do not have permission to update this resource.');
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 08:27:31 +00:00
|
|
|
return $next($request);
|
2026-07-02 11:05:27 +00:00
|
|
|
}
|
2025-08-26 08:27:31 +00:00
|
|
|
|
2026-07-02 11:05:27 +00:00
|
|
|
private function resourceFromRoute(Request $request): ?object
|
|
|
|
|
{
|
|
|
|
|
foreach (self::ROUTE_RESOURCE_MODELS as $routeParameter => $models) {
|
|
|
|
|
$uuid = $request->route($routeParameter);
|
2025-08-26 08:27:31 +00:00
|
|
|
|
2026-07-02 11:05:27 +00:00
|
|
|
if (! $uuid) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-08-26 08:27:31 +00:00
|
|
|
|
2026-07-02 11:05:27 +00:00
|
|
|
foreach ($models as $model) {
|
|
|
|
|
$resource = $model::where('uuid', $uuid)->first();
|
2025-08-26 08:27:31 +00:00
|
|
|
|
2026-07-02 11:05:27 +00:00
|
|
|
if ($resource) {
|
|
|
|
|
return $resource;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-26 08:27:31 +00:00
|
|
|
|
2026-07-02 11:05:27 +00:00
|
|
|
return null;
|
2025-08-26 08:27:31 +00:00
|
|
|
}
|
|
|
|
|
}
|