mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 08:51:11 -05:00
Add models, enums, and services for user roles, theme preferences, slug history, and ID generation
This commit is contained in:
@@ -2,31 +2,36 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use App\Enums\ThemePreference;
|
||||
use App\Enums\UserRole;
|
||||
use App\Services\IdGenerator;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
* @var array<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'role',
|
||||
'theme_preference',
|
||||
'pending_email',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
* @var array<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
@@ -43,6 +48,107 @@ class User extends Authenticatable
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'role' => UserRole::class,
|
||||
'theme_preference' => ThemePreference::class,
|
||||
'is_banned' => 'boolean',
|
||||
'ban_date' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's profile
|
||||
*/
|
||||
public function profile(): HasOne
|
||||
{
|
||||
return $this->hasOne(Profile::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's display name, falling back to username if not set
|
||||
*/
|
||||
public function getDisplayName(): string
|
||||
{
|
||||
return $this->profile?->display_name ?? $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user has moderation privileges
|
||||
*/
|
||||
public function canModerate(): bool
|
||||
{
|
||||
return $this->role->canModerate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user has admin privileges
|
||||
*/
|
||||
public function canAdmin(): bool
|
||||
{
|
||||
return $this->role->canAdmin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ban the user with a reason
|
||||
*/
|
||||
public function ban(string $reason): void
|
||||
{
|
||||
$this->update([
|
||||
'is_banned' => true,
|
||||
'ban_reason' => $reason,
|
||||
'ban_date' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unban the user
|
||||
*/
|
||||
public function unban(): void
|
||||
{
|
||||
$this->update([
|
||||
'is_banned' => false,
|
||||
'ban_reason' => null,
|
||||
'ban_date' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle pending email changes
|
||||
*/
|
||||
public function setPendingEmail(string $email): void
|
||||
{
|
||||
$this->update(['pending_email' => $email]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm pending email change
|
||||
*/
|
||||
public function confirmEmailChange(): void
|
||||
{
|
||||
if ($this->pending_email) {
|
||||
$this->update([
|
||||
'email' => $this->pending_email,
|
||||
'pending_email' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot the model
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function (User $user) {
|
||||
if (!$user->user_id) {
|
||||
$user->user_id = IdGenerator::generate(User::class, 'user_id');
|
||||
}
|
||||
if (!$user->role) {
|
||||
$user->role = UserRole::USER;
|
||||
}
|
||||
if (!$user->theme_preference) {
|
||||
$user->theme_preference = ThemePreference::LIGHT;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user