*/ use HasApiTokens, HasFactory, Notifiable, HasRoles; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'name', 'login', 'email', 'password', 'is_active', 'rf_lpudoctor_id', 'rf_department_id', 'current_role_id', ]; /** * The attributes that should be hidden for serialization. * * @var list */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } public function department(): BelongsTo { return $this->belongsTo(Department::class, 'rf_department_id'); } public function departments() { return $this->hasMany(UserDepartment::class, 'rf_user_id', 'id')->orderBy('order'); } public function favoriteDepartment() { return $this->department()->where('is_favorited', true); } public function userRoles(): HasMany { return $this->hasMany(UserRole::class, 'rf_user_id', 'id'); } // Переименовано из roles() чтобы не конфликтовать с Spatie HasRoles::roles() public function appRoles(): HasManyThrough { return $this->hasManyThrough( Role::class, UserRole::class, 'rf_user_id', 'role_id', 'id', 'rf_role_id' ); } public function currentRole() { $defaultRoleId = $this->appRoles()->where('is_default', true)->first()->role_id; if (app()->runningInConsole()) { // Код выполняется в CLI (команда artisan, тесты и т.д.) return Role::where('role_id', $defaultRoleId)->first(); } $sessionId = session()->getId(); $token = Auth::user()->currentAccessToken(); // $sessionKey = 'user_' . $this->id . '_current_role'; // $roleId = $this->current_role_id ?? $defaultRoleId; // // $role = Role::where('role_id', $roleId)->first(); if ($token) { foreach ($token->abilities as $ability) { if (str_starts_with($ability, 'role:')) { $apiRoleId = (int) str_replace('role:', '', $ability); $roleId = $apiRoleId ?? $defaultRoleId; $role = Role::where('role_id', $roleId)->first(); if ($role) { return $role; } } } } $sessionRoleId = DB::table('sessions')->where('id', $sessionId)->value('role_id'); $roleId = $sessionRoleId ?? $defaultRoleId; // dd($sessionId); $role = Role::where('role_id', $roleId)->first(); return $role; } // Методы для проверки ролей public function isAdmin(): bool { return $this->currentRole()->slug === 'admin'; } public function isChiefDoctor(): bool { return $this->currentRole()->slug === 'gv'; } public function isDeputyChief(): bool { return $this->currentRole()->slug === 'zam'; } public function isHeadOfDepartment(): bool { return $this->currentRole()->slug === 'zav'; } public function isDoctor(): bool { return $this->currentRole()->slug === 'dej'; } public function isNurse(): bool { return $this->currentRole()->slug === 'nurse'; } public function isSeniorStaff(): bool { return $this->isAdmin() || $this->isChiefDoctor() || $this->isDeputyChief() || $this->isHeadOfDepartment(); } public function currentRoleCan(string $permission): bool { $slug = $this->currentRole()->slug; return \Spatie\Permission\Models\Role::findByName($slug) ?->permissions->pluck('name') ->contains($permission) ?? false; } public function lpuDoctor() { return $this->belongsTo(MisLpuDoctor::class, 'rf_lpudoctor_id'); } // Получение доступных отделений public function availableDepartments() { $departments = Department::all(); if ($this->isSeniorStaff()) { return $departments; } return $this->department ? [$this->department] : []; } }