mirror of
https://github.com/tiennm99/coolify.git
synced 2026-08-21 06:26:22 +00:00
fix: improve team resource route handling
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http\Middleware;
|
||||
use App\Models\Application;
|
||||
use App\Models\Environment;
|
||||
use App\Models\Project;
|
||||
use App\Models\Server;
|
||||
use App\Models\Service;
|
||||
use App\Models\ServiceApplication;
|
||||
use App\Models\ServiceDatabase;
|
||||
@@ -23,53 +24,61 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class CanUpdateResource
|
||||
{
|
||||
/**
|
||||
* @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],
|
||||
];
|
||||
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$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.');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Get resource from route parameters
|
||||
// $resource = null;
|
||||
// if ($request->route('application_uuid')) {
|
||||
// $resource = Application::where('uuid', $request->route('application_uuid'))->first();
|
||||
// } elseif ($request->route('service_uuid')) {
|
||||
// $resource = Service::where('uuid', $request->route('service_uuid'))->first();
|
||||
// } elseif ($request->route('stack_service_uuid')) {
|
||||
// // Handle ServiceApplication or ServiceDatabase
|
||||
// $stack_service_uuid = $request->route('stack_service_uuid');
|
||||
// $resource = ServiceApplication::where('uuid', $stack_service_uuid)->first() ??
|
||||
// ServiceDatabase::where('uuid', $stack_service_uuid)->first();
|
||||
// } elseif ($request->route('database_uuid')) {
|
||||
// // Try different database types
|
||||
// $database_uuid = $request->route('database_uuid');
|
||||
// $resource = StandalonePostgresql::where('uuid', $database_uuid)->first() ??
|
||||
// StandaloneMysql::where('uuid', $database_uuid)->first() ??
|
||||
// StandaloneMariadb::where('uuid', $database_uuid)->first() ??
|
||||
// StandaloneRedis::where('uuid', $database_uuid)->first() ??
|
||||
// StandaloneKeydb::where('uuid', $database_uuid)->first() ??
|
||||
// StandaloneDragonfly::where('uuid', $database_uuid)->first() ??
|
||||
// StandaloneClickhouse::where('uuid', $database_uuid)->first() ??
|
||||
// StandaloneMongodb::where('uuid', $database_uuid)->first();
|
||||
// } elseif ($request->route('server_uuid')) {
|
||||
// // For server routes, check if user can manage servers
|
||||
// if (! auth()->user()->isAdmin()) {
|
||||
// abort(403, 'You do not have permission to access this resource.');
|
||||
// }
|
||||
private function resourceFromRoute(Request $request): ?object
|
||||
{
|
||||
foreach (self::ROUTE_RESOURCE_MODELS as $routeParameter => $models) {
|
||||
$uuid = $request->route($routeParameter);
|
||||
|
||||
// return $next($request);
|
||||
// } elseif ($request->route('environment_uuid')) {
|
||||
// $resource = Environment::where('uuid', $request->route('environment_uuid'))->first();
|
||||
// } elseif ($request->route('project_uuid')) {
|
||||
// $resource = Project::ownedByCurrentTeam()->where('uuid', $request->route('project_uuid'))->first();
|
||||
// }
|
||||
if (! $uuid) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// if (! $resource) {
|
||||
// abort(404, 'Resource not found.');
|
||||
// }
|
||||
foreach ($models as $model) {
|
||||
$resource = $model::where('uuid', $uuid)->first();
|
||||
|
||||
// if (! Gate::allows('update', $resource)) {
|
||||
// abort(403, 'You do not have permission to update this resource.');
|
||||
// }
|
||||
if ($resource) {
|
||||
return $resource;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return $next($request);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user