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

demonz's avatar

Do I need policies with spatie permission?

Hi guys , I assigned each user a role , and I use middleware role : admin for example for the admin routings in web.php So do i need to add also a policies inside controller functions ? Or it isn’t necessary since middleware role does the job ?

0 likes
4 replies
LaryAI's avatar
Level 58

If you are using Spatie Permission package, you can use the middleware to check if the user has the required role to access a specific route. In this case, you don't need to define policies for each controller function. However, if you want to add more complex authorization logic, you can use policies in addition to the middleware.

For example, you can define a policy to check if a user can update a specific resource based on some conditions. Then, you can use the authorize method in your controller function to check if the user is authorized to perform the action.

Here's an example of how to use policies with Spatie Permission package:

  1. Define a policy for a specific model:
namespace App\Policies;

use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class PostPolicy
{
    use HandlesAuthorization;

    public function update(User $user, Post $post)
    {
        return $user->hasRole('admin') || $user->id === $post->user_id;
    }
}
  1. Register the policy in the AuthServiceProvider:
namespace App\Providers;

use App\Models\Post;
use App\Policies\PostPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        Post::class => PostPolicy::class,
    ];

    public function boot()
    {
        $this->registerPolicies();
    }
}
  1. Use the policy in your controller function:
namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function update(Request $request, Post $post)
    {
        $this->authorize('update', $post);

        // Update the post
    }
}

In this example, the update method in the PostController checks if the user is authorized to update the post using the authorize method. The update method in the PostPolicy defines the authorization logic based on the user's role and the post's owner.

jlrdw's avatar

How would role work for users? Can users edit other users data?

Probably not, so authorization is also needed.

demonz's avatar

@jlrdw only admin role can edit other users data , and for example a regular user will only edit his own details because the edit function will retrieve the current authenticated User details . I am using middleware role to restrict the access to the pages , but do I need to add policies for the function in the controller ? Or it is just a second layer of authorization? , I think the middleware role is already done the job in the routing web.php

jlrdw's avatar

@demonz it depends on your query and what is in the query string.

If a user only edits their data, then the userid is part of the query in the WHERE clause.

And if any id information is passed in a query string, do you prevent someone changing the id. I recommend not passing sensitive information in a query string unless it's a know trusted admin. Rather as stated use the Auth id in the query.

All these things is part of authorization and authentication.

As for as policies or gates or custom authorization that is your choice. But Spatie has many examples in their documentation.

Please or to participate in this conversation.