view() method to determine if the authenticated user should receive the update.
How It Works
For each harmonic broadcast, Chorus:- Loads the affected model instance
- Calls the policy’s
view()method with the authenticated user - Only sends the harmonic to users who pass the authorization check
- Automatically rejects unauthorized harmonics
Example Policy Implementation
- Belong to the same tenant as the message
- Have access to the platform where the message was posted
Authorization Flow
When a harmonic is created for a model update:- Harmonic Creation - A harmonic is generated for the model change
- Policy Check - For each connected user, Chorus calls the model’s policy
- User Filtering - Only authorized users receive the harmonic
- Broadcast - The harmonic is sent to authorized clients only
Best Practices
Efficient Authorization
- Keep authorization checks efficient (use database indexes for filter fields)
- Consider caching expensive authorization computations
- Use simple, fast database queries in policy methods
Security
- Always implement the
view()method in your policies for models using the Harmonics trait - Test authorization thoroughly with different user scenarios and edge cases
- Use existing authorization patterns from your application
Reusability
- Leverage existing authorization logic from your policies
- Keep policy methods consistent across your application
- Document authorization requirements for each model
Common Patterns
Tenant-Based Authorization
Role-Based Authorization
Relationship-Based Authorization
By implementing proper authorization policies, you ensure that your real-time synchronization remains secure while leveraging Laravel’s built-in authorization system.