How mimmi20/mezzio-generic-authorization-rbac works
This project is a PHP library that provides a Laminas-RBAC adapter implementation for the mezzio-generic-authorization system. Its primary purpose is to supply role-based access control (RBAC) functionality within applications built using the Mezzio middleware framework. The design centers on integrating an RBAC system with mezzio’s authentication, using Laminas’ RBAC components and adding the possibility of dynamic assertions.
Main Components
• Library Core:
• ConfigProvider (src/ConfigProvider.php): Supplies RBAC configuration to the application.
• LaminasRbac (src/LaminasRbac.php): Likely a wrapper or adapter around the Laminas RBAC implementation.
• LaminasRbacAssertionInterface (src/LaminasRbacAssertionInterface.php): An extension of Laminas’ AssertionInterface to support PSR-7 requests.
• LaminasRbacFactory (src/LaminasRbacFactory.php): A factory class to create instances of the RBAC adapter using dependency injection.
• Testing:
• The tests directory shows unit tests for the key components (e.g., tests/ConfigProviderTest.php, tests/LaminasRbacTest.php).
Understand the Integration Points
• Mezzio Integration: The library is intended as an adapter for mezzio-generic-authorization and works with mezzio-authentication (adding the PSR-7 UserInterface attribute).
• Laminas RBAC Library: Underpins the actual RBAC functionality.
• Dynamic Assertions: The project supports dynamic authorization checks (involving HTTP request context) – a central part is extending or customizing the assertion logic (as illustrated by the EditorAuth example in the README).
Relationships and Interactions
• Configuration Flow: The ConfigProvider provides the required configuration (like roles and permissions) included in the application’s configuration file (as shown in the README example).
• Factory Instantiation: The factory (LaminasRbacFactory) is used to instantiate the LaminasRbac-based adapter; it pulls in the configuration and dependencies from a service container.
• Authorization Flow: When a request comes into the application, mezzio-authentication supplies user identity attributes; these are then checked against RBAC rules through the LaminasRbac implementation.
• Dynamic Assertion Handling: If a dynamic check is needed, the custom assertion interface (LaminasRbacAssertionInterface) is used to inspect the request and determine if access should be granted.
Architectural Patterns and Design Principles
• Dependency Injection: Usage of factories (e.g., LaminasRbacFactory) to create instances with their dependencies.
• Adapter Pattern: The library adapts the standard Laminas RBAC concepts to work within a mezzio authorization context.
• Separation of Concerns: Configuration, authorization checking, and service instantiation are clearly separated.