Overview
Nguhöe EHR implements role-based access control (RBAC) using the Spatie Laravel Permission package. The system defines four distinct user roles with specific permissions and access levels.User Roles
The system implements four primary roles:- Admin - Full system access and administrative privileges
- Doctor - Medical staff with patient care and consultation access
- Receptionist - Front desk staff managing appointments and payments
- Patient - Patients with access to their own medical information
Configuration
Spatie Permission configuration is located atconfig/permission.php.
Models
The Eloquent model used for permissions.
The Eloquent model used for roles.
Database Tables
Database table names for the permission system.
Column Names
The column name for the model’s primary key in pivot tables.
Cache Configuration
How long permissions are cached. Cache is automatically flushed when permissions or roles are updated.
Cache key prefix for storing permissions.
Cache store to use for permission caching.
Role Definitions
Admin Role
Administrators have full access to all system features. Capabilities:- Manage staff (create, edit, delete doctors and receptionists)
- View all patients and medical records
- Manage appointments
- Access financial reports
- Configure system settings
- View all consultations
- Manage payments
Doctor Role
Medical professionals with patient care responsibilities. Capabilities:- View and manage patients
- Create and view medical consultations
- Manage appointments
- Create prescriptions
- Manage their own schedule
- Upload patient attachments
Receptionist Role
Front desk staff managing administrative tasks. Capabilities:- Manage patient records
- Schedule and manage appointments
- Process payments
- Upload patient attachments
- View basic patient information
Patient Role
Patients with access to their own medical information. Capabilities:- View their own appointments
- View their own prescriptions
- Book new appointments
- View available appointment slots
- Download their own prescriptions
Permission Structure
Based on the route definitions inroutes/web.php, here are the main permissions:
Patient Management
view-patients- View patient list (Admin, Doctor, Receptionist)create-patients- Create new patients (Admin, Doctor, Receptionist)edit-patients- Edit patient information (Admin, Doctor, Receptionist)delete-patients- Delete patients (Admin)
Appointment Management
view-appointments- View appointments (Admin, Doctor, Receptionist)create-appointments- Create appointments (Admin, Doctor, Receptionist)edit-appointments- Edit appointments (Admin, Doctor, Receptionist)delete-appointments- Delete appointments (Admin)
Consultation Management
view-consultations- View consultations (Admin, Doctor)create-consultations- Create consultations (Admin, Doctor)edit-consultations- Edit consultations (Admin, Doctor)delete-consultations- Delete consultations (Admin)
Payment Management
view-payments- View payments (Admin, Receptionist)create-payments- Process payments (Admin, Receptionist)
Staff Management
view-staff- View staff list (Admin)create-staff- Create staff members (Admin)edit-staff- Edit staff information (Admin)delete-staff- Delete staff (Admin)
Reports
view-reports- Access system reports (Admin)
Schedule Management
manage-own-schedule- Manage doctor’s own schedule (Doctor)
Middleware
Nguhöe EHR uses role-based middleware to protect routes. Middleware aliases are configured inbootstrap/app.php:
Role Middleware
Restrict access to users with specific roles:Permission Middleware
Restrict access to users with specific permissions:Role or Permission Middleware
Restrict access to users with either a specific role or permission:User Model Setup
The User model must use theHasRoles trait:
Role Assignment
Assigning Roles to Users
Removing Roles
Checking Roles
Permission Assignment
Assigning Permissions to Roles
Assigning Permissions Directly to Users
Checking Permissions
Guards
Nguhöe EHR uses theweb guard for authentication, configured in config/auth.php:
web guard by default.
Database Seeding
Create roles and permissions in your database seeder:Authorization in Controllers
Authorize actions within controllers:Route-Level Authorization
Example fromroutes/web.php showing the multi-tier access structure:
Blade Directives
Check roles and permissions in Blade templates:Security Best Practices
- Principle of Least Privilege: Assign users only the roles and permissions they need
- Regular Audits: Periodically review user roles and permissions
- Role Separation: Don’t assign admin roles to regular staff unless necessary
- Permission Caching: Permissions are cached for 24 hours; clear cache after changes in production
- Database Protection: Ensure role and permission tables are not directly modifiable by users
Clearing Permission Cache
Troubleshooting
”User does not have the right roles” Error
Ensure the user has been assigned the required role:Permissions Not Working
- Clear permission cache:
php artisan cache:forget spatie.permission.cache - Verify User model uses
HasRolestrait - Check middleware is properly registered in
bootstrap/app.php - Ensure migrations have been run