Download the PHP package nexara/api-platform-voter without Composer
On this page you can find all versions of the php package nexara/api-platform-voter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nexara/api-platform-voter
More information about nexara/api-platform-voter
Files in nexara/api-platform-voter
Package api-platform-voter
Short Description Symfony bundle that enforces voter-based authorization for API Platform 3.
License MIT
Homepage https://github.com/nexara-group/api-platform-voter
Informations about the package api-platform-voter
Nexara API Platform Voter
A Symfony bundle that enforces consistent voter-based authorization for API Platform 3 resources.
Features
Core Features
- ✅ Opt-in security per resource via
#[Secured]attribute - ✅ Automatic CRUD mapping to voter attributes (
{prefix}:list,{prefix}:create, etc.) - ✅ Custom operation support with explicit voter methods
- ✅ UPDATE operations receive both new and previous objects for comparison
- ✅ Flexible configuration with customizable prefixes and targeted voters
- ✅ Type-safe with PHP 8.1+ and strict types
- ✅ Well-tested with comprehensive test coverage
Advanced Features (v0.3+)
- 🧪 Testing utilities with role hierarchy support (
VoterTestTrait,SecurityBuilder) - ⚙️ Flexible operation mapping with configurable naming conventions
- 🔒 Automatic custom provider security with opt-in/opt-out configuration
- 🐛 Debug tools with voter chain visualization
- 📊 Validation commands for voter implementations
- 🔄 Migration helpers from native API Platform security
- 🌐 GraphQL support with field-level authorization
- 🏢 Multi-tenancy with automatic tenant context injection
- ⚡ Performance optimizations with lazy loading and caching
- 🛠️ Maker command with pre-defined templates
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or 7.0+
- API Platform 3.0+
Installation
The bundle will be automatically registered in config/bundles.php.
Quick Start
1. Mark Your Resource as Protected
Add the #[Secured] attribute to your API Platform resource:
2. Create a Voter
Use the maker command to generate a voter:
Or create one manually with 3 configuration modes (v0.3+):
Mode 1: Auto-Configuration (Recommended)
Mode 2: Fluent Builder (Modern)
Mode 3: Manual (Backward Compatible)
3. That's It!
Your API Platform resource is now protected by the voter. All CRUD operations will be automatically checked.
Operation Mapping
The bundle automatically maps API Platform operations to voter attributes:
| Operation | HTTP Method | Voter Attribute | Voter Method | Subject |
|---|---|---|---|---|
| Collection GET | GET /articles |
article:list |
canList() |
null |
| Collection POST | POST /articles |
article:create |
canCreate() |
New object |
| Item GET | GET /articles/{id} |
article:read |
canRead($object) |
Object |
| Item PUT/PATCH | PUT /articles/{id} |
article:update |
canUpdate($new, $previous) |
[$new, $previous] |
| Item DELETE | DELETE /articles/{id} |
article:delete |
canDelete($object) |
Object |
| Custom operation | POST /articles/{id}/publish |
article:publish |
canPublish($object, $previous) |
Object or [$new, $previous] |
Custom Operations
For custom operations, implement a method following the naming convention can{OperationName}:
Voter Configuration Modes (v0.3+)
The unified CrudVoter supports 3 configuration modes:
1. Auto-Configuration (Zero Config)
2. Fluent Builder (Modern API)
3. Manual Configuration (Backward Compatible)
Migration from v0.2.x: See VOTER_MIGRATION_GUIDE.md
Configuration
Create config/packages/nexara_api_platform_voter.yaml:
Attribute Options
#[Secured] Parameters
prefix(optional): Custom prefix for voter attributes. Defaults to lowercase resource class name.voter(optional): Specific voter class to use. When set, only this voter can grant access.
Advanced Usage
Accessing the User
Inject Symfony's Security service to access the current user:
Comparing Previous and New Objects
For UPDATE operations, you receive both the new and previous state:
Multiple Resource Classes
A single voter can handle multiple resource classes:
GraphQL Support
For GraphQL APIs, use GraphQLCrudVoter with field-level authorization:
Multi-Tenancy
For multi-tenant applications, use TenantAwareVoterTrait:
Debug & Troubleshooting
Visualize voter decision chains:
Enable debug mode in configuration:
Testing
Testing Your Voters
The bundle provides powerful testing utilities with full role hierarchy support:
Using VoterTestTrait
Using SecurityBuilder
Using VoterTestCase
Running Tests
The bundle includes a comprehensive test suite:
Console Commands
Validate Voter Implementations
Validates:
- ✅ CRUD method implementations
- ✅ Custom operation methods
- ✅ VoterRegistry registration
- ✅
#[Secured]attribute on resources - ✅ Test coverage
- ✅ Method signatures
Analyze Migration from Native Security
Provides:
- 📊 Analysis of resources with native security expressions
- 📋 Step-by-step migration plan
- ⏱️ Estimated migration time
- 🎯 Complexity assessment
Quality Assurance
This bundle maintains high code quality standards:
- PHPStan (level 8) for static analysis
- ECS for code style (PSR-12, Clean Code)
- Rector for automated refactoring
- PHPUnit for testing
Contributing
Contributions are welcome! Please read CONTRIBUTING.md for details.
Security
If you discover a security vulnerability, please review our Security Policy.
License
This bundle is released under the MIT License.
Credits
Developed and maintained by Nexara s.r.o.
Support
All versions of api-platform-voter with dependencies
symfony/framework-bundle Version ^6.4 || ^7.0
symfony/security-bundle Version ^6.4 || ^7.0
symfony/cache Version ^6.4 || ^7.0
psr/cache Version ^3.0
api-platform/core Version ^3.0