Guards
Gates
Gates are closure based means it can be access from throughout of our application.
We define our Gates inside AuthserviceProvider under boot method. We can define many gates as we needed. we define gate for both roles and permissions.
Now use Gate inside controller method, where you want to authorize user action.
Policies
In the policies, we map policy class to the specific model (Eluquant model).
With the help of command we create policyClass like so.
php artisan make:policy PostPolicy --model=Post
Define policy in the controller for a particular action.
Define policy check inside policy class view method. here we are checking is user_id is equal to the post user_id if check success than it will return true and user can do this task.
Authorization function in laravel
But Laravel enables us to write policy to manage every policy check instead if writing checks in each method.
For that, we will use laravel Authorize function.
let make it make copy of the controller.php controller file and rename it to UserPolicyController.php
Now use it in controller method as
Comments
Post a Comment