User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Models;
  3. // use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use EloquentFilter\Filterable;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Casts\Attribute;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. use Illuminate\Foundation\Auth\User as Authenticatable;
  10. use Illuminate\Notifications\Notifiable;
  11. use Illuminate\Support\Facades\Auth;
  12. use Laravel\Sanctum\HasApiTokens;
  13. use Spatie\Permission\Traits\HasRoles;
  14. class User extends Authenticatable
  15. {
  16. use HasApiTokens, HasFactory, Notifiable, HasRoles, SoftDeletes,Filterable;
  17. protected string $guard_name = 'api';
  18. /**
  19. * The attributes that are mass assignable.
  20. *
  21. * @var array<int, string>
  22. */
  23. protected $fillable = [
  24. 'name',
  25. 'username',
  26. 'email',
  27. 'password',
  28. 'company_id',
  29. 'department_id',
  30. 'role_id',
  31. 'created_by',
  32. 'gender',
  33. 'address',
  34. 'phone',
  35. 'status',
  36. 'fs_password',
  37. ];
  38. //因为CompanyScope要拿到当前用户,但是user模型还没创建完无法使用CompanyScope
  39. protected static function booted(): void
  40. {
  41. //创建用户
  42. static::creating(function (User $user){
  43. $result=User::query()->where('company_id',$user->company_id)->max('display_id');
  44. $displayIndex=$result >0? $result+1 :1;
  45. $user->display_id =$displayIndex;
  46. });
  47. //更改用户所属公司
  48. static::updating(function (User $user){
  49. if($user->isDirty('company_id')){
  50. $result=User::query()->where('company_id',$user->company_id)->max('display_id');
  51. $displayIndex=$result >0? $result+1 :1;
  52. $user->display_id =$displayIndex;
  53. }
  54. });
  55. }
  56. /**
  57. * The attributes that should be hidden for serialization.
  58. *
  59. * @var array<int, string>
  60. */
  61. protected $hidden = [
  62. 'password',
  63. 'remember_token',
  64. ];
  65. /**
  66. * The attributes that should be cast.
  67. *
  68. * @var array<string, string>
  69. */
  70. protected $casts = [
  71. 'email_verified_at' => 'datetime',
  72. 'password' => 'hashed',
  73. ];
  74. protected function superAdmin(): Attribute
  75. {
  76. return Attribute::make(
  77. get: fn() => $this->role_id == config("auth.super_admin_role_id"),
  78. );
  79. }
  80. public function scopeAllowed(Builder $query)
  81. {
  82. if (!Auth::user()->super_admin) {
  83. $query->where("company_id", Auth::user()->company_id);
  84. }
  85. }
  86. public function guardName(): string
  87. {
  88. return $this->guard_name;
  89. }
  90. public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  91. {
  92. return $this->belongsTo(Company::class);
  93. }
  94. public function role(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  95. {
  96. return $this->belongsTo(Role::class);
  97. }
  98. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  99. {
  100. return $this->belongsTo(User::class, 'created_by');
  101. }
  102. public function department(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  103. {
  104. return $this->belongsTo(Department::class);
  105. }
  106. }