73 lines
2.2 KiB
PHP
Executable File
73 lines
2.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Database\Factories\UserFactory;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Fortify\Contracts\PasskeyUser;
|
|
use Laravel\Fortify\PasskeyAuthenticatable;
|
|
use Laravel\Fortify\TwoFactorAuthenticatable;
|
|
use Modules\Company\Models\Company;
|
|
use Modules\Company\Models\CompanyDirectorAssignment;
|
|
use Modules\Company\Models\CompanyDivision;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User extends Authenticatable implements PasskeyUser
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use HasFactory, HasRoles, Notifiable, PasskeyAuthenticatable, TwoFactorAuthenticatable;
|
|
|
|
protected $fillable = [
|
|
'company_id', 'name', 'email', 'password', 'is_platform_admin', 'is_company_admin', 'status',
|
|
];
|
|
|
|
protected $hidden = ['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'is_platform_admin' => 'boolean',
|
|
'is_company_admin' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class);
|
|
}
|
|
|
|
public function divisions(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(CompanyDivision::class, 'company_division_user')
|
|
->withPivot(['company_id', 'role', 'is_director'])
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function directorAssignments(): HasMany
|
|
{
|
|
return $this->hasMany(CompanyDirectorAssignment::class);
|
|
}
|
|
|
|
public function initials(): string
|
|
{
|
|
return Str::of($this->name)
|
|
->explode(' ')
|
|
->take(2)
|
|
->map(fn ($word) => Str::substr((string) $word, 0, 1))
|
|
->implode('');
|
|
}
|
|
|
|
public function isMpbaPlatformAdmin(): bool
|
|
{
|
|
return $this->is_platform_admin && $this->company?->is_system_company === true;
|
|
}
|
|
}
|