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

View File

@@ -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

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");
});

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';
}

View File

@@ -10,7 +10,6 @@ class UserSettings extends Model
protected $guarded = [
'id',
'max_storage_amount',
];
/**

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,

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),
]),

View File

@@ -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),
],
],
],