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 is available at: https://docs.dotkernel.org/dot-authorization/.
| 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.
Run this command in your project directory:
composer require dotkernel/dot-authorizationUsually, a concrete implementation installs this package as a dependency. You do not need to add it to your project.
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.
/**
* @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.
Role objects implement this interface. A role object returns its name, so the interface has one method.
public function getName(): string;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.
ExceptionInterface extends Throwable.
Catch it to catch each exception of this package.
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.