User.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Models;
  3. // use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Filament\Models\Contracts\FilamentUser;
  5. use Filament\Models\Contracts\HasName;
  6. use Filament\Panel;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Foundation\Auth\User as Authenticatable;
  9. use Illuminate\Notifications\Notifiable;
  10. class User extends Authenticatable implements FilamentUser, HasName
  11. {
  12. /** @use HasFactory<\Database\Factories\UserFactory> */
  13. use HasFactory, Notifiable;
  14. /**
  15. * The table associated with the model.
  16. *
  17. * @var string
  18. */
  19. protected $table = 'users';
  20. /**
  21. * The primary key associated with the table.
  22. *
  23. * @var string
  24. */
  25. protected $primaryKey = 'id';
  26. /**
  27. * Indicates if the model's ID is auto-incrementing.
  28. *
  29. * @var bool
  30. */
  31. public $incrementing = true;
  32. /**
  33. * The data type of the primary key ID.
  34. *
  35. * @var string
  36. */
  37. protected $keyType = 'int';
  38. /**
  39. * The attributes that are mass assignable.
  40. *
  41. * @var list<string>
  42. */
  43. protected $fillable = [
  44. 'id',
  45. 'user_id',
  46. 'username',
  47. 'email',
  48. 'password',
  49. 'password_hash',
  50. 'full_name',
  51. 'role',
  52. 'phone',
  53. 'department',
  54. 'is_active',
  55. 'remember_token',
  56. 'last_login',
  57. 'login_count',
  58. 'deleted_at',
  59. ];
  60. /**
  61. * The attributes that should be hidden for serialization.
  62. *
  63. * @var list<string>
  64. */
  65. protected $hidden = [
  66. 'password_hash',
  67. 'remember_token',
  68. ];
  69. /**
  70. * Get the attributes that should be cast.
  71. *
  72. * @return array<string, string>
  73. */
  74. protected function casts(): array
  75. {
  76. return [
  77. 'email_verified_at' => 'datetime',
  78. 'is_active' => 'boolean',
  79. ];
  80. }
  81. public function canAccessPanel(?Panel $panel): bool
  82. {
  83. // 只有激活的用户才能访问面板
  84. return $this->is_active === true;
  85. }
  86. /**
  87. * Get the password for authentication.
  88. */
  89. public function getAuthPassword(): string
  90. {
  91. return (string) $this->password_hash;
  92. }
  93. /**
  94. * Get the password attribute (for compatibility).
  95. */
  96. public function getPasswordAttribute(): string
  97. {
  98. return $this->password_hash;
  99. }
  100. /**
  101. * Set the password attribute (for compatibility).
  102. */
  103. public function setPasswordAttribute(string $value): void
  104. {
  105. $this->attributes['password_hash'] = $value;
  106. }
  107. /**
  108. * Get the name to display for the user in Filament.
  109. * This fixes the TypeError where getUserName() expected string but got null.
  110. */
  111. public function getFilamentName(): string
  112. {
  113. return $this->full_name ?: $this->username ?: $this->email ?: 'Unknown User';
  114. }
  115. }