Download the PHP package sybio/permission-bundle without Composer
On this page you can find all versions of the php package sybio/permission-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sybio/permission-bundle
More information about sybio/permission-bundle
Files in sybio/permission-bundle
Package permission-bundle
Short Description A Symfony bundle to manage permissions as Value Objects.
License MIT
Homepage https://github.com/Sybio/permission-bundle
Informations about the package permission-bundle
Permission Bundle
A Symfony bundle to manage permissions (authorizations) as Value Objects, with an integrated validation system and native integration with Symfony's security system.
Introduction
This bundle provides a modern and type-safe approach to managing permissions in your Symfony applications. Instead of using strings or constants, permissions are represented as immutable Value Objects, each having its own checker (equivalent to a handler) that contains the business logic for validation.
This approach offers better maintainability, better testability, and better IDE integration thanks to strong typing.
Advantages
✅ Type-safe : Permissions are typed objects, not strings
✅ Separation of concerns : Each permission has its own dedicated checker
✅ Symfony integration : Compatible with the security (Voter) and validation systems
✅ Auto-configuration : Checkers are automatically detected and registered
✅ Testability : Easy to test thanks to dependency injection
✅ Maintainability : Easy to extend with new permissions and maintain over time
Installation
If you're using Symfony Flex, the bundle will be automatically registered. Otherwise, register it manually in config/bundles.php:
Configuration
Configuration is optional. By default, the bundle uses the PERMISSION security attribute. You can customize it in config/packages/sybio_permission.yaml:
Creating a permission and its checker
1. Create the permission (Value Object)
Create a class that implements PermissionInterface:
2. Create the checker
Create a checker that implements PermissionCheckerInterface:
The checker is automatically detected and registered thanks to the bundle's auto-configuration.
Usage
Method 1: With the security system
Use the security system to check permissions:
Note that the attribute 'PERMISSION' is defined in the configuration and you can customize it.
You can also use the Permission class as attribute, it will work : $this->isGranted(EditArticlePermission::class, new EditArticlePermission(....
Then get violations list if needed:
The power of this bundle is that you can also pass to the view why access was denied by reading violations.
Example with a controller
Method 2: With PermissionValidator
Thanks to the validator component, you can validate permissions directly with validation:
Method 3: With Twig Extensions
The bundle provides two Twig functions to check permissions directly in your templates.
Function has_permission()
This function returns a boolean indicating whether the permission is granted or not.
Usage:
The function takes:
- First argument: The fully qualified class name of the permission (as a string)
- Remaining arguments: The arguments to pass to the permission's constructor
Function permission()
This function returns a decision object containing both the granted status and the violations list.
Usage:
The returned object has:
granted(bool): Whether the permission is grantedviolations(ConstraintViolationListInterface): The list of violations if the permission was denied
Complete example
Here's a complete example with a (little) more complex permission:
License
MIT
Author
Sybio (Clément Guillemain)
All versions of permission-bundle with dependencies
symfony/config Version ^7.1
symfony/dependency-injection Version ^7.1
symfony/http-kernel Version ^7.1
symfony/security-core Version ^7.1
symfony/twig-bundle Version ^7.1
symfony/validator Version ^7.1