implementation of user limits and refactoring

This commit is contained in:
Čarodej
2021-11-19 18:44:46 +01:00
parent 4851fb5eab
commit 6ca84d9041
54 changed files with 300 additions and 327 deletions
@@ -104,7 +104,6 @@ class SetupDevEnvironment extends Command
->settings()
->create([
'avatar' => $avatar_name,
'max_storage_amount' => 5,
'name' => 'Jane Doe',
'address' => $this->faker->address,
'state' => $this->faker->state,
@@ -163,7 +162,6 @@ class SetupDevEnvironment extends Command
->settings()
->create([
'avatar' => $avatar_name,
'max_storage_amount' => 5,
'name' => $this->faker->name,
'address' => $this->faker->address,
'state' => $this->faker->state,
@@ -1004,7 +1002,7 @@ class SetupDevEnvironment extends Command
'value' => 1,
],
[
'name' => 'storage_default',
'name' => 'default_storage_amount',
'value' => 5,
],
[
@@ -125,7 +125,7 @@ class SetupProdEnvironment extends Command
'value' => 1,
],
[
'name' => 'storage_default',
'name' => 'default_storage_amount',
'value' => 5,
],
[
@@ -195,7 +195,6 @@ class SetupProdEnvironment extends Command
$user
->settings()
->create([
'max_storage_amount' => 5,
'name' => 'Admin',
]);
@@ -25,7 +25,7 @@ class CreateNewUserAction extends Controller
RegisterUserRequest $request
): Application | ResponseFactory | Response {
$settings = get_settings([
'storage_default', 'registration', 'user_verification',
'default_storage_amount', 'registration', 'user_verification',
]);
// Check if account registration is enabled
@@ -44,17 +44,12 @@ class CreateNewUserAction extends Controller
$user->markEmailAsVerified();
}
UserSettings::unguard();
$user
->settings()
->create([
'name' => $request->input('name'),
'max_storage_amount' => $settings['storage_default'],
]);
UserSettings::reguard();
event(new Registered($user));
// Log in if verification is disabled
+18 -10
View File
@@ -1,4 +1,5 @@
<?php
namespace App\Users\Models;
use ByteUnits\Metric;
@@ -74,7 +75,6 @@ class User extends Authenticatable implements MustVerifyEmail
'name',
'role',
'created_at',
'max_storage_amount',
];
public $incrementing = false;
@@ -93,7 +93,7 @@ class User extends Authenticatable implements MustVerifyEmail
{
$is_storage_limit = get_settings('storage_limitation') ?? 1;
if (! $is_storage_limit) {
if (!$is_storage_limit) {
return [
'used' => $this->usedCapacity,
'used_formatted' => Metric::bytes($this->usedCapacity)->format(),
@@ -101,10 +101,10 @@ class User extends Authenticatable implements MustVerifyEmail
}
return [
'used' => (float) get_storage_fill_percentage($this->usedCapacity, $this->settings->max_storage_amount),
'used_formatted' => get_storage_fill_percentage($this->usedCapacity, $this->settings->max_storage_amount) . '%',
'capacity' => $this->settings->max_storage_amount,
'capacity_formatted' => format_gigabytes($this->settings->max_storage_amount),
'used' => (float)get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount),
'used_formatted' => get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount) . '%',
'capacity' => $this->limitations->max_storage_amount,
'capacity_formatted' => format_gigabytes($this->limitations->max_storage_amount),
];
}
@@ -114,7 +114,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function getUsedCapacityAttribute(): int
{
return $this->filesWithTrashed
->map(fn ($item) => $item->getRawOriginal())->sum('filesize');
->map(fn($item) => $item->getRawOriginal())->sum('filesize');
}
/**
@@ -130,14 +130,16 @@ class User extends Authenticatable implements MustVerifyEmail
->get();
}
/**
* Get user attributes
*/
public function settings(): HasOne
{
return $this->hasOne(UserSettings::class);
}
public function limitations(): HasOne
{
return $this->hasOne(UserLimitation::class);
}
/**
* Get user favourites folder
*/
@@ -192,6 +194,12 @@ class User extends Authenticatable implements MustVerifyEmail
static::creating(function ($user) {
$user->id = Str::uuid();
// Create default limitations
$user->limitations()->create([
'max_storage_amount' => get_settings('default_storage_amount') ?? 1,
'max_team_members' => 3,
]);
// Create user directory for his files
Storage::makeDirectory("files/$user->id");
});
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App\Users\Models;
use Illuminate\Database\Eloquent\Model;
class UserLimitation extends Model
{
public $timestamps = false;
protected $guarded = [];
protected $hidden = [
'user_id',
];
public $incrementing = false;
protected $keyType = 'string';
}
-1
View File
@@ -10,7 +10,6 @@ class UserSettings extends Model
protected $guarded = [
'id',
'max_storage_amount',
];
/**
+1 -1
View File
@@ -15,7 +15,7 @@ class SettingsResource extends JsonResource
{
return [
'data' => [
'id' => $this->id,
'id' => $this->user_id,
'type' => 'settings',
'attributes' => [
'avatar' => $this->avatar,
+9 -4
View File
@@ -16,13 +16,11 @@ class UserResource extends JsonResource
*/
public function toArray($request)
{
// TODO: zrefaktorovat
return [
'data' => [
'id' => $this->id,
'type' => 'user',
'attributes' => [
'max_storage_amount' => $this->settings->max_storage_amount,
'email' => is_demo() ? obfuscate_email($this->email) : $this->email,
'role' => $this->role,
'two_factor_authentication' => $this->two_factor_secret ? true : false,
@@ -32,8 +30,15 @@ class UserResource extends JsonResource
'updated_at' => format_date($this->updated_at, '%d. %B. %Y'),
],
'relationships' => [
'settings' => new SettingsResource($this->settings),
'favourites' => new FolderCollection($this->favouriteFolders),
'settings' => new SettingsResource($this->settings),
'favourites' => new FolderCollection($this->favouriteFolders),
'limitations' => [
'id' => $this->id,
'type' => 'limitations',
'data' => [
'attributes' => $this->limitations,
],
],
$this->mergeWhen($this->hasSubscription(), fn() => [
'subscription' => new SubscriptionResource($this->subscription),
]),
@@ -47,29 +47,29 @@ class UserStorageResource extends JsonResource
'type' => 'storage',
'attributes' => [
'used' => Metric::bytes($this->usedCapacity)->format(),
'capacity' => format_gigabytes($this->settings->max_storage_amount),
'percentage' => (float) get_storage_fill_percentage($this->usedCapacity, $this->settings->max_storage_amount),
'capacity' => format_gigabytes($this->limitations->max_storage_amount),
'percentage' => (float) get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount),
],
'meta' => [
'images' => [
'used' => Metric::bytes($images)->format(),
'percentage' => (float) get_storage_fill_percentage($images, $this->settings->max_storage_amount),
'percentage' => (float) get_storage_fill_percentage($images, $this->limitations->max_storage_amount),
],
'audios' => [
'used' => Metric::bytes($audios)->format(),
'percentage' => (float) get_storage_fill_percentage($audios, $this->settings->max_storage_amount),
'percentage' => (float) get_storage_fill_percentage($audios, $this->limitations->max_storage_amount),
],
'videos' => [
'used' => Metric::bytes($videos)->format(),
'percentage' => (float) get_storage_fill_percentage($videos, $this->settings->max_storage_amount),
'percentage' => (float) get_storage_fill_percentage($videos, $this->limitations->max_storage_amount),
],
'documents' => [
'used' => Metric::bytes($documents)->format(),
'percentage' => (float) get_storage_fill_percentage($documents, $this->settings->max_storage_amount),
'percentage' => (float) get_storage_fill_percentage($documents, $this->limitations->max_storage_amount),
],
'others' => [
'used' => Metric::bytes($others)->format(),
'percentage' => (float) get_storage_fill_percentage($others, $this->settings->max_storage_amount),
'percentage' => (float) get_storage_fill_percentage($others, $this->limitations->max_storage_amount),
],
],
],
@@ -16,7 +16,7 @@ class ChangeUserStorageCapacityController extends Controller
User $user,
): UserStorageResource {
$user
->settings()
->limitations()
->update(
$request->input('attributes')
);
@@ -43,18 +43,13 @@ class UserController extends Controller
'email_verified_at' => now(),
]);
UserSettings::unguard();
$user
->settings()
->create([
'max_storage_amount' => $request->input('max_storage_amount'),
'avatar' => store_avatar($request, 'avatar'),
'name' => $request->input('name'),
]);
UserSettings::reguard();
return response(new UserResource($user), 201);
}
}
@@ -48,7 +48,6 @@ class CreateAdminAccountController extends Controller
$user
->settings()
->create([
'max_storage_amount' => get_settings('storage_default') ?? 5,
'avatar' => store_avatar($request, 'avatar'),
'name' => $request->input('name'),
]);
@@ -61,7 +61,7 @@ class StoreAppSettingsController extends Controller
'value' => $request->input('storageLimitation'),
],
[
'name' => 'storage_default',
'name' => 'default_storage_amount',
'value' => $request->input('defaultStorage') ?? 5,
],
])->each(function ($col) {
@@ -1,4 +1,5 @@
<?php
namespace Support\Listeners;
use Illuminate\Events\Dispatcher;
@@ -10,22 +11,25 @@ class SubscriptionEventSubscriber
{
public function handleSubscriptionWasCreated($subscription)
{
$subscription->user->settings->update([
$subscription->user->limitations()->update([
'max_storage_amount' => $subscription->feature('max_storage_amount'),
'max_team_members' => $subscription->feature('max_team_members'),
]);
}
public function handleSubscriptionWasUpdated($subscription)
{
$subscription->user->settings->update([
$subscription->user->limitations()->update([
'max_storage_amount' => $subscription->feature('max_storage_amount'),
'max_team_members' => $subscription->feature('max_team_members'),
]);
}
public function handleSubscriptionWasExpired($subscription)
{
$subscription->user->settings->update([
'max_storage_amount' => get_settings('storage_default'),
$subscription->user->limitations()->update([
'max_storage_amount' => get_settings('default_storage_amount'),
'max_team_members' => 5,
]);
}
+1 -1
View File
@@ -499,7 +499,7 @@ if (! function_exists('user_storage_percentage')) {
$used = $user->usedCapacity + $additionals;
}
return get_storage_fill_percentage($used, $user->settings->max_storage_amount);
return get_storage_fill_percentage($used, $user->limitations->max_storage_amount);
}
}