- Refactoring

- Download restriction
This commit is contained in:
Čarodej
2022-01-05 18:29:07 +01:00
parent ec29764c3f
commit 29a954e21b
15 changed files with 400 additions and 176 deletions

View File

@@ -1,30 +0,0 @@
<?php
namespace App\Limitations;
use Illuminate\Support\Manager;
use App\Limitations\Engines\DefaultLimitationEngine;
use App\Limitations\Engines\FixedBillingLimitationEngine;
use App\Limitations\Engines\MeteredBillingLimitationEngine;
class LimitationManager extends Manager
{
public function getDefaultDriver(): string
{
return get_limitation_driver();
}
public function createDefaultDriver(): DefaultLimitationEngine
{
return new DefaultLimitationEngine();
}
public function createFixedDriver(): FixedBillingLimitationEngine
{
return new FixedBillingLimitationEngine();
}
public function createMeteredDriver(): MeteredBillingLimitationEngine
{
return new MeteredBillingLimitationEngine();
}
}

View File

@@ -1,11 +1,11 @@
<?php
namespace App\Limitations\Engines;
namespace App\Restrictions\Engines;
use App\Users\Models\User;
use App\Limitations\LimitationEngine;
use App\Restrictions\RestrictionsEngine;
use Domain\Teams\Actions\CheckMaxTeamMembersLimitAction;
class DefaultLimitationEngine implements LimitationEngine
class DefaultRestrictionsEngine implements RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool
{

View File

@@ -1,11 +1,11 @@
<?php
namespace App\Limitations\Engines;
namespace App\Restrictions\Engines;
use App\Users\Models\User;
use App\Limitations\LimitationEngine;
use App\Restrictions\RestrictionsEngine;
use Domain\Teams\Actions\CheckMaxTeamMembersLimitAction;
class FixedBillingLimitationEngine implements LimitationEngine
class FixedBillingRestrictionsEngine implements RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool
{

View File

@@ -1,10 +1,10 @@
<?php
namespace App\Limitations\Engines;
namespace App\Restrictions\Engines;
use App\Users\Models\User;
use App\Limitations\LimitationEngine;
use App\Restrictions\RestrictionsEngine;
class MeteredBillingLimitationEngine implements LimitationEngine
class MeteredBillingRestrictionsEngine implements RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool
{

View File

@@ -1,9 +1,9 @@
<?php
namespace App\Limitations;
namespace App\Restrictions;
use App\Users\Models\User;
interface LimitationEngine
interface RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool;

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Restrictions;
use Illuminate\Support\Manager;
use App\Restrictions\Engines\DefaultRestrictionsEngine;
use App\Restrictions\Engines\FixedBillingRestrictionsEngine;
use App\Restrictions\Engines\MeteredBillingRestrictionsEngine;
class RestrictionsManager extends Manager
{
public function getDefaultDriver(): string
{
return get_restriction_driver();
}
public function createDefaultDriver(): DefaultRestrictionsEngine
{
return new DefaultRestrictionsEngine();
}
public function createFixedDriver(): FixedBillingRestrictionsEngine
{
return new FixedBillingRestrictionsEngine();
}
public function createMeteredDriver(): MeteredBillingRestrictionsEngine
{
return new MeteredBillingRestrictionsEngine();
}
}

View File

@@ -12,9 +12,9 @@ use Illuminate\Support\Facades\DB;
use Database\Factories\UserFactory;
use Domain\Settings\Models\Setting;
use Kyslik\ColumnSortable\Sortable;
use App\Limitations\LimitationManager;
use Illuminate\Support\Facades\Storage;
use Illuminate\Notifications\Notifiable;
use App\Restrictions\RestrictionsManager;
use App\Users\Notifications\ResetPassword;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
@@ -203,7 +203,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function __call($method, $parameters)
{
if (str_starts_with($method, 'can')) {
return resolve(LimitationManager::class)
return resolve(RestrictionsManager::class)
->driver()
->$method($this, ...$parameters);
}

View File

@@ -2,6 +2,7 @@
namespace Domain\Files\Controllers\FileAccess;
use Gate;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use Domain\Files\Models\File as UserFile;
use Domain\Files\Actions\DownloadFileAction;
@@ -18,11 +19,19 @@ class GetFileController extends Controller
public function __invoke(
string $filename,
): BinaryFileResponse {
): Response|BinaryFileResponse {
$file = UserFile::withTrashed()
->where('basename', $filename)
->firstOrFail();
// Check if user can download file
if (! $file->owner->canDownload()) {
return response([
'type' => 'error',
'message' => 'This user action is not allowed.',
], 401);
}
if (! Gate::any(['can-edit', 'can-view'], [$file, null])) {
abort(403, 'Access Denied');
}

View File

@@ -2,6 +2,7 @@
namespace Domain\Files\Controllers\FileAccess;
use Domain\Files\Models\File;
use Illuminate\Http\Response;
use Domain\Sharing\Models\Share;
use App\Http\Controllers\Controller;
use Domain\Files\Actions\DownloadFileAction;
@@ -26,7 +27,15 @@ class VisitorGetFileController extends Controller
public function __invoke(
$filename,
Share $shared,
): BinaryFileResponse {
): BinaryFileResponse|Response {
// Check if user can download file
if (! $shared->user->canDownload()) {
return response([
'type' => 'error',
'message' => 'This user action is not allowed.',
], 401);
}
// Check ability to access protected share files
($this->protectShareRecord)($shared);

View File

@@ -33,11 +33,11 @@ if (! function_exists('obfuscate_email')) {
}
}
if (! function_exists('get_limitation_driver')) {
if (! function_exists('get_restriction_driver')) {
/**
* Get driver for limitation API
*/
function get_limitation_driver(): string
function get_restriction_driver(): string
{
return match (get_settings('subscription_type')) {
'fixed' => 'fixed',

View File

@@ -1,112 +0,0 @@
<?php
namespace Tests\App\Limitations;
use Tests\TestCase;
use App\Users\Models\User;
use Illuminate\Support\Facades\DB;
class MeteredBillingLimitationTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
DB::table('settings')->insert([
'name' => 'subscription_type',
'value' => 'metered',
]);
}
/**
* @test
*/
public function it_can_upload()
{
$user = User::factory()
->hasFailedpayments(2)
->create();
$this->assertEquals(true, $user->canUpload());
}
/**
* @test
*/
public function it_cant_upload_because_user_has_3_failed_payments()
{
$user = User::factory()
->hasFailedpayments(3)
->create();
$this->assertEquals(false, $user->canUpload());
}
/**
* @test
*/
public function it_can_create_new_folder()
{
$user = User::factory()
->create();
// Create basic folder
$this
->actingAs($user)
->postJson('/api/create-folder', [
'name' => 'New Folder',
])
->assertStatus(201);
// Create team folder
$this
->actingAs($user)
->postJson('/api/teams/folders', [
'name' => 'New Team Folder',
'invitations' => [
[
'email' => 'john@doe.com',
'permission' => 'can-edit',
'type' => 'invitation',
],
],
])
->assertStatus(201);
$this->assertDatabaseCount('folders', 2);
}
/**
* @test
*/
public function it_cant_create_new_folder_because_user_has_3_failed_payments()
{
$user = User::factory()
->hasFailedpayments(3)
->create();
// Create basic folder
$this
->actingAs($user)
->postJson('/api/create-folder', [
'name' => 'New Folder',
])
->assertStatus(401);
// Create team folder
$this
->actingAs($user)
->postJson('/api/teams/folders', [
'name' => 'New Folder',
'invitations' => [
[
'email' => 'john@doe.com',
'permission' => 'can-edit',
'type' => 'invitation',
],
],
])
->assertStatus(401);
$this->assertDatabaseCount('folders', 0);
}
}

View File

@@ -1,13 +1,14 @@
<?php
namespace Tests\App\Limitations;
namespace Tests\App\Restrictions;
use Tests\TestCase;
use App\Users\Models\User;
use Domain\Files\Models\File;
use Domain\Sharing\Models\Share;
use Domain\Settings\Models\Setting;
use Domain\Teams\Models\TeamFolderMember;
class DefaultLimitationTest extends TestCase
class DefaultRestrictionsTest extends TestCase
{
/**
* @test
@@ -130,10 +131,10 @@ class DefaultLimitationTest extends TestCase
->create()
->each(
fn ($member) => TeamFolderMember::factory()
->create([
'parent_id' => $user->folders[0]->id,
'user_id' => $member->id,
])
->create([
'parent_id' => $user->folders[0]->id,
'user_id' => $member->id,
])
);
// Try invite new members, it has to fail
@@ -184,4 +185,54 @@ class DefaultLimitationTest extends TestCase
])
->assertCreated();
}
/**
* @test
*/
public function it_can_get_private_file()
{
$user = User::factory()
->create();
$file = File::factory()
->create([
'user_id' => $user->id,
'basename' => 'fake-file.pdf',
'name' => 'fake-file.pdf',
]);
// 404 but, ok, because there is not stored temporary file in test
$this
->actingAs($user)
->get("file/$file->name")
->assertStatus(404);
}
/**
* @test
*/
public function it_can_get_shared_file()
{
$user = User::factory()
->create();
$file = File::factory()
->create([
'basename' => 'fake-file.pdf',
'name' => 'fake-file.pdf',
]);
$share = Share::factory()
->create([
'item_id' => $file->id,
'user_id' => $user->id,
'type' => 'file',
'is_protected' => false,
]);
// 404 but, ok, because there is not stored temporary file in test
$this
->get("file/$file->name/$share->token")
->assertStatus(404);
}
}

View File

@@ -1,13 +1,14 @@
<?php
namespace Tests\App\Limitations;
namespace Tests\App\Restrictions;
use Tests\TestCase;
use App\Users\Models\User;
use Domain\Files\Models\File;
use Domain\Sharing\Models\Share;
use Domain\Settings\Models\Setting;
use Domain\Teams\Models\TeamFolderMember;
class FixedBillingLimitationTest extends TestCase
class FixedBillingRestrictionsTest extends TestCase
{
public function setUp(): void
{
@@ -91,10 +92,10 @@ class FixedBillingLimitationTest extends TestCase
->create()
->each(
fn ($member) => TeamFolderMember::factory()
->create([
'parent_id' => $user->folders[0]->id,
'user_id' => $member->id,
])
->create([
'parent_id' => $user->folders[0]->id,
'user_id' => $member->id,
])
);
// Try invite new members, it has to fail
@@ -145,4 +146,54 @@ class FixedBillingLimitationTest extends TestCase
])
->assertCreated();
}
/**
* @test
*/
public function it_can_get_private_file()
{
$user = User::factory()
->create();
$file = File::factory()
->create([
'user_id' => $user->id,
'basename' => 'fake-file.pdf',
'name' => 'fake-file.pdf',
]);
// 404 but, ok, because there is not stored temporary file in test
$this
->actingAs($user)
->get("file/$file->name")
->assertStatus(404);
}
/**
* @test
*/
public function it_can_get_shared_file()
{
$user = User::factory()
->create();
$file = File::factory()
->create([
'basename' => 'fake-file.pdf',
'name' => 'fake-file.pdf',
]);
$share = Share::factory()
->create([
'item_id' => $file->id,
'user_id' => $user->id,
'type' => 'file',
'is_protected' => false,
]);
// 404 but, ok, because there is not stored temporary file in test
$this
->get("file/$file->name/$share->token")
->assertStatus(404);
}
}

View File

@@ -0,0 +1,216 @@
<?php
namespace Tests\App\Restrictions;
use Tests\TestCase;
use App\Users\Models\User;
use Domain\Files\Models\File;
use Domain\Sharing\Models\Share;
use Illuminate\Support\Facades\DB;
class MeteredBillingRestrictionsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
DB::table('settings')->insert([
'name' => 'subscription_type',
'value' => 'metered',
]);
}
/**
* @test
*/
public function it_can_upload()
{
$user = User::factory()
->hasFailedpayments(2)
->create();
$this->assertEquals(true, $user->canUpload());
}
/**
* @test
*/
public function it_cant_upload_because_user_has_3_failed_payments()
{
$user = User::factory()
->hasFailedpayments(3)
->create();
$this->assertEquals(false, $user->canUpload());
}
/**
* @test
*/
public function it_can_create_new_folder()
{
$user = User::factory()
->create();
// Create basic folder
$this
->actingAs($user)
->postJson('/api/create-folder', [
'name' => 'New Folder',
])
->assertStatus(201);
// Create team folder
$this
->actingAs($user)
->postJson('/api/teams/folders', [
'name' => 'New Team Folder',
'invitations' => [
[
'email' => 'john@doe.com',
'permission' => 'can-edit',
'type' => 'invitation',
],
],
])
->assertStatus(201);
$this->assertDatabaseCount('folders', 2);
}
/**
* @test
*/
public function it_cant_create_new_folder_because_user_has_3_failed_payments()
{
$user = User::factory()
->hasFailedpayments(3)
->create();
// Create basic folder
$this
->actingAs($user)
->postJson('/api/create-folder', [
'name' => 'New Folder',
])
->assertStatus(401);
// Create team folder
$this
->actingAs($user)
->postJson('/api/teams/folders', [
'name' => 'New Folder',
'invitations' => [
[
'email' => 'john@doe.com',
'permission' => 'can-edit',
'type' => 'invitation',
],
],
])
->assertStatus(401);
$this->assertDatabaseCount('folders', 0);
}
/**
* @test
*/
public function it_cant_get_private_file_because_user_has_3_failed_payments()
{
$user = User::factory()
->hasFailedpayments(3)
->create();
$file = File::factory()
->create([
'user_id' => $user->id,
'basename' => 'fake-file.pdf',
'name' => 'fake-file.pdf',
]);
$this
->actingAs($user)
->get("file/$file->name")
->assertStatus(401);
}
/**
* @test
*/
public function it_can_get_private_file()
{
$user = User::factory()
->create();
$file = File::factory()
->create([
'user_id' => $user->id,
'basename' => 'fake-file.pdf',
'name' => 'fake-file.pdf',
]);
// 404 but, ok, because there is not stored temporary file in test
$this
->actingAs($user)
->get("file/$file->name")
->assertStatus(404);
}
/**
* @test
*/
public function it_cant_get_shared_file_because_user_has_3_failed_payments()
{
$user = User::factory()
->hasFailedpayments(3)
->create();
$file = File::factory()
->create([
'user_id' => $user->id,
'basename' => 'fake-file.pdf',
'name' => 'fake-file.pdf',
]);
$share = Share::factory()
->create([
'item_id' => $file->id,
'user_id' => $user->id,
'type' => 'file',
'is_protected' => false,
]);
$this
->get("file/$file->name/$share->token")
->assertStatus(401);
}
/**
* @test
*/
public function it_can_get_shared_file()
{
$user = User::factory()
->create();
$file = File::factory()
->create([
'user_id' => $user->id,
'basename' => 'fake-file.pdf',
'name' => 'fake-file.pdf',
]);
$share = Share::factory()
->create([
'item_id' => $file->id,
'user_id' => $user->id,
'type' => 'file',
'is_protected' => false,
]);
// 404 but, ok, because there is not stored temporary file in test
$this
->get("file/$file->name/$share->token")
->assertStatus(404);
}
}

View File

@@ -1,10 +1,10 @@
<?php
namespace Tests\App\Limitations;
namespace Tests\App\Restrictions;
use Tests\TestCase;
use Domain\Settings\Models\Setting;
class LimitationTest extends TestCase
class RestrictionsTest extends TestCase
{
/**
* @test
@@ -17,7 +17,7 @@ class LimitationTest extends TestCase
'value' => 'metered',
]);
$this->assertEquals('metered', get_limitation_driver());
$this->assertEquals('metered', get_restriction_driver());
}
/**
* @test
@@ -30,7 +30,7 @@ class LimitationTest extends TestCase
'value' => 'fixed',
]);
$this->assertEquals('fixed', get_limitation_driver());
$this->assertEquals('fixed', get_restriction_driver());
}
/**
* @test
@@ -42,6 +42,6 @@ class LimitationTest extends TestCase
$subscriptionType?->delete();
$this->assertEquals('default', get_limitation_driver());
$this->assertEquals('default', get_restriction_driver());
}
}