first
This commit is contained in:
106
app/Models/User.php
Normal file
106
app/Models/User.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
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\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory, Notifiable, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'role',
|
||||
'department_id',
|
||||
'is_active',
|
||||
'last_login_at',
|
||||
'email_verified_at',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'last_login_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Role helpers
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->role === 'admin';
|
||||
}
|
||||
|
||||
public function isUser(): bool
|
||||
{
|
||||
return $this->role === 'user';
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return (bool) $this->is_active;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Relationships
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function department(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Department::class);
|
||||
}
|
||||
|
||||
public function ownedProjects(): HasMany
|
||||
{
|
||||
return $this->hasMany(TranscriptionProject::class, 'owner_user_id');
|
||||
}
|
||||
|
||||
public function collaboratingProjects(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
TranscriptionProject::class,
|
||||
'project_collaborators',
|
||||
'user_id',
|
||||
'project_id'
|
||||
)->withPivot('role', 'added_by')->withTimestamps();
|
||||
}
|
||||
|
||||
public function auditActionsPerformed(): HasMany
|
||||
{
|
||||
return $this->hasMany(AuditLog::class, 'actor_user_id');
|
||||
}
|
||||
|
||||
public function auditActionsTargeted(): HasMany
|
||||
{
|
||||
return $this->hasMany(AuditLog::class, 'target_user_id');
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Usage check (for safe delete)
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function hasUsage(): bool
|
||||
{
|
||||
return $this->ownedProjects()->withTrashed()->exists()
|
||||
|| $this->collaboratingProjects()->exists()
|
||||
|| AuditLog::where('actor_user_id', $this->id)->exists();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user