fix(permissions): apply the patient and tag read gates to invited clinic doctors

PatientController::resolveScope and TenantTagController::guardTagView only ever
checked the secretary, while every write in both controllers already ran through
both checkers. So an invited clinic doctor with patients.view off got 200 with an
empty list where a secretary got 403 — one permission, two behaviours. No data
was exposed either way; tenant scoping emptied the result.

The fix is not canOrNonMember. That collapses two different situations: a
membership row switched to active=false means the collaboration ended, and
ClinicDoctorPermission::can() returns false for everything in that case too.
Routing it through the permission gate turned the existing 404 on a single record
into a 403, which confirms the record exists to someone who just lost access.
ClinicRecordAccessTest caught it.

isActiveMemberDenied() answers the narrower question — active member, permission
off — and leaves a deactivated row to the data scope, which closes it with a 404
and discloses nothing. A test now pins that distinction so it cannot be collapsed
again.

Tags keep the tags.view OR patients.view rule, now for both roles.

Verified live in three states: active with both off 403/403, deactivated not 403,
active with patients.view on 200/200.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-07 19:30:52 +03:30
co-authored by Claude Opus 5
parent 6a6852379d
commit d6de746938
7 changed files with 152 additions and 4 deletions
@@ -68,6 +68,24 @@ class ClinicDoctorAccessChecker
return $this->permissions->can($user, $clinic, $resource, $action);
}
/**
* پزشکِ عضوی که همکاری‌اش **فعال** است ولی این مجوز را ندارد.
*
* ردیفِ غیرفعال عمداً `false` می‌دهد: «پایان همکاری» با «مجوز خاموش» یکی نیست.
* اولی را دامنهٔ داده با ۴۰۴ می‌بندد (پرونده اصلاً در محیط او نیست و وجودش هم
* فاش نمی‌شود)؛ دومی ۴۰۳ است. جمع‌کردنشان در یک پاسخ، وجودِ رکورد را لو می‌دهد.
*/
public function isActiveMemberDenied(User $user, string $resource, string $action): bool
{
$context = $this->contextResolver->resolve($user);
if (!$context->isClinic()) {
return false;
}
return $this->permissions->isActiveMember($user, $context->clinic)
&& !$this->permissions->can($user, $context->clinic, $resource, $action);
}
/** 403 اگر پزشکِ عضو مجاز نباشد؛ سایر کاربران بدون تغییر عبور می‌کنند. */
public function denyUnlessGranted(User $user, string $resource, string $action): void
{
@@ -21,6 +21,26 @@ class ClinicDoctorPermissionChecker
private readonly DoctorRepository $doctorRepo,
) {}
/**
* پزشکی که ردیفِ همکاری‌اش با این کلینیک **فعال** است.
*
* مالک و ادمین «عضو» حساب نمی‌شوند — آن‌ها اصلاً با این مجوزها سنجیده نمی‌شوند.
* ردیفِ غیرفعال یعنی پایان همکاری، که با «مجوز خاموش» یکی نیست.
*/
public function isActiveMember(User $user, Clinic $clinic): bool
{
if ($user->hasRole('ROLE_ADMIN') || $clinic->getUser()->getId() === $user->getId()) {
return false;
}
$doctor = $this->doctorRepo->findByUser($user);
if ($doctor === null || !$clinic->hasDoctor($doctor)) {
return false;
}
return $this->permRepo->getOrCreate($clinic, $doctor)->isActive();
}
public function can(User $user, Clinic $clinic, string $resource, string $action): bool
{
if ($user->hasRole('ROLE_ADMIN') || $clinic->getUser()->getId() === $user->getId()) {