mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-25 18:20:38 +00:00
team members limitation frontend/backend
This commit is contained in:
@@ -109,34 +109,6 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
];
|
||||
}
|
||||
|
||||
// TODO: caching & refactoring
|
||||
public function accountLimitations(): array
|
||||
{
|
||||
$members = \DB::table('team_folder_members')
|
||||
->where('user_id', $this->id)
|
||||
->pluck('parent_id');
|
||||
|
||||
$membersUse = \DB::table('team_folder_members')
|
||||
->where('user_id', '!=', $this->id)
|
||||
->whereIn('parent_id', $members)
|
||||
->pluck('user_id')
|
||||
->unique()
|
||||
->count();
|
||||
|
||||
return [
|
||||
'max_storage_amount' => [
|
||||
'use' => Metric::bytes($this->usedCapacity)->format(),
|
||||
'total' => format_gigabytes($this->limitations->max_storage_amount),
|
||||
'percentage' => (float)get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount),
|
||||
],
|
||||
'max_team_members' => [
|
||||
'use' => $membersUse,
|
||||
'total' => (int)$this->limitations->max_team_members,
|
||||
'percentage' => ($membersUse / $this->limitations->max_team_members) * 100,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user used storage capacity in bytes
|
||||
*/
|
||||
@@ -209,6 +181,11 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
return $this->hasMany(File::class);
|
||||
}
|
||||
|
||||
public function folders(): HasMany
|
||||
{
|
||||
return $this->hasMany(Folder::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the password reset notification.
|
||||
*/
|
||||
@@ -227,7 +204,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
// Create default limitations
|
||||
$user->limitations()->create([
|
||||
'max_storage_amount' => get_settings('default_storage_amount') ?? 1,
|
||||
'max_team_members' => 3,
|
||||
'max_team_members' => 5,
|
||||
]);
|
||||
|
||||
// Create user directory for his files
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\Models;
|
||||
|
||||
use ByteUnits\Metric;
|
||||
use DB;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Database\Factories\UserLimitationFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
/**
|
||||
* @property int max_storage_amount
|
||||
* @property int max_team_members
|
||||
* @property string user_id
|
||||
*/
|
||||
class UserLimitation extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
@@ -14,7 +23,7 @@ class UserLimitation extends Model
|
||||
protected $guarded = [];
|
||||
|
||||
protected $hidden = [
|
||||
'user_id',
|
||||
'user_id', 'user'
|
||||
];
|
||||
|
||||
public $incrementing = false;
|
||||
@@ -25,4 +34,74 @@ class UserLimitation extends Model
|
||||
{
|
||||
return UserLimitationFactory::new();
|
||||
}
|
||||
|
||||
public function user(): HasOne
|
||||
{
|
||||
return $this->hasOne(User::class, 'id', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get summary of user limitations and their usage
|
||||
*/
|
||||
public function summary(): array
|
||||
{
|
||||
return [
|
||||
'max_storage_amount' => $this->getMaxStorageAmount(),
|
||||
'max_team_members' => $this->getMaxTeamMembers(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get usage data of user storage
|
||||
*/
|
||||
private function getMaxStorageAmount(): array
|
||||
{
|
||||
$userCapacity = $this->user->usedCapacity;
|
||||
|
||||
return [
|
||||
'use' => Metric::bytes($userCapacity)->format(),
|
||||
'total' => format_gigabytes($this->max_storage_amount),
|
||||
'percentage' => get_storage_fill_percentage($userCapacity, $this->max_storage_amount),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get usage data of team members
|
||||
*/
|
||||
private function getMaxTeamMembers(): array
|
||||
{
|
||||
$userTeamFolderIds = DB::table('team_folder_members')
|
||||
->where('user_id', $this->user_id)
|
||||
->pluck('parent_id');
|
||||
|
||||
$memberIds = DB::table('team_folder_members')
|
||||
->where('user_id', '!=', $this->user_id)
|
||||
->whereIn('parent_id', $userTeamFolderIds)
|
||||
->pluck('user_id')
|
||||
->unique();
|
||||
|
||||
// Get member emails
|
||||
$memberEmails = User::whereIn('id', $memberIds)
|
||||
->pluck('email');
|
||||
|
||||
// Get active invitation emails
|
||||
$InvitationEmails = DB::table('team_folder_invitations')
|
||||
->where('status', 'pending')
|
||||
->where('inviter_id', $this->user_id)
|
||||
->pluck('email')
|
||||
->unique();
|
||||
|
||||
// Get allowed emails in the limit
|
||||
$totalUsedEmails = $memberEmails->merge($InvitationEmails)
|
||||
->unique();
|
||||
|
||||
return [
|
||||
'use' => $totalUsedEmails->count(),
|
||||
'total' => (int) $this->max_team_members,
|
||||
'percentage' => ($totalUsedEmails->count() / $this->max_team_members) * 100,
|
||||
'meta' => [
|
||||
'allowed_emails' => $totalUsedEmails,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ class UserResource extends JsonResource
|
||||
]),
|
||||
],
|
||||
'meta' => [
|
||||
'limitations' => $this->accountLimitations(),
|
||||
'limitations' => $this->limitations->summary(),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user