User.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. protected function companyAdmin(): Attribute
  82. {
  83. return Attribute::make(
  84. get: fn() => $this->role_id == config("auth.company_admin_role_id"),
  85. );
  86. }
  87. public function scopeAllowed(Builder $query)
  88. {
  89. if (!Auth::user()->super_admin) {
  90. if (Auth::user()->company_admin){
  91. $company = Company::query()
  92. ->where('id',Auth::user()->company_id)
  93. ->orWhere("parent_id", Auth::user()->company_id)
  94. ->pluck("id");
  95. $query->whereIn("company_id", $company->toArray());
  96. }else{
  97. $query->where("company_id", Auth::user()->company_id);
  98. }
  99. }
  100. }
  101. public function guardName(): string
  102. {
  103. return $this->guard_name;
  104. }
  105. public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  106. {
  107. return $this->belongsTo(Company::class);
  108. }
  109. public function role(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  110. {
  111. return $this->belongsTo(Role::class);
  112. }
  113. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  114. {
  115. return $this->belongsTo(User::class, 'created_by');
  116. }
  117. public function department(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  118. {
  119. return $this->belongsTo(Department::class);
  120. }
  121. public function leaderId(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  122. {
  123. return $this->belongsTo(User::class, 'leader_id');
  124. }
  125. }