contextResolver->resolve($user); if ($context->isClinic()) { return $this->restrictionInClinic($user, $context->clinic); } // پزشکی که هنوز رکورد Doctor ندارد، محیطش «doctor» با شناسهٔ تهی است — // assertPatientGate همان را ۴۰۳ می‌کند. return $context->type === EntityContext::TYPE_DOCTOR ? PatientRecordScope::forDoctor($context->id) : PatientRecordScope::unknown(); } /** * مالک کلینیک همهٔ پرونده‌ها را می‌بیند؛ پزشکِ عضو فقط بیمارانِ خودش؛ منشی * فقط بیمارانِ پزشکانِ تخصیص‌یافته به او — عضویت در کلینیک به‌تنهایی یعنی * منشیِ یک پزشک پروندهٔ بیماران پزشک دیگر را ببیند. */ private function restrictionInClinic(User $user, Clinic $clinic): PatientRecordScope { if ($clinic->getUser()->getId() === $user->getId()) { return PatientRecordScope::forClinic($clinic->getId()); } if ($user->hasRole('ROLE_DOCTOR')) { $doctor = $this->doctorRepo->findByUser($user); if ($doctor === null) { return PatientRecordScope::unknown(); } if (!$this->clinicPermissions->can($user, $clinic, self::RESOURCE, 'view')) { return PatientRecordScope::forDoctor($doctor->getId()); } return PatientRecordScope::forClinicRestrictedToDoctors($clinic->getId(), [$doctor->getId()]); } if ($user->hasRole('ROLE_SECRETARY')) { $doctorIds = array_map( fn($d) => $d->getId(), $this->secretaryRepo->findDoctorsBySecretaryInClinic($user, $clinic), ); return PatientRecordScope::forClinicRestrictedToDoctors($clinic->getId(), $doctorIds); } return PatientRecordScope::forClinic($clinic->getId()); } }