Frontend upload restrict consolidation

This commit is contained in:
Čarodej
2022-01-06 12:24:27 +01:00
parent 8d53ed1531
commit e5e713659e
13 changed files with 84 additions and 108 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ use Domain\Settings\Models\Setting;
use Kyslik\ColumnSortable\Sortable;
use Illuminate\Support\Facades\Storage;
use Illuminate\Notifications\Notifiable;
use App\Restrictions\RestrictionsManager;
use App\Users\Restrictions\RestrictionsManager;
use App\Users\Notifications\ResetPassword;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
@@ -0,0 +1,51 @@
<?php
namespace App\Users\Restrictions\Engines;
use App\Users\Models\User;
use App\Users\Restrictions\RestrictionsEngine;
use Domain\Teams\Actions\CheckMaxTeamMembersLimitAction;
class DefaultRestrictionsEngine implements RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool
{
// 1. If storage limitations is set to false, then allow upload
if (! get_settings('storage_limitation')) {
return true;
}
// Get used storage percentage
$usedPercentage = get_storage_percentage(
used: $user->usedCapacity + $fileSize,
maxAmount: $user->limitations->max_storage_amount,
);
// 2. Check if storage usage exceed predefined capacity
return ! ($usedPercentage >= 100);
}
public function canDownload(User $user): bool
{
return true;
}
public function canCreateFolder(User $user): bool
{
return true;
}
public function canCreateTeamFolder(User $user): bool
{
return true;
}
public function canInviteTeamMembers(User $user, array $newInvites = []): bool
{
return true;
}
public function canVisitShared(User $user): bool
{
return true;
}
}
@@ -0,0 +1,46 @@
<?php
namespace App\Users\Restrictions\Engines;
use App\Users\Models\User;
use App\Users\Restrictions\RestrictionsEngine;
use Domain\Teams\Actions\CheckMaxTeamMembersLimitAction;
class FixedBillingRestrictionsEngine implements RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool
{
// Get used capacity
$usedPercentage = get_storage_percentage(
used: $user->usedCapacity + $fileSize,
maxAmount: $user->limitations->max_storage_amount,
);
// Check if storage usage exceed predefined capacity
return ! ($usedPercentage >= 100);
}
public function canDownload(User $user): bool
{
return true;
}
public function canCreateFolder(User $user): bool
{
return true;
}
public function canCreateTeamFolder(User $user): bool
{
return true;
}
public function canInviteTeamMembers(User $user, array $newInvites = []): bool
{
return resolve(CheckMaxTeamMembersLimitAction::class)($user, $newInvites);
}
public function canVisitShared(User $user): bool
{
return true;
}
}
@@ -0,0 +1,43 @@
<?php
namespace App\Users\Restrictions\Engines;
use App\Users\Models\User;
use App\Users\Restrictions\RestrictionsEngine;
class MeteredBillingRestrictionsEngine implements RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool
{
// Disable upload when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
}
public function canDownload(User $user): bool
{
// Disable download when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
}
public function canCreateFolder(User $user): bool
{
// Disable create folder when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
}
public function canCreateTeamFolder(User $user): bool
{
// Disable create folder when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
}
public function canInviteTeamMembers(User $user, array $newInvites = []): bool
{
return true;
}
public function canVisitShared(User $user): bool
{
// Disable share visit when user has more than 3 failed payments
return ! ($user->failedPayments()->count() >= 3);
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\Users\Restrictions;
use App\Users\Models\User;
interface RestrictionsEngine
{
public function canUpload(User $user, int $fileSize = 0): bool;
public function canDownload(User $user): bool;
public function canCreateFolder(User $user): bool;
public function canCreateTeamFolder(User $user): bool;
public function canInviteTeamMembers(User $user, array $newInvites = []): bool;
public function canVisitShared(User $user): bool;
}
@@ -0,0 +1,30 @@
<?php
namespace App\Users\Restrictions;
use Illuminate\Support\Manager;
use App\Users\Restrictions\Engines\DefaultRestrictionsEngine;
use App\Users\Restrictions\Engines\FixedBillingRestrictionsEngine;
use App\Users\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();
}
}