Overview
TheRoleOrPermissionMiddleware class provides flexible route protection by checking if the authenticated user has either the specified roles OR the specified permissions. This allows for more versatile access control where either role-based or permission-based access grants entry.
Namespace: Spatie\Permission\Middleware\RoleOrPermissionMiddleware
Registration
Register the middleware in yourbootstrap/app.php file:
Methods
handle()
Handles the incoming request and verifies the user has the required role(s) or permission(s).$request- The incoming HTTP request$next- The next middleware closure$roleOrPermission- Role or permission name(s) as string or pipe-separated values$guard- Optional authentication guard name
- Retrieves the authenticated user from the specified guard
- Supports Passport client credentials for machine-to-machine authentication
- Verifies the user has both
HasRolesandHasPermissionstraits - Checks if the user has any of the specified values as either permissions OR roles (using pipe
|as separator) - Throws
UnauthorizedExceptionif user is not logged in, missing the traits, or lacks required roles/permissions
UnauthorizedException::notLoggedIn()- User is not authenticatedUnauthorizedException::missingTraitHasRoles($user)- User model lacksHasRolestraitUnauthorizedException::forRolesOrPermissions($rolesOrPermissions)- User lacks any of the required roles or permissions
using()
Helper method to programmatically specify roles/permissions and guard for the middleware.$roleOrPermission- Role or permission name(s) as string, array, or BackedEnum$guard- Optional authentication guard name
Usage Examples
Single Role or Permission
Allow access if user has either the role or permission:Multiple Values (OR Logic)
Allow access if user has ANY of the specified roles or permissions:Flexible Access Control
Combine roles and permissions for versatile authorization:With Custom Guard
Specify a custom authentication guard:Using the using() Method
Programmatically specify roles or permissions:Route Groups
Apply to multiple routes:With BackedEnum
Use PHP enums for type-safe definitions:Controller Usage
Apply in controller constructors:Practical Use Cases
Super Admin Bypass
Allow super admins to bypass specific permission checks:Role-Based with Permission Override
Grant access to specific roles, but also allow individual permission grants:Temporary Access
Grant temporary permissions without changing roles:Passport Client Credentials
The middleware supports Laravel Passport machine-to-machine authentication. Whenpermission.use_passport_client_credentials is enabled in config, the middleware will authenticate Passport clients using bearer tokens.
Combining with Other Middleware
Combine with other middleware for enhanced security:Difference from Other Middleware
vs RoleMiddleware
- RoleMiddleware: Checks ONLY roles
- RoleOrPermissionMiddleware: Checks both roles AND permissions (OR logic)
vs PermissionMiddleware
- PermissionMiddleware: Checks ONLY permissions
- RoleOrPermissionMiddleware: Checks both roles AND permissions (OR logic)
Notes
- The pipe character (
|) separates multiple values, implementing OR logic - User must have at least ONE of the specified values as either a role or permission
- The middleware requires the user model to use the
HasRolestrait - Checks both
canAny()for permissions andhasAnyRole()for roles - More flexible than using separate role or permission middleware
- Particularly useful when you want to grant access by role hierarchy OR specific permissions
- Both
hasAnyRoleandhasAnyPermissionmethods must exist on the user model