user = User::find($user); } elseif ($user instanceof User) { $this->user = $user; } else { $this->user = auth()->user(); } return $this; } private function checkUserPermission(Permission $permission): bool { if (is_null($this->user)) { $this->user = auth()->user(); } return $this->user->may($permission); } public function can(Permission $permission): bool { if (! $this->checkUserPermission($permission)) { return false; } $prop = 'can' . $permission->name; if (! method_exists($this, $prop)) { return false; } return $this->{$prop}(); } private function activeStudy(Closure $fn): bool { if (! $this->study->isActive()) { return false; } return $fn(); } public function canAssignPhysician(): bool { return $this->activeStudy(fn () => $this->study->report_status <= ReportStatus::Draft); } public function canStudyHistoryEdit(): bool { return $this->activeStudy(fn () => $this->study->report_status <= ReportStatus::Draft); } public function canAttachmentUpload(): bool { return $this->activeStudy(fn () => $this->study->report_status <= ReportStatus::Draft); } public function canReportDownload(): bool { return $this->activeStudy(fn () => $this->study->report_status >= ReportStatus::Finalized); } public function __construct(private readonly Study $study) {} }