152 lines
3.7 KiB
PHP
152 lines
3.7 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\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
use HasFactory, Notifiable, HasApiTokens;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'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');
|
|
}
|
|
|
|
public function favoriteDepartment()
|
|
{
|
|
return $this->department()->where('is_favorited', true);
|
|
}
|
|
|
|
public function userRoles(): HasMany
|
|
{
|
|
return $this->hasMany(UserRole::class, 'rf_user_id', 'id');
|
|
}
|
|
|
|
public function roles(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
Role::class,
|
|
UserRole::class,
|
|
'rf_user_id',
|
|
'role_id',
|
|
'id',
|
|
'rf_role_id'
|
|
);
|
|
}
|
|
|
|
public function currentRole()
|
|
{
|
|
$defaultRoleId = $this->roles()->where('is_default', true)->first()->role_id;
|
|
$sessionKey = 'user_' . $this->id . '_current_role';
|
|
$roleId = $this->current_role_id ?? $defaultRoleId;
|
|
|
|
$role = Role::where('role_id', $roleId)->first();
|
|
|
|
return $role;
|
|
}
|
|
|
|
// Методы для проверки ролей
|
|
public function isAdmin()
|
|
{
|
|
return $this->currentRole()->slug === 'admin';
|
|
}
|
|
|
|
public function isDoctor()
|
|
{
|
|
return $this->currentRole()->slug === 'doctor';
|
|
}
|
|
|
|
public function isHeadOfDepartment()
|
|
{
|
|
return $this->currentRole()->slug === 'head_of_department';
|
|
}
|
|
|
|
public function lpuDoctor()
|
|
{
|
|
return $this->belongsTo(MisLpuDoctor::class, 'rf_lpudoctor_id');
|
|
}
|
|
|
|
// Получение доступных отделений
|
|
public function availableDepartments()
|
|
{
|
|
$departments = Department::all();
|
|
|
|
if ($this->isAdmin()) {
|
|
return $departments;
|
|
}
|
|
|
|
return $this->department ? [$this->department] : [];
|
|
}
|
|
|
|
// Получение доступных действий
|
|
public function permissions()
|
|
{
|
|
$permissions = [
|
|
'view_dashboard' => true,
|
|
'view_metrics' => true,
|
|
'view_reports' => true,
|
|
];
|
|
|
|
if ($this->isAdmin() || $this->isDoctor() || $this->isHeadOfDepartment()) {
|
|
$permissions['create_metrics'] = true;
|
|
$permissions['edit_metrics'] = true;
|
|
$permissions['delete_metrics'] = true;
|
|
$permissions['manage_users'] = $this->isAdmin();
|
|
}
|
|
|
|
return $permissions;
|
|
}
|
|
}
|