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:
parent
8a715489cb
commit
6a5fd40a5c
6 changed files with 246 additions and 0 deletions
|
|
@ -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 @@
|
|||
|
||||
class UploadController extends BaseController
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
|
||||
public function upload(Request $request)
|
||||
{
|
||||
$databaseIdentifier = request()->route('databaseUuid');
|
||||
|
|
@ -18,6 +21,9 @@ public function upload(Request $request)
|
|||
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 @@ public function forceDelete(User $user, Application $application): bool
|
|||
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 @@ public function manage(User $user, $database): bool
|
|||
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 @@ public function manageBackups(User $user, ServiceDatabase $serviceDatabase): boo
|
|||
{
|
||||
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 @@ public function manageEnvironment(User $user, Service $service): bool
|
|||
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.
|
||||
*/
|
||||
|
|
|
|||
186
tests/Feature/Authorization/UploadBackupAuthorizationTest.php
Normal file
186
tests/Feature/Authorization/UploadBackupAuthorizationTest.php
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Application;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\Project;
|
||||
use App\Models\Server;
|
||||
use App\Models\Service;
|
||||
use App\Models\ServiceDatabase;
|
||||
use App\Models\StandaloneDocker;
|
||||
use App\Models\StandalonePostgresql;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
InstanceSettings::updateOrCreate(['id' => 0]);
|
||||
|
||||
$this->team = Team::factory()->create();
|
||||
|
||||
$this->admin = User::factory()->create();
|
||||
$this->admin->teams()->attach($this->team, ['role' => 'admin']);
|
||||
|
||||
$this->member = User::factory()->create();
|
||||
$this->member->teams()->attach($this->team, ['role' => 'member']);
|
||||
|
||||
$keyId = DB::table('private_keys')->insertGetId([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'name' => 'Test Key',
|
||||
'private_key' => 'test-key',
|
||||
'team_id' => $this->team->id,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$this->server = Server::factory()->create([
|
||||
'team_id' => $this->team->id,
|
||||
'private_key_id' => $keyId,
|
||||
]);
|
||||
|
||||
StandaloneDocker::withoutEvents(function () {
|
||||
$this->destination = StandaloneDocker::firstOrCreate(
|
||||
['server_id' => $this->server->id, 'network' => 'coolify'],
|
||||
['uuid' => (string) Str::uuid(), 'name' => 'test-docker']
|
||||
);
|
||||
});
|
||||
|
||||
$this->project = Project::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'name' => 'Test Project',
|
||||
'team_id' => $this->team->id,
|
||||
]);
|
||||
|
||||
$this->environment = $this->project->environments()->first();
|
||||
|
||||
$this->database = StandalonePostgresql::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'name' => 'Test DB',
|
||||
'postgres_user' => 'postgres',
|
||||
'postgres_password' => 'password',
|
||||
'postgres_db' => 'testdb',
|
||||
'image' => 'postgres:15',
|
||||
'status' => 'running',
|
||||
'environment_id' => $this->environment->id,
|
||||
'destination_id' => $this->destination->id,
|
||||
'destination_type' => $this->destination->getMorphClass(),
|
||||
]);
|
||||
|
||||
$this->application = Application::factory()->create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'name' => 'Test App',
|
||||
'environment_id' => $this->environment->id,
|
||||
'destination_id' => $this->destination->id,
|
||||
'destination_type' => $this->destination->getMorphClass(),
|
||||
'status' => 'running',
|
||||
]);
|
||||
|
||||
$this->service = Service::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'name' => 'test-service',
|
||||
'environment_id' => $this->environment->id,
|
||||
'destination_id' => $this->destination->id,
|
||||
'destination_type' => $this->destination->getMorphClass(),
|
||||
'docker_compose_raw' => 'version: "3"',
|
||||
]);
|
||||
|
||||
$this->serviceDatabase = ServiceDatabase::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'name' => 'test-service-db',
|
||||
'service_id' => $this->service->id,
|
||||
]);
|
||||
});
|
||||
|
||||
// --- DatabasePolicy::uploadBackup (covers Standalone* databases) ---
|
||||
|
||||
test('admin can upload backup for standalone database', function () {
|
||||
expect($this->admin->can('uploadBackup', $this->database))->toBeTrue();
|
||||
});
|
||||
|
||||
test('member cannot upload backup for standalone database', function () {
|
||||
expect($this->member->can('uploadBackup', $this->database))->toBeFalse();
|
||||
});
|
||||
|
||||
// --- ApplicationPolicy::uploadBackup ---
|
||||
|
||||
test('admin can upload backup for application', function () {
|
||||
expect($this->admin->can('uploadBackup', $this->application))->toBeTrue();
|
||||
});
|
||||
|
||||
test('member cannot upload backup for application', function () {
|
||||
expect($this->member->can('uploadBackup', $this->application))->toBeFalse();
|
||||
});
|
||||
|
||||
// --- ServicePolicy::uploadBackup ---
|
||||
|
||||
test('admin can upload backup for service', function () {
|
||||
expect($this->admin->can('uploadBackup', $this->service))->toBeTrue();
|
||||
});
|
||||
|
||||
test('member cannot upload backup for service', function () {
|
||||
expect($this->member->can('uploadBackup', $this->service))->toBeFalse();
|
||||
});
|
||||
|
||||
// --- ServiceDatabasePolicy::uploadBackup (delegates to ServicePolicy) ---
|
||||
|
||||
test('admin can upload backup for service database', function () {
|
||||
expect($this->admin->can('uploadBackup', $this->serviceDatabase))->toBeTrue();
|
||||
});
|
||||
|
||||
test('member cannot upload backup for service database', function () {
|
||||
expect($this->member->can('uploadBackup', $this->serviceDatabase))->toBeFalse();
|
||||
});
|
||||
|
||||
// --- Cross-team isolation ---
|
||||
|
||||
test('user from different team cannot upload backup', function () {
|
||||
$otherTeam = Team::factory()->create();
|
||||
$otherUser = User::factory()->create();
|
||||
$otherUser->teams()->attach($otherTeam, ['role' => 'admin']);
|
||||
|
||||
expect($otherUser->can('uploadBackup', $this->database))->toBeFalse();
|
||||
expect($otherUser->can('uploadBackup', $this->application))->toBeFalse();
|
||||
expect($otherUser->can('uploadBackup', $this->service))->toBeFalse();
|
||||
expect($otherUser->can('uploadBackup', $this->serviceDatabase))->toBeFalse();
|
||||
});
|
||||
|
||||
// --- HTTP endpoint: POST /upload/backup/{uuid} ---
|
||||
|
||||
test('member gets 403 from POST /upload/backup and no file lands on disk', function () {
|
||||
$uploadDir = storage_path('app/upload/'.$this->database->uuid);
|
||||
if (File::exists($uploadDir)) {
|
||||
File::deleteDirectory($uploadDir);
|
||||
}
|
||||
|
||||
$this->actingAs($this->member);
|
||||
session(['currentTeam' => $this->team]);
|
||||
|
||||
$response = $this->post(route('upload.backup', ['databaseUuid' => $this->database->uuid]));
|
||||
|
||||
$response->assertForbidden();
|
||||
|
||||
expect(File::exists($uploadDir.'/restore'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('user from different team hits null-resource branch with 500', function () {
|
||||
$otherTeam = Team::factory()->create();
|
||||
$otherUser = User::factory()->create();
|
||||
$otherUser->teams()->attach($otherTeam, ['role' => 'admin']);
|
||||
|
||||
$this->actingAs($otherUser);
|
||||
session(['currentTeam' => $otherTeam]);
|
||||
|
||||
$response = $this->post(route('upload.backup', ['databaseUuid' => $this->database->uuid]));
|
||||
|
||||
$response->assertStatus(500);
|
||||
});
|
||||
|
||||
test('unauthenticated request is redirected to login', function () {
|
||||
$response = $this->post(route('upload.backup', ['databaseUuid' => $this->database->uuid]));
|
||||
|
||||
$response->assertRedirect('/login');
|
||||
});
|
||||
Loading…
Reference in a new issue