Files
clinicpro/src/Clinic/Security/ClinicDoctorAccessChecker.php
T
hamedandClaude Opus 5 1a7bf53577 refactor(tenant): make EntityContextResolver the single context resolver
Phase 1 of the tenant-marking series. The "which environment is this user
working in?" decision was reimplemented in six places, each reading
UserActiveContext.db_uuid and then guessing whether the uuid belongs to a
clinic or a doctor. Every copy was a place the roles could silently diverge.

EntityContextResolver already encoded the right precedence (explicit
clinic_uuid > stored active context > role) but only five files used it, and
it did not recognise secretaries at all: canActInClinic accepted admins,
clinic owners and member doctors, so a secretary's active clinic context
always collapsed to unknown. That gap is why SecretaryAccessChecker carried
its own copy of the logic.

- canActInClinic now also accepts an active DoctorSecretary relation, and a
  matching canActForDoctor covers the personal-practice branch.
- AppointmentAccessChecker, ClinicDoctorAccessChecker, SecretaryAccessChecker,
  PatientRecordScopeResolver, MyAppointmentsController and the secretary
  dashboard all resolve through it now.
- PatientRecordScopeResolver keeps only its real responsibility: which
  doctors' patients are visible inside the resolved environment.
- The resolver answers "where"; ClinicDoctorPermissionChecker and
  SecretaryPermissionChecker still answer "what may you do".

Left deliberately untouched, with the reason recorded at each site:
SubscriptionController, InventoryController and TenantTagController check
ROLE_DOCTOR unconditionally and ignore the active context, so a member doctor
sees personal inventory/tags/subscription even inside a clinic. Switching them
changes what users see, which is a product decision, not a refactor.
AuthController keeps its repository because it writes the active context.

tests/ApiTestCase now seeds the "free" subscription plan. db_test had no such
row, so getEffectivePlan returned null, every hasFeature() was false and 83
tests across Patient, ClinicService, Insurance and Appointment failed with 403.

No schema, route, request, response or error code changed.

Tests: 813 passing (was 730 passing / 83 failing). PHPStan clean on all
changed files.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 10:59:22 +03:30

79 lines
3.3 KiB
PHP

<?php
namespace App\Clinic\Security;
use App\Auth\Entity\User;
use App\Doctor\Repository\DoctorRepository;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Context\EntityContextResolver;
use App\Shared\Exception\AppException;
/**
* نقطهٔ واحدِ اعمالِ مجوزِ «پزشکِ عضوِ کلینیک» روی endpointهای چند-نقشه — قرینهٔ
* SecretaryAccessChecker اما برای ClinicDoctorPermission.
*
* فقط پزشکی که در محیطِ فعالش (UserActiveContext.db_uuid = کلینیک) عضوِ همان کلینیک
* است محدود می‌شود. بقیه — مالکِ کلینیک، ادمین، منشی، پزشکِ مطب شخصی، یا هر کاربری
* که محیطش کلینیک نیست — دست‌نخورده عبور می‌کنند (این checker آن‌ها را محدود نمی‌کند).
*/
class ClinicDoctorAccessChecker
{
public function __construct(
private readonly EntityContextResolver $contextResolver,
private readonly DoctorRepository $doctorRepo,
private readonly ClinicDoctorPermissionChecker $permissions,
) {}
/**
* idِ کلینیکی که این پزشک در محیطِ فعالش عضوِ آن است — برای کنترلرهایی که
* tenant را نقش‌محور حل می‌کنند و بدون این، پزشکِ عضو را به مطبِ شخصی‌اش می‌بردند
* (نه دادهٔ کلینیک). null اگر محیط کلینیک نیست یا کاربر عضو نیست.
*/
public function memberClinicId(User $user): ?int
{
$context = $this->contextResolver->resolve($user);
if (!$context->isClinic()) {
return null;
}
$doctor = $this->doctorRepo->findByUser($user);
return $doctor !== null && $context->clinic->hasDoctor($doctor) ? $context->id : null;
}
/**
* فقط پزشکِ عضوِ کلینیک را با ClinicDoctorPermission محدود کن؛ سایر کاربران true.
*/
public function canOrNonMember(User $user, string $resource, string $action): bool
{
$context = $this->contextResolver->resolve($user);
if (!$context->isClinic()) {
// محیطِ مطب شخصی یا نامشخص → این checker مالِ او نیست.
return true;
}
$clinic = $context->clinic;
// مالکِ کلینیک هرگز با مجوزهای عضویت قفل نمی‌شود.
if ($clinic->getUser()->getId() === $user->getId()) {
return true;
}
$doctor = $this->doctorRepo->findByUser($user);
if ($doctor === null || !$clinic->hasDoctor($doctor)) {
// غیرعضو (مثلاً منشی) — این checker مالِ او نیست.
return true;
}
return $this->permissions->can($user, $clinic, $resource, $action);
}
/** 403 اگر پزشکِ عضو مجاز نباشد؛ سایر کاربران بدون تغییر عبور می‌کنند. */
public function denyUnlessGranted(User $user, string $resource, string $action): void
{
if (!$this->canOrNonMember($user, $resource, $action)) {
throw new AppException(ErrorCodes::ERR_FORBIDDEN_001, null, 403);
}
}
}