Laravel 5.5 permissions based on user id

For a Laravel 5.5 project I solved a permission problem that i’d like to share in case someone else can benefit.

Problem:

The structure of our urls look like this:

users/5/companies

Which will show all the companies for user 5. I however wanted to limit it so that only user 5 may see user/5 company listings. User/4 or another use should see access denied when viewing /users/5/compaines.

My current route was defined this way in routes/web.php:

Solution:

Use middleware attached to a route group with prefix to do the check. Although you can accomplish this in a controller, you are better off using middleware. Middleware intercepts the request before it even its your controllers. So you can stop it at an earlier stage.

Step 1. Create Middleware. I’ll call it SectionOwner, you can name it as you like. Run this command from the command line. This command will create your middleware and give you some boilerplate code.

Step 2. Visit Your SectionOwner Middlewarefile located in : /app/Http/Middleware. Change the handle function from:

To

In my cause, I can simply compare id of the requester with route param {user}

Step 3.  Tell Laravel Kernal.php about the new Middleware so that we can use it on our routes.

Look in your kernal.php and find the array for $routeMiddleware. You will have to add your MiddleWare with the full namespace.  I added it and aliased it as section.owner

Step 4. Add to routes

Now we can use our middleware in our web.php route file. I will also add the auth middleware so that if the user is not logged in it will ask them to login.

My web.php now becomes:

Now you have it,  the path users/5/companies is now secure and only user/5/ may access it.

Optional: You may also add the middleware to the entire prefix group so that users/5/* is only accessible by user 5.

 

Bonus

We also have roles that are used for permissions. We wanted admins to be able to access any user’s company listing. I Added a permissions check to allow admins access.

Warning: You still have to secure your company, edit, delete, store, etc… routes. I’ve done that separately outside of this post.

Thanks for reading! I hope you found this helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *