118 lines
3.3 KiB
PHP
118 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Role;
|
|
use App\Models\Admin\Admin;
|
|
use App\Models\Dosen\Dosen;
|
|
use App\Models\Mahasiswa\Mahasiswa;
|
|
use App\Models\Operator\Operator;
|
|
use App\Models\Superuser\Superuser;
|
|
use Database\Factories\UserFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Str;
|
|
|
|
// `role` sengaja tidak mass-assignable: hanya diisi lewat factory / forceFill.
|
|
#[Fillable(['name', 'username', 'email', 'password'])]
|
|
#[Hidden(['password', 'remember_token'])]
|
|
class User extends Authenticatable
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use HasFactory, Notifiable;
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'role' => Role::class,
|
|
'is_active' => 'boolean',
|
|
'last_login_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/** @return HasOne<Superuser, $this> */
|
|
public function superuser(): HasOne
|
|
{
|
|
return $this->hasOne(Superuser::class);
|
|
}
|
|
|
|
/** @return HasOne<Admin, $this> */
|
|
public function admin(): HasOne
|
|
{
|
|
return $this->hasOne(Admin::class);
|
|
}
|
|
|
|
/** @return HasOne<Operator, $this> */
|
|
public function operator(): HasOne
|
|
{
|
|
return $this->hasOne(Operator::class);
|
|
}
|
|
|
|
/** @return HasOne<Dosen, $this> */
|
|
public function dosen(): HasOne
|
|
{
|
|
return $this->hasOne(Dosen::class);
|
|
}
|
|
|
|
/** @return HasOne<Mahasiswa, $this> */
|
|
public function mahasiswa(): HasOne
|
|
{
|
|
return $this->hasOne(Mahasiswa::class);
|
|
}
|
|
|
|
/**
|
|
* Profil sesuai role akun ini (relasi dimuat bila belum, aman untuk strict mode).
|
|
*
|
|
* @return Attribute<Model|null, never>
|
|
*/
|
|
protected function profile(): Attribute
|
|
{
|
|
return Attribute::get(function (): ?Model {
|
|
$relation = $this->role->profileRelation();
|
|
|
|
return $this->loadMissing($relation)->getRelation($relation);
|
|
})->withoutObjectCaching();
|
|
}
|
|
|
|
public function hasRole(Role|string ...$roles): bool
|
|
{
|
|
foreach ($roles as $role) {
|
|
$role = $role instanceof Role ? $role : Role::tryFrom($role);
|
|
|
|
if ($role !== null && $this->role === $role) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/** Dua huruf inisial dari nama, mengabaikan gelar yang berakhiran titik (Dr., Ir.) dan gelar belakang. */
|
|
public function initials(): string
|
|
{
|
|
$name = Str::before($this->name, ',');
|
|
$words = collect(preg_split('/\s+/', trim($name), -1, PREG_SPLIT_NO_EMPTY));
|
|
|
|
$given = $words->reject(fn (string $word): bool => str_ends_with($word, '.'));
|
|
|
|
if ($given->isEmpty()) {
|
|
$given = $words;
|
|
}
|
|
|
|
return Str::upper($given->take(2)->map(fn (string $word): string => Str::substr($word, 0, 1))->implode(''));
|
|
}
|
|
}
|