Files
hoshpoint-backend/app/Models/User.php
soheil khaledabadi 936f789ac8 feat: Persian panel/API fixes and structural-model tweaks
- lang/fa: add validation/auth/passwords/pagination translations so admin
  and API validation errors are in Persian
- templates: soft-delete instead of cascade-deleting their generations;
  keep resolving trashed template on generations (labels/downloads)
- templates: add TemplateInfolist with a real thumbnail ImageEntry
- generations: auto-name template-based generations and use that name
  for the download file and as a searchable column
- admin: show all dates as Jalali via ->jalaliDate()/->jalaliDateTime()
  Filament macros (App\Support\Jalali)
- wallets: harden the deposit/withdraw action to surface the exact error
  instead of a 500
- profile: normalize Iranian mobile numbers (App\Support\Mobile),
  add social_links, coerce notification-preference booleans, require the
  current password to delete an account

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013P66ea1dC5CVM9t29496t5
2026-09-07 01:00:09 +03:30

111 lines
3.0 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
#[Fillable(['name', 'username', 'email', 'mobile', 'password', 'avatar_path', 'is_public', 'social_links'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable implements FilamentUser
{
/** @use HasFactory<UserFactory> */
use HasApiTokens, HasFactory, Notifiable;
/**
* @return BelongsToMany<Role, $this>
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class);
}
/**
* @return HasOne<Wallet, $this>
*/
public function wallet(): HasOne
{
return $this->hasOne(Wallet::class);
}
/**
* @return HasManyThrough<WalletTransaction, Wallet, $this>
*/
public function walletTransactions(): HasManyThrough
{
return $this->hasManyThrough(WalletTransaction::class, Wallet::class);
}
/**
* @return HasMany<Notification, $this>
*/
public function notifications(): HasMany
{
return $this->hasMany(Notification::class)->latest();
}
/**
* @return HasOne<NotificationPreference, $this>
*/
public function notificationPreference(): HasOne
{
return $this->hasOne(NotificationPreference::class);
}
/**
* @return HasMany<TemplateGeneration, $this>
*/
public function templateGenerations(): HasMany
{
return $this->hasMany(TemplateGeneration::class, 'requested_by')->latest();
}
public function hasRole(string|array $roles): bool
{
$roles = (array) $roles;
return $this->roles()->whereIn('slug', $roles)->exists();
}
public function hasPermission(string|array $permissions): bool
{
$permissions = (array) $permissions;
return $this->roles()
->whereHas('permissions', fn ($query) => $query->whereIn('slug', $permissions))
->exists();
}
public function canAccessPanel(Panel $panel): bool
{
return $this->hasRole('admin') || $this->hasPermission('admin.access');
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'is_public' => 'boolean',
'social_links' => 'array',
];
}
}