dotkernel / dot-authorization
Dotkernel authorization service abstractions
Requires
- php: ~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0
Requires (Dev)
- laminas/laminas-coding-standard: ^3.0
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Dotkernel authorization service abstractions.
dot-authorization is the base authorization package of Dotkernel. It defines the interfaces that authorization services for Dotkernel applications implement.
Documentation
Documentation is available at: https://docs.dotkernel.org/dot-authorization/.
Versions
| Version | PHP versions | Branch |
|---|---|---|
| 4.x | 8.3, 8.4, 8.5, 8.6 | 4.0 |
| 3.x | 8.2, 8.3, 8.4, 8.5 | 3.0 |
To upgrade from version 3, read the upgrade guide.
Badges
Installation
Run this command in your project directory:
composer require dotkernel/dot-authorization
Usually, a concrete implementation installs this package as a dependency. You do not need to add it to your project.
AuthorizationInterface
Each authorization service for Dotkernel applications implements this interface. By default, the Dotkernel authorization packages get the service from the container with the fully qualified name of this interface.
Methods
/** * @param array<string|RoleInterface> $roles */ public function isGranted(string $permission, array $roles = [], mixed $context = null): bool;
This is the only method for authorization.
The method gets a permission and a list of roles.
It returns true if one or more of the roles have the permission.
Each role is a role name (string) or a RoleInterface object.
The interface expects a role-based access control (RBAC) implementation.
RoleInterface
Role objects implement this interface. A role object returns its name, so the interface has one method.
public function getName(): string;
IdentityInterface
Entities that have roles implement this interface.
The getRoles() method returns the roles of the entity.
Each role is a role name or a role object, and one array can contain both.
This package is for RBAC authorization. Roles can be flat or hierarchical, and each role has permissions. A role is granted if it has the necessary permission.
Exceptions
ExceptionInterface extends Throwable.
Catch it to catch each exception of this package.
ForbiddenException
Throw this exception when a user accesses content without the necessary permissions.
An application can catch it to show a forbidden page or to redirect the user.
This package does not define how to handle the exception.
The default code is 403, and the previous exception can be any Throwable.
dot-rbac contains a concrete authorization service. dot-rbac-guard contains a handler for the forbidden exception. These are the default Dotkernel packages for authorization.