User.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // 同步到global_users
  57. static::created(function (User $user) {
  58. GlobalUser::create([
  59. 'username' => $user->username,
  60. 'tenant_id' => tenant()->id,
  61. ]);
  62. });
  63. }
  64. /**
  65. * The attributes that should be hidden for serialization.
  66. *
  67. * @var array<int, string>
  68. */
  69. protected $hidden = [
  70. 'password',
  71. 'remember_token',
  72. ];
  73. /**
  74. * The attributes that should be cast.
  75. *
  76. * @var array<string, string>
  77. */
  78. protected $casts = [
  79. 'email_verified_at' => 'datetime',
  80. 'password' => 'hashed',
  81. ];
  82. protected function superAdmin(): Attribute
  83. {
  84. return Attribute::make(
  85. get: fn() => $this->role_id == config("auth.super_admin_role_id"),
  86. );
  87. }
  88. protected function companyAdmin(): Attribute
  89. {
  90. return Attribute::make(
  91. get: fn() => $this->role_id == config("auth.company_admin_role_id"),
  92. );
  93. }
  94. public function scopeAllowed(Builder $query)
  95. {
  96. if (!Auth::user()->super_admin) {
  97. if (Auth::user()->company_admin){
  98. $company = Company::query()
  99. ->where('id',Auth::user()->company_id)
  100. ->orWhere("parent_id", Auth::user()->company_id)
  101. ->pluck("id");
  102. $query->whereIn("company_id", $company->toArray());
  103. }else{
  104. $query->where("company_id", Auth::user()->company_id);
  105. }
  106. }
  107. }
  108. public function guardName(): string
  109. {
  110. return $this->guard_name;
  111. }
  112. public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  113. {
  114. return $this->belongsTo(Company::class);
  115. }
  116. public function role(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  117. {
  118. return $this->belongsTo(Role::class);
  119. }
  120. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  121. {
  122. return $this->belongsTo(User::class, 'created_by');
  123. }
  124. public function department(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  125. {
  126. return $this->belongsTo(Department::class);
  127. }
  128. public function leaderId(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  129. {
  130. return $this->belongsTo(User::class, 'leader_id');
  131. }
  132. }