mirror of
https://github.com/tiennm99/coolify.git
synced 2026-10-15 10:14:47 +00:00
refactor(policies): add uploadBackup ability and enforce it on backup upload endpoint
Introduce a dedicated `uploadBackup` ability on Application, Database,
Service, and ServiceDatabase policies (admin/owner only) and call
`$this->authorize('uploadBackup', $resource)` in `UploadController::upload`
so the backup-upload endpoint goes through the same policy layer as the
rest of the authorization refactor. Adds Pest coverage for each policy
variant plus HTTP-level checks.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
8a715489cb
commit
6a5fd40a5c
6 files changed
+246
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
@@ -11,6 +12,8 @@ use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
|
||||
|
||||
class UploadController extends BaseController
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
|
||||
public function upload(Request $request)
|
||||
{
|
||||
$databaseIdentifier = request()->route('databaseUuid');
|
||||
@@ -18,6 +21,9 @@ class UploadController extends BaseController
|
||||
if (is_null($resource)) {
|
||||
return response()->json(['error' => 'You do not have permission for this database'], 500);
|
||||
}
|
||||
|
||||
$this->authorize('uploadBackup', $resource);
|
||||
|
||||
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
|
||||
|
||||
if ($receiver->isUploaded() === false) {
|
||||
|
||||
@@ -78,6 +78,24 @@ class ApplicationPolicy
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can upload a backup archive for this application.
|
||||
*/
|
||||
public function uploadBackup(User $user, Application $application): Response
|
||||
{
|
||||
$teamId = $this->getTeamId($application);
|
||||
|
||||
if ($teamId === null) {
|
||||
return Response::deny('Application team not found.');
|
||||
}
|
||||
|
||||
if ($user->isAdminOfTeam($teamId)) {
|
||||
return Response::allow();
|
||||
}
|
||||
|
||||
return Response::deny('You need at least admin or owner permissions to upload backups for this application.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can deploy the application.
|
||||
*/
|
||||
|
||||
@@ -87,6 +87,24 @@ class DatabasePolicy
|
||||
return $teamId !== null && $user->isAdminOfTeam($teamId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can upload a backup archive for this database.
|
||||
*/
|
||||
public function uploadBackup(User $user, $database): Response
|
||||
{
|
||||
$teamId = $this->getTeamId($database);
|
||||
|
||||
if ($teamId === null) {
|
||||
return Response::deny('Database team not found.');
|
||||
}
|
||||
|
||||
if ($user->isAdminOfTeam($teamId)) {
|
||||
return Response::allow();
|
||||
}
|
||||
|
||||
return Response::deny('You need at least admin or owner permissions to upload backups for this database.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can manage database backups.
|
||||
*/
|
||||
|
||||
@@ -63,4 +63,12 @@ class ServiceDatabasePolicy
|
||||
{
|
||||
return Gate::allows('update', $serviceDatabase->service);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can upload a backup archive for this service database.
|
||||
*/
|
||||
public function uploadBackup(User $user, ServiceDatabase $serviceDatabase): bool
|
||||
{
|
||||
return Gate::allows('uploadBackup', $serviceDatabase->service);
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,16 @@ class ServicePolicy
|
||||
return $teamId !== null && $user->isAdminOfTeam($teamId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can upload a backup archive for a database within this service.
|
||||
*/
|
||||
public function uploadBackup(User $user, Service $service): bool
|
||||
{
|
||||
$teamId = $this->getTeamId($service);
|
||||
|
||||
return $teamId !== null && $user->isAdminOfTeam($teamId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can deploy the service.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user