Scope staff Care queries to the organization owner_ref.
Deploy Ladill Care / deploy (push) Successful in 1m11s
Deploy Ladill Care / deploy (push) Successful in 1m11s
Demo doctors were querying their own public_id, so branches/patients/queues looked empty even though the Pro tenant had data. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -11,7 +11,16 @@ use Illuminate\Http\Request;
|
|||||||
|
|
||||||
trait ScopesApiToAccount
|
trait ScopesApiToAccount
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Tenant data owner — always the organization owner_ref.
|
||||||
|
* Staff API tokens must scope to the employer tenant, not the staff public_id.
|
||||||
|
*/
|
||||||
protected function ownerRef(Request $request): string
|
protected function ownerRef(Request $request): string
|
||||||
|
{
|
||||||
|
return (string) $this->organization($request)->owner_ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function actorRef(Request $request): string
|
||||||
{
|
{
|
||||||
return (string) $request->user()->public_id;
|
return (string) $request->user()->public_id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,17 @@ use Illuminate\Http\Request;
|
|||||||
|
|
||||||
trait ScopesToAccount
|
trait ScopesToAccount
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Tenant data owner — always the organization owner_ref.
|
||||||
|
* Staff members must read/write the employer's records, not their own public_id.
|
||||||
|
*/
|
||||||
protected function ownerRef(Request $request): string
|
protected function ownerRef(Request $request): string
|
||||||
|
{
|
||||||
|
return (string) $this->organization($request)->owner_ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Acting user (audit / created_by), distinct from the tenant owner. */
|
||||||
|
protected function actorRef(Request $request): string
|
||||||
{
|
{
|
||||||
return (string) $request->user()->public_id;
|
return (string) $request->user()->public_id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,13 +149,13 @@ class MemberController extends Controller
|
|||||||
$this->authorizeAbility($request, 'admin.members.manage');
|
$this->authorizeAbility($request, 'admin.members.manage');
|
||||||
$this->authorizeOwner($request, $member);
|
$this->authorizeOwner($request, $member);
|
||||||
|
|
||||||
abort_if($member->user_ref === $this->ownerRef($request), 422, 'You cannot remove yourself.');
|
abort_if($member->user_ref === $this->actorRef($request), 422, 'You cannot remove yourself.');
|
||||||
|
|
||||||
$memberId = $member->id;
|
$memberId = $member->id;
|
||||||
$organizationId = $member->organization_id;
|
$organizationId = $member->organization_id;
|
||||||
$member->delete();
|
$member->delete();
|
||||||
|
|
||||||
AuditLogger::record($this->ownerRef($request), 'member.deleted', $organizationId, $this->ownerRef($request), Member::class, $memberId);
|
AuditLogger::record($this->ownerRef($request), 'member.deleted', $organizationId, $this->actorRef($request), Member::class, $memberId);
|
||||||
|
|
||||||
return redirect()->route('care.members.index')->with('success', 'Member removed.');
|
return redirect()->route('care.members.index')->with('success', 'Member removed.');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class BranchContext
|
|||||||
{
|
{
|
||||||
$scope = $this->organizations->branchScope($member);
|
$scope = $this->organizations->branchScope($member);
|
||||||
|
|
||||||
return Branch::owned((string) $request->user()->public_id)
|
return Branch::owned((string) $organization->owner_ref)
|
||||||
->where('organization_id', $organization->id)
|
->where('organization_id', $organization->id)
|
||||||
->where('is_active', true)
|
->where('is_active', true)
|
||||||
->when($scope, fn ($q) => $q->where('id', $scope))
|
->when($scope, fn ($q) => $q->where('id', $scope))
|
||||||
|
|||||||
@@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Http\Middleware\EnsurePlatformSession;
|
||||||
|
use App\Models\Appointment;
|
||||||
|
use App\Models\Branch;
|
||||||
|
use App\Models\Member;
|
||||||
|
use App\Models\Organization;
|
||||||
|
use App\Models\Patient;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class CareStaffTenantScopeTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_staff_doctor_sees_employer_queue_and_patients(): void
|
||||||
|
{
|
||||||
|
$this->withoutMiddleware(EnsurePlatformSession::class);
|
||||||
|
|
||||||
|
$owner = User::create([
|
||||||
|
'public_id' => 'demo-pro-owner',
|
||||||
|
'name' => 'Demo Pro Owner',
|
||||||
|
'email' => 'demo-pro@ladill.com',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$doctor = User::create([
|
||||||
|
'public_id' => 'demo-pro-doctor-pid',
|
||||||
|
'name' => 'Demo Pro Doctor',
|
||||||
|
'email' => 'demo-pro-doctor@ladill.com',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$organization = Organization::create([
|
||||||
|
'owner_ref' => $owner->public_id,
|
||||||
|
'name' => 'Demo Pro Clinic',
|
||||||
|
'slug' => 'demo-pro-clinic',
|
||||||
|
'timezone' => 'UTC',
|
||||||
|
'settings' => [
|
||||||
|
'onboarded' => true,
|
||||||
|
'plan' => 'pro',
|
||||||
|
'plan_expires_at' => now()->addMonth()->toIso8601String(),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
Member::create([
|
||||||
|
'owner_ref' => $owner->public_id,
|
||||||
|
'organization_id' => $organization->id,
|
||||||
|
'user_ref' => $owner->public_id,
|
||||||
|
'role' => 'hospital_admin',
|
||||||
|
'branch_id' => null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$branch = Branch::create([
|
||||||
|
'owner_ref' => $owner->public_id,
|
||||||
|
'organization_id' => $organization->id,
|
||||||
|
'name' => 'East Legon Care',
|
||||||
|
'is_active' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Member::create([
|
||||||
|
'owner_ref' => $owner->public_id,
|
||||||
|
'organization_id' => $organization->id,
|
||||||
|
'user_ref' => $doctor->public_id,
|
||||||
|
'role' => 'doctor',
|
||||||
|
'branch_id' => $branch->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$patient = Patient::create([
|
||||||
|
'uuid' => (string) \Illuminate\Support\Str::uuid(),
|
||||||
|
'owner_ref' => $owner->public_id,
|
||||||
|
'organization_id' => $organization->id,
|
||||||
|
'branch_id' => $branch->id,
|
||||||
|
'patient_number' => 'DEMO-8883-00001',
|
||||||
|
'first_name' => 'Ama',
|
||||||
|
'last_name' => 'Mensah',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Appointment::create([
|
||||||
|
'uuid' => (string) \Illuminate\Support\Str::uuid(),
|
||||||
|
'owner_ref' => $owner->public_id,
|
||||||
|
'organization_id' => $organization->id,
|
||||||
|
'branch_id' => $branch->id,
|
||||||
|
'patient_id' => $patient->id,
|
||||||
|
'type' => Appointment::TYPE_WALK_IN,
|
||||||
|
'status' => Appointment::STATUS_WAITING,
|
||||||
|
'scheduled_at' => now(),
|
||||||
|
'waiting_at' => now(),
|
||||||
|
'queue_position' => 1,
|
||||||
|
'reason' => 'Toothache',
|
||||||
|
'created_by' => $owner->public_id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->actingAs($doctor)
|
||||||
|
->get(route('care.queue.index'))
|
||||||
|
->assertOk()
|
||||||
|
->assertSee('East Legon Care')
|
||||||
|
->assertSee('Ama Mensah')
|
||||||
|
->assertSee('Toothache')
|
||||||
|
->assertDontSee('No patients waiting.');
|
||||||
|
|
||||||
|
$this->actingAs($doctor)
|
||||||
|
->get(route('care.patients.index'))
|
||||||
|
->assertOk()
|
||||||
|
->assertSee('Ama Mensah')
|
||||||
|
->assertSee('DEMO-8883-00001');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user