Health Manager implements a granular permissions system that controls which users can view other users’ health data. The system is based on explicit permission grants stored in the user_permissions table.
// Grant doctor permission to view patient's data$patient->allowedViewers()->attach($doctor->id);// Revoke permission later$patient->allowedViewers()->detach($doctor->id);
// Grant caregiver access$elderly->allowedViewers()->attach($caregiver->id);// Get all people the caregiver is caring for$patientsUnderCare = $caregiver->accessibleUsers;
Deleting a user will remove all permissions where they are either the owner or the viewer. This ensures no orphaned permission records remain in the database.
The current implementation does not explicitly grant admins access to all user data through the permissions system. Admin privileges are enforced at the route/middleware level, not the data access level.
If you need admins to have universal data access, you can modify the canView() method:
public function canView($targetUserId){ // Allow admins to view all data if ($this->isAdmin()) { return true; } return $this->id === $targetUserId || $this->accessibleUsers()->where('owner_id', $targetUserId)->exists();}
// Method 1: Using allowedViewers relationship$owner->allowedViewers()->detach($viewer->id);// Method 2: Using accessibleUsers relationship$viewer->accessibleUsers()->detach($owner->id);// Revoke all permissions for an owner$owner->allowedViewers()->detach();// Revoke all permissions for a viewer$viewer->accessibleUsers()->detach();
// Check if viewer has permission for multiple owners$targetUserIds = [1, 2, 3, 4, 5];$accessibleIds = $viewer->accessibleUsers() ->whereIn('owner_id', $targetUserIds) ->pluck('owner_id') ->toArray();// Get all health records the viewer can access$healthRecords = HealthMeasurement::whereIn('user_id', function($query) use ($viewer) { $query->select('owner_id') ->from('user_permissions') ->where('viewer_id', $viewer->id);})->orWhere('user_id', $viewer->id)->get();