114 lines
2.6 KiB
PHP
114 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\BelongsToTenant;
|
|
use App\Traits\HasPublicIdentifier;
|
|
use Database\Factories\UserFactory;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Modules\UserManagement\Models\CustomerProfile;
|
|
use Modules\UserManagement\Models\EmployeeProfile;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use BelongsToTenant, HasFactory, HasPublicIdentifier, HasRoles, Notifiable;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'user_type',
|
|
'status',
|
|
'userable_type',
|
|
'userable_id',
|
|
'contractor_id',
|
|
'must_change_password',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'must_change_password' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function userable(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public function employeeProfile()
|
|
{
|
|
return $this->hasOne(EmployeeProfile::class);
|
|
}
|
|
|
|
public function customerProfile()
|
|
{
|
|
return $this->hasOne(CustomerProfile::class);
|
|
}
|
|
|
|
public function contractor(): BelongsTo
|
|
{
|
|
return $this->belongsTo(\Modules\ContractorManagement\Models\Contractor::class);
|
|
}
|
|
|
|
public function isAdmin(): bool
|
|
{
|
|
return $this->user_type === 'admin';
|
|
}
|
|
|
|
public function isPlatformAdmin(): bool
|
|
{
|
|
return is_null($this->contractor_id) && $this->isAdmin();
|
|
}
|
|
|
|
public function isContractorAdmin(): bool
|
|
{
|
|
return ! is_null($this->contractor_id)
|
|
&& $this->hasRole('Contractor');
|
|
}
|
|
|
|
public function isContractorUser(): bool
|
|
{
|
|
return ! is_null($this->contractor_id);
|
|
}
|
|
|
|
public function isEmployee(): bool
|
|
{
|
|
return in_array($this->user_type, ['admin', 'employee']);
|
|
}
|
|
|
|
public function isCustomer(): bool
|
|
{
|
|
return $this->user_type === 'customer';
|
|
}
|
|
|
|
public function isActive(): bool
|
|
{
|
|
return $this->status === 'active';
|
|
}
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('status', 'active');
|
|
}
|
|
|
|
public function scopeOfType($query, string $type)
|
|
{
|
|
return $query->where('user_type', $type);
|
|
}
|
|
}
|