User.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 'full_name',
  50. 'role',
  51. 'phone',
  52. 'department',
  53. 'is_active',
  54. ];
  55. /**
  56. * The attributes that should be hidden for serialization.
  57. *
  58. * @var list<string>
  59. */
  60. protected $hidden = [
  61. 'password',
  62. ];
  63. /**
  64. * Get the attributes that should be cast.
  65. *
  66. * @return array<string, string>
  67. */
  68. protected function casts(): array
  69. {
  70. return [
  71. 'email_verified_at' => 'datetime',
  72. 'is_active' => 'boolean',
  73. ];
  74. }
  75. public function canAccessPanel(Panel $panel): bool
  76. {
  77. return true; // 所有用户都可以访问面板
  78. }
  79. /**
  80. * Get the password for authentication.
  81. */
  82. public function getAuthPassword(): string
  83. {
  84. return (string) ($this->password_hash ?? $this->password ?? '');
  85. }
  86. /**
  87. * Get the name to display for the user in Filament.
  88. * This fixes the TypeError where getUserName() expected string but got null.
  89. */
  90. public function getFilamentName(): string
  91. {
  92. return $this->full_name ?: $this->username ?: $this->email ?: 'Unknown User';
  93. }
  94. }