187 lines
4.0 KiB
PHP
187 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
use HasFactory, Notifiable, HasRoles;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'traccar_user_id',
|
|
'traccar_username',
|
|
'traccar_password',
|
|
'phone',
|
|
'timezone',
|
|
'is_active',
|
|
'is_admin',
|
|
'status',
|
|
'last_login_at',
|
|
'company',
|
|
'notes',
|
|
'created_by',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
'traccar_password',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'is_active' => 'boolean',
|
|
'is_admin' => 'boolean',
|
|
'last_login_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get devices assigned to this user
|
|
*/
|
|
public function devices()
|
|
{
|
|
return $this->hasMany(Device::class);
|
|
}
|
|
|
|
/**
|
|
* Get device groups this user has access to
|
|
*/
|
|
public function deviceGroups()
|
|
{
|
|
return $this->belongsToMany(DeviceGroup::class, 'device_group_user', 'user_id', 'device_group_id');
|
|
}
|
|
|
|
/**
|
|
* Get user's driver profile
|
|
*/
|
|
public function driver()
|
|
{
|
|
return $this->hasOne(Driver::class);
|
|
}
|
|
|
|
/**
|
|
* Get notification preferences
|
|
*/
|
|
public function notificationPreferences()
|
|
{
|
|
return $this->hasOne(NotificationPreference::class);
|
|
}
|
|
|
|
/**
|
|
* Get notification settings (new advanced system)
|
|
*/
|
|
public function notificationSettings()
|
|
{
|
|
return $this->hasOne(NotificationSetting::class);
|
|
}
|
|
|
|
/**
|
|
* Get user's subscription
|
|
*/
|
|
public function subscription()
|
|
{
|
|
return $this->hasOne(Subscription::class);
|
|
}
|
|
|
|
/**
|
|
* Get user's active subscription
|
|
*/
|
|
public function activeSubscription()
|
|
{
|
|
return $this->hasOne(Subscription::class)->where('status', 'active');
|
|
}
|
|
|
|
/**
|
|
* Get users created by this user (admin functionality)
|
|
*/
|
|
public function createdUsers()
|
|
{
|
|
return $this->hasMany(User::class, 'created_by');
|
|
}
|
|
|
|
/**
|
|
* Get user who created this user
|
|
*/
|
|
public function creator()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
/**
|
|
* Get commands sent by this user
|
|
*/
|
|
public function commands()
|
|
{
|
|
return $this->hasMany(Command::class);
|
|
}
|
|
|
|
/**
|
|
* Check if user is admin
|
|
*/
|
|
public function isAdmin(): bool
|
|
{
|
|
return $this->is_admin || $this->hasRole('admin');
|
|
}
|
|
|
|
/**
|
|
* Check if user is active
|
|
*/
|
|
public function isActive(): bool
|
|
{
|
|
return $this->status === 'active' && $this->is_active;
|
|
}
|
|
|
|
/**
|
|
* Get user's device limit based on subscription
|
|
*/
|
|
public function getDeviceLimit(): int
|
|
{
|
|
if ($this->subscription && $this->subscription->isActive()) {
|
|
return $this->subscription->device_limit;
|
|
}
|
|
|
|
return 1; // Default limit
|
|
}
|
|
|
|
/**
|
|
* Get the user's initials
|
|
*/
|
|
public function initials(): string
|
|
{
|
|
return Str::of($this->name)
|
|
->explode(' ')
|
|
->take(2)
|
|
->map(fn ($word) => Str::substr($word, 0, 1))
|
|
->implode('');
|
|
}
|
|
}
|