Adding a New Role to SecureHealth
This guide explains the step-by-step process for adding a new role to the SecureHealth application.
Overviewβ
The role system in SecureHealth controls access to patient data and system features. Roles are defined in multiple places throughout the application and must be updated consistently.
- SecureHealth installation
- Admin access to the system
- Understanding of Symfony security components
Step 1: Update Security Configurationβ
File: config/packages/security.yaml
1.1 Add Role to Access Controlβ
Add access control rules for your new role in the access_control section:
access_control:
# Example: Allow new role to access specific endpoints
- { path: ^/api/patients, methods: [GET], roles: [ROLE_DOCTOR, ROLE_NURSE, ROLE_RECEPTIONIST, ROLE_NEW_ROLE] }
1.2 Update Role Hierarchy (Optional)β
If your role should inherit permissions from another role, add it to the role_hierarchy section:
role_hierarchy:
ROLE_DOCTOR: [ROLE_NURSE, ROLE_RECEPTIONIST, ROLE_ADMIN]
ROLE_NURSE: [ROLE_RECEPTIONIST]
# Add your new role here if it should inherit permissions
ROLE_NEW_ROLE: [ROLE_RECEPTIONIST]
Step 2: Update Patient Documentβ
File: src/Document/Patient.php
2.1 Add Role-Based Data Accessβ
In the toArray() method (around line 230-250), add a new conditional block for your role:
// After existing role checks...
elseif (in_array('ROLE_NEW_ROLE', $roles)) {
// Define what data this role can access
$data['diagnosis'] = $this->getDiagnosis();
$data['medications'] = $this->getMedications();
// Add or remove fields based on role requirements
}
- Basic info:
firstName,lastName,email,phoneNumber,birthDate,createdAt - Medical data:
ssn,diagnosis,medications,notes,notesHistory - Administrative:
insuranceDetails,primaryDoctorId
Step 3: Update Security Voterβ
File: src/Security/Voter/PatientVoter.php
3.1 Add Role to Permission Checksβ
Update the checkPermission() method to include your new role in permission checks:
case self::VIEW:
// Add your role to appropriate permission checks
return in_array('ROLE_DOCTOR', $roles) ||
in_array('ROLE_NURSE', $roles) ||
in_array('ROLE_RECEPTIONIST', $roles) ||
in_array('ROLE_NEW_ROLE', $roles);
case self::VIEW_DIAGNOSIS:
case self::VIEW_MEDICATIONS:
// Control medical data access
return in_array('ROLE_DOCTOR', $roles) ||
in_array('ROLE_NURSE', $roles) ||
in_array('ROLE_NEW_ROLE', $roles);
PATIENT_VIEW- View basic patient infoPATIENT_CREATE- Create new patientsPATIENT_EDIT- Edit patient recordsPATIENT_DELETE- Delete patientsPATIENT_VIEW_DIAGNOSIS- View diagnosisPATIENT_EDIT_DIAGNOSIS- Edit diagnosisPATIENT_VIEW_MEDICATIONS- View medicationsPATIENT_EDIT_MEDICATIONS- Edit medicationsPATIENT_VIEW_SSN- View SSNPATIENT_VIEW_INSURANCE- View insurancePATIENT_EDIT_INSURANCE- Edit insurancePATIENT_VIEW_NOTES- View notesPATIENT_EDIT_NOTES- Edit notesPATIENT_ADD_NOTE- Add new notesPATIENT_UPDATE_NOTE- Update notesPATIENT_DELETE_NOTE- Delete notes
Step 4: Update PatientController Serializationβ
File: src/Controller/Api/PatientController.php
Add your new role to the role detection logic (around line 75-88):
if ($user) {
$userRoles = $user->getRoles();
if (in_array('ROLE_DOCTOR', $userRoles)) {
$userRole = 'ROLE_DOCTOR';
} elseif (in_array('ROLE_NURSE', $userRoles)) {
$userRole = 'ROLE_NURSE';
} elseif (in_array('ROLE_ADMIN', $userRoles)) {
$userRole = 'ROLE_ADMIN';
} elseif (in_array('ROLE_RECEPTIONIST', $userRoles)) {
$userRole = 'ROLE_RECEPTIONIST';
} elseif (in_array('ROLE_NEW_ROLE', $userRoles)) {
$userRole = 'ROLE_NEW_ROLE';
}
}
Repeat this pattern in all methods that serialize patient data: index(), show(), create(), update().
Step 5: Create Default User (Optional)β
File: src/Command/CreateUsersCommand.php
Add a default user for the new role in the $users array (around line 46-72):
$users = [
// ... existing users ...
[
'email' => 'newrole@example.com',
'username' => 'New Role User',
'password' => 'password',
'roles' => ['ROLE_NEW_ROLE']
]
];
Run the command to create the user:
php bin/console app:create-users --force
Step 6: Update Frontend (Optional)β
If your new role needs specific UI features:
6.1 Update Navigationβ
File: public/assets/js/navbar.js
Add role-specific navigation items based on the user's role.
6.2 Update Role Documentationβ
File: public/role-documentation.html
Add documentation for the new role's capabilities and responsibilities.
Key Principlesβ
- Least Privilege: Only grant access to data that the role needs to perform its duties
- Data Segregation: Medical data (diagnosis, medications, notes) should be separate from administrative data (insurance, demographics)
- Consistency: Update all relevant files to maintain system integrity
- Testing: Test the new role with sample users to ensure proper access control
Example: Adding ROLE_PHARMACISTβ
A pharmacist role that can view medications and allergies but not full diagnosis:
// 1. security.yaml: Add to access control for medication endpoints
// 2. Patient.php:
elseif (in_array('ROLE_PHARMACIST', $roles)) {
$data['medications'] = $this->getMedications();
$data['diagnosis'] = $this->getDiagnosis(); // Limited view
}
// 3. PatientVoter.php: Add to VIEW_MEDICATIONS permission
// 4. PatientController.php: Add to role detection chain
// 5. CreateUsersCommand.php: Add default pharmacist user
Testing Checklistβ
:::checklist Testing Requirements
- User can log in with new role
- User can access permitted endpoints
- User is denied access to restricted data
- Role hierarchy works correctly (if applicable)
- Audit logs record the role's actions
- Frontend displays appropriate UI for the role :::
Additional Files to Considerβ
When adding a new role, you may also need to update:
Controllersβ
src/Controller/Api/DashboardController.php- Add role-specific dashboard datasrc/Controller/Api/AuditLogController.php- Control audit log accesssrc/Controller/Api/AppointmentController.php- Appointment management permissions
Servicesβ
src/Service/PatientVerificationService.php- Role-based verification requirementssrc/Service/RAGChatbotService.php- AI chatbot access control
Testsβ
tests/Security/Voter/PatientVoterTest.php- Add tests for new role permissionstests/Integration/- Integration tests for role-specific workflows
Frontendβ
public/assets/js/dashboard.js- Dashboard functionality for the rolepublic/assets/js/navbar.js- Navigation menu itemspublic/patients.html- Patient management interfacepublic/role-documentation.html- Role-specific documentation
Security Considerationsβ
- Always follow the principle of least privilege
- Test thoroughly with sample users
- Ensure audit logging captures all role-based actions
- Consider HIPAA compliance implications for each role
- Document the role's purpose and data access requirements
- Review and update access controls regularly
Common Pitfallsβ
- Forgetting to update all relevant files - The role system spans multiple files
- Inconsistent permission logic - Ensure voter and controller logic match
- Missing frontend updates - UI should reflect role capabilities
- Inadequate testing - Test both positive and negative access scenarios
- Poor documentation - Document what each role can and cannot do