User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. 'leader_id',
  38. ];
  39. //因为CompanyScope要拿到当前用户,但是user模型还没创建完无法使用CompanyScope
  40. protected static function booted(): void
  41. {
  42. //创建用户
  43. static::creating(function (User $user){
  44. $result=User::query()->where('company_id',$user->company_id)->max('display_id');
  45. $displayIndex=$result >0? $result+1 :1;
  46. $user->display_id =$displayIndex;
  47. });
  48. //更改用户所属公司
  49. static::updating(function (User $user){
  50. if($user->isDirty('company_id')){
  51. $result=User::query()->where('company_id',$user->company_id)->max('display_id');
  52. $displayIndex=$result >0? $result+1 :1;
  53. $user->display_id =$displayIndex;
  54. }
  55. });
  56. }
  57. /**
  58. * The attributes that should be hidden for serialization.
  59. *
  60. * @var array<int, string>
  61. */
  62. protected $hidden = [
  63. 'password',
  64. 'remember_token',
  65. ];
  66. /**
  67. * The attributes that should be cast.
  68. *
  69. * @var array<string, string>
  70. */
  71. protected $casts = [
  72. 'email_verified_at' => 'datetime',
  73. 'password' => 'hashed',
  74. ];
  75. protected function superAdmin(): Attribute
  76. {
  77. return Attribute::make(
  78. get: fn() => $this->role_id == config("auth.super_admin_role_id"),
  79. );
  80. }
  81. public function scopeAllowed(Builder $query)
  82. {
  83. if (!Auth::user()->super_admin) {
  84. $query->where("company_id", Auth::user()->company_id);
  85. }
  86. }
  87. public function guardName(): string
  88. {
  89. return $this->guard_name;
  90. }
  91. public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  92. {
  93. return $this->belongsTo(Company::class);
  94. }
  95. public function role(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  96. {
  97. return $this->belongsTo(Role::class);
  98. }
  99. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  100. {
  101. return $this->belongsTo(User::class, 'created_by');
  102. }
  103. public function department(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  104. {
  105. return $this->belongsTo(Department::class);
  106. }
  107. }