Add BackedEnum Support to RoleOrPermissionMiddleware #2890
+24
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was working on a multi-tenant inventory which has multiple roles and permissions where we decided to modernize our codebase by migrating from string-based permissions to PHP 8.1+ BackedEnums for better type safety and IDE autocompletion. We created a comprehensive enum structure:
Everything was going smoothly - we updated our routes to use enums with
PermissionMiddlewareandRoleMiddleware, and they worked perfectly:Then the problem hit: We had several routes that needed to check for either a role or a permission (e.g., "allow access if user is an admin OR has edit permission"). We tried to use
RoleOrPermissionMiddlewarewith our enums:Here I have added complete BackedEnum which is a PHP built-in interface in PHP 8.1+ versions to support to
RoleOrPermissionMiddleware, making it consistent with the other middleware classes in the package.