123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace App\Models;
- // use Illuminate\Contracts\Auth\MustVerifyEmail;
- use EloquentFilter\Filterable;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Support\Facades\Auth;
- use Laravel\Sanctum\HasApiTokens;
- use Spatie\Permission\Traits\HasRoles;
- class User extends Authenticatable
- {
- use HasApiTokens, HasFactory, Notifiable, HasRoles, SoftDeletes,Filterable;
- protected string $guard_name = 'api';
- /**
- * The attributes that are mass assignable.
- *
- * @var array<int, string>
- */
- protected $fillable = [
- 'name',
- 'username',
- 'email',
- 'password',
- 'company_id',
- 'department_id',
- 'role_id',
- 'created_by',
- 'gender',
- 'address',
- 'phone',
- 'status',
- 'fs_password',
- 'leader_id',
- ];
- //因为CompanyScope要拿到当前用户,但是user模型还没创建完无法使用CompanyScope
- protected static function booted(): void
- {
- //创建用户
- static::creating(function (User $user){
- $result=User::query()->where('company_id',$user->company_id)->max('display_id');
- $displayIndex=$result >0? $result+1 :1;
- $user->display_id =$displayIndex;
- });
- //更改用户所属公司
- static::updating(function (User $user){
- if($user->isDirty('company_id')){
- $result=User::query()->where('company_id',$user->company_id)->max('display_id');
- $displayIndex=$result >0? $result+1 :1;
- $user->display_id =$displayIndex;
- }
- });
- // 同步到global_users
- static::created(function (User $user) {
- GlobalUser::create([
- 'username' => $user->username,
- 'tenant_id' => tenant()->id,
- ]);
- });
- }
- /**
- * The attributes that should be hidden for serialization.
- *
- * @var array<int, string>
- */
- protected $hidden = [
- 'password',
- 'remember_token',
- ];
- /**
- * The attributes that should be cast.
- *
- * @var array<string, string>
- */
- protected $casts = [
- 'email_verified_at' => 'datetime',
- 'password' => 'hashed',
- ];
- protected function superAdmin(): Attribute
- {
- return Attribute::make(
- get: fn() => $this->role_id == config("auth.super_admin_role_id"),
- );
- }
- protected function companyAdmin(): Attribute
- {
- return Attribute::make(
- get: fn() => $this->role_id == config("auth.company_admin_role_id"),
- );
- }
- public function scopeAllowed(Builder $query)
- {
- if (!Auth::user()->super_admin) {
- if (Auth::user()->company_admin){
- $company = Company::query()
- ->where('id',Auth::user()->company_id)
- ->orWhere("parent_id", Auth::user()->company_id)
- ->pluck("id");
- $query->whereIn("company_id", $company->toArray());
- }else{
- $query->where("company_id", Auth::user()->company_id);
- }
- }
- }
- public function guardName(): string
- {
- return $this->guard_name;
- }
- public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
- {
- return $this->belongsTo(Company::class);
- }
- public function role(): \Illuminate\Database\Eloquent\Relations\BelongsTo
- {
- return $this->belongsTo(Role::class);
- }
- public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
- {
- return $this->belongsTo(User::class, 'created_by');
- }
- public function department(): \Illuminate\Database\Eloquent\Relations\BelongsTo
- {
- return $this->belongsTo(Department::class);
- }
- public function leaderId(): \Illuminate\Database\Eloquent\Relations\BelongsTo
- {
- return $this->belongsTo(User::class, 'leader_id');
- }
- }
|