Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How would you refactor the following permission checking to a better one?

Currently the way users are checked if they have permission to access some resource is done using Middlewares:

  1. There is a DB table with list of available permissions
  2. Then there is another DB table that matches users and their corresponding permission.(users can have many permissions)
  3. For each permission there is a Middleware file, that all it does is to match the authenticated user's list of permissions against the specific permission file check. So for example Permission_X_Middleware.php will check if the current user has permission X in the list of his permissions.
  4. Every Middleware file created is registered in the Kernel.php
  5. For the roue that needs permission, the Middleware is attached:
Route::get('/user', 'SomeController@index')->middleware('Permission_X_Middleware');
  1. the middleware either approves or redirects the user to the unauthorized page.
  • There are many different pages and many users who have different permissions that's why the need for a lot of permission types

But the entire way of checking permission as it's done above does not look the best to me. How can I make it better?

0 likes
2 replies

Please or to participate in this conversation.