Three related fixes, all rooted in the same flaw: authorization and scoping
decided by the caller's role instead of by the environment the data belongs to.
1. Single-appointment access (clinic operations were entirely broken)
AppointmentController::canView/canManage only knew the patient, the owning
doctor and admin -- appointment.clinic was never consulted. A clinic user could
create an appointment through /my/appointment but got 403 on detail, edit,
move, reserve transfer/replace and status change, so nearly every appointment
operation failed in clinic mode.
AppointmentAccessChecker now decides from appointment.clinic: clinic owner,
member doctor (via ClinicDoctorPermissionChecker) and assigned secretary (via
active context + DoctorSecretary) are recognised. Actions reuse the existing
permission vocabulary, so active=false remains the single source of truth for
"collaboration ended". Cancellation is gated separately and an inline status on
PATCH /appointment/{uuid} cannot bypass that gate. The patient is narrowed to
view + cancel.
Also fixed alongside: listByDoctor now serves a clinic manager but scoped to
that clinic; todayStats gained an admin branch and no longer passes an array of
doctor ids as the clinic parameter; PatientController::appointments filters on
appointment.clinic instead of current membership, so deactivating a doctor no
longer erases clinic appointment history from the case file.
The doctor-only active_slot_key was reviewed and deliberately left alone -- a
doctor is one physical person, so adding clinic to the key would permit
double-booking, not fix a bug. Reasoning recorded on the entity.
2. Appointment registration and confirmation
Panel-created appointments are born pending ("ثبت شده") instead of confirmed.
Confirming is now an explicit act: POST /appointment/{uuid}/confirm transitions
the status, files the case file for the appointment's environment (reusing an
existing record or creating one) and registers full or partial payments on the
resulting visit -- all in one transaction.
AppointmentExpiryService would have expired those pending appointments the
moment their slot time passed; findExpiredPending is now limited to online
gateway holds, which are the only pendings carrying a TTL. A pending
appointment still occupies its slot, so the time stays reserved.
The admin panel gets a "قطعی کردن نوبت" modal showing the visit fee, each
selected service, the total, and paid/remaining/status. It is wired inside
AppointmentStatusDropdown, so picking "confirmed" anywhere (timeline, detail,
reserve list, info modal) goes through it and confirmation can never silently
skip the case file and payment.
3. Clinic case-file access
PatientRecordScopeResolver replaces the single-destination role mapping: the
active context decides, so a doctor invited into a clinic finally sees their
patients' records there. A clinic record is per-patient and shared by design,
so "their own patients" is derived from appointments with that doctor in that
clinic rather than from a new column. Clinic secretaries are limited to their
assigned doctors. Read and write share one rule, and out-of-scope records
report 404 so other environments are never disclosed.
Tests: 29 new cases across the three areas (clinic appointment access, confirm
flow, clinic record access). Full suite 466 tests, 2 pre-existing failures
unchanged. API docs updated for all three.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
136 lines
5.6 KiB
PHP
136 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Appointment\Security;
|
|
|
|
use App\Appointment\Entity\Appointment;
|
|
use App\Auth\Entity\User;
|
|
use App\Auth\Repository\UserActiveContextRepository;
|
|
use App\Clinic\Repository\ClinicRepository;
|
|
use App\Clinic\Security\ClinicDoctorPermissionChecker;
|
|
use App\Doctor\Repository\DoctorRepository;
|
|
use App\Secretary\Repository\DoctorSecretaryRepository;
|
|
use App\Secretary\Security\SecretaryPermissionChecker;
|
|
|
|
/**
|
|
* تنها تصمیمگیرندهٔ دسترسی روی «یک نوبت مشخص».
|
|
*
|
|
* پیش از این، مسیرهای تکنوبت فقط بیمار، پزشکِ مالک و ادمین را میشناختند؛ نوبتی که
|
|
* کاربر کلینیک از مسیر /my/appointment میساخت، روی مشاهده و ویرایش ۴۰۳ میگرفت.
|
|
* محیط نوبت با appointment.clinic بیان میشود (NULL یعنی مطب شخصی) و همان مبنای
|
|
* تصمیم است — نه نقش کاربر.
|
|
*
|
|
* اکشنها از همان واژگان ClinicDoctorPermission/DoctorSecretary گرفته شدهاند تا
|
|
* «پایان همکاری» فقط یک منبع حقیقت داشته باشد: active=false در همان رکوردها.
|
|
*/
|
|
class AppointmentAccessChecker
|
|
{
|
|
public const ACTION_VIEW = 'view';
|
|
public const ACTION_UPDATE_STATUS = 'update_status';
|
|
public const ACTION_CANCEL = 'cancel';
|
|
|
|
private const RESOURCE = 'appointments';
|
|
|
|
public function __construct(
|
|
private readonly ClinicDoctorPermissionChecker $clinicPermissions,
|
|
private readonly SecretaryPermissionChecker $secretaryPermissions,
|
|
private readonly DoctorSecretaryRepository $secretaryRepo,
|
|
private readonly UserActiveContextRepository $contextRepo,
|
|
private readonly ClinicRepository $clinicRepo,
|
|
private readonly DoctorRepository $doctorRepo,
|
|
) {}
|
|
|
|
public function canView(Appointment $appointment, User $user): bool
|
|
{
|
|
return $this->can($appointment, $user, self::ACTION_VIEW);
|
|
}
|
|
|
|
/** مجوز تغییر نوبت: ویرایش، جابهجایی، رزرو، جایگزینی و تغییر وضعیت. */
|
|
public function canManage(Appointment $appointment, User $user): bool
|
|
{
|
|
return $this->can($appointment, $user, self::ACTION_UPDATE_STATUS);
|
|
}
|
|
|
|
public function canCancel(Appointment $appointment, User $user): bool
|
|
{
|
|
return $this->can($appointment, $user, self::ACTION_CANCEL);
|
|
}
|
|
|
|
public function can(Appointment $appointment, User $user, string $action): bool
|
|
{
|
|
if ($user->hasRole('ROLE_ADMIN')) {
|
|
return true;
|
|
}
|
|
|
|
if ($appointment->getDoctor()->getUser()->getId() === $user->getId()) {
|
|
return true;
|
|
}
|
|
|
|
// بیمار نوبت خودش را میبیند و لغو میکند، ولی جابهجا/ویرایش نمیکند.
|
|
if ($appointment->getUser()->getId() === $user->getId()) {
|
|
return $action === self::ACTION_VIEW || $action === self::ACTION_CANCEL;
|
|
}
|
|
|
|
$clinic = $appointment->getClinic();
|
|
if ($clinic !== null && $this->clinicPermissions->can($user, $clinic, self::RESOURCE, $action)) {
|
|
return true;
|
|
}
|
|
|
|
return $this->secretaryCan($appointment, $user, $action);
|
|
}
|
|
|
|
/**
|
|
* کلینیکی که این کاربر در آن اجازهٔ دیدن نوبتهای این پزشک را دارد، یا null.
|
|
* برای لیستهایی که باید به یک محیط محدود شوند (نه تکنوبت).
|
|
*/
|
|
public function viewableClinicFor(User $user, \App\Doctor\Entity\Doctor $doctor): ?\App\Clinic\Entity\Clinic
|
|
{
|
|
$dbUuid = $this->contextRepo->findByUser($user)?->getDbUuid();
|
|
$clinic = $dbUuid !== null ? $this->clinicRepo->findByUuid($dbUuid) : null;
|
|
|
|
if ($clinic === null) {
|
|
$clinic = $this->clinicRepo->findByUser($user);
|
|
}
|
|
|
|
if ($clinic === null || !$clinic->hasDoctor($doctor)) {
|
|
return null;
|
|
}
|
|
|
|
return $this->clinicPermissions->can($user, $clinic, self::RESOURCE, self::ACTION_VIEW)
|
|
? $clinic
|
|
: null;
|
|
}
|
|
|
|
/**
|
|
* منشی در محیط فعالِ خودش. در محیط کلینیک، نوبت باید هم متعلق به همان کلینیک
|
|
* باشد و هم پزشکش جزو پزشکان تخصیصیافته به این منشی — عضویت در کلینیک بهتنهایی
|
|
* یعنی منشیِ یک پزشک بتواند نوبت پزشک دیگری را دستکاری کند.
|
|
*/
|
|
private function secretaryCan(Appointment $appointment, User $user, string $action): bool
|
|
{
|
|
$dbUuid = $this->contextRepo->findByUser($user)?->getDbUuid();
|
|
if ($dbUuid === null) {
|
|
return false;
|
|
}
|
|
|
|
$clinic = $this->clinicRepo->findByUuid($dbUuid);
|
|
if ($clinic !== null) {
|
|
if ($appointment->getClinic()?->getId() !== $clinic->getId()) {
|
|
return false;
|
|
}
|
|
|
|
$relation = $this->secretaryRepo->findActiveClinicRow($user, $clinic, $appointment->getDoctor());
|
|
|
|
return $relation !== null && $this->secretaryPermissions->can($relation, self::RESOURCE, $action);
|
|
}
|
|
|
|
$doctor = $this->doctorRepo->findByUuid($dbUuid);
|
|
if ($doctor === null || $doctor->getId() !== $appointment->getDoctor()->getId()) {
|
|
return false;
|
|
}
|
|
|
|
$relation = $this->secretaryRepo->findActiveBySecretaryForDoctor($user, $doctor);
|
|
|
|
return $relation !== null && $this->secretaryPermissions->can($relation, self::RESOURCE, $action);
|
|
}
|
|
}
|