209 lines
6.0 KiB
PHP
209 lines
6.0 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||
use Illuminate\Notifications\Notifiable;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Laravel\Sanctum\HasApiTokens;
|
||
use Spatie\Permission\Traits\HasRoles;
|
||
|
||
class User extends Authenticatable
|
||
{
|
||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||
use HasApiTokens, HasFactory, Notifiable, HasRoles;
|
||
|
||
/**
|
||
* The attributes that are mass assignable.
|
||
*
|
||
* @var list<string>
|
||
*/
|
||
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<string>
|
||
*/
|
||
protected $hidden = [
|
||
'password',
|
||
'remember_token',
|
||
];
|
||
|
||
/**
|
||
* Get the attributes that should be cast.
|
||
*
|
||
* @return array<string, string>
|
||
*/
|
||
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 misDepartments()
|
||
{
|
||
return $this->belongsToMany(
|
||
Department::class, // конечная модель
|
||
'user_departments', // промежуточная таблица
|
||
'rf_user_id', // внешний ключ в промежуточной таблице на User
|
||
'rf_department_id', // внешний ключ в промежуточной таблице на Department
|
||
'id', // локальный ключ в User
|
||
'department_id' // локальный ключ в Department (уточните имя!)
|
||
)->withPivot('order') // если нужен order
|
||
->orderBy('user_departments.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] : [];
|
||
}
|
||
|
||
|
||
}
|