Alejandro Albornoz
Alejandro Albornoz

Reputation: 31

Laravel permission on same resource for multiple roles (Spatie)

I need to grant access to users with different roles to actions of one resource.

I tried the following but no luck in web.php routes file:

But when i declare the 2nd line the first in annulled.

Same thing happens when it's declared in the controller's construct method:

Last line permission prevails.

Any ideas?

Upvotes: 1

Views: 3455

Answers (2)

Alejandro Albornoz
Alejandro Albornoz

Reputation: 31

I can't get why this is working:

public function __construct()
{
    $this->middleware('role:Administrador|Supervisor')->except('index', 'show');
    $this->middleware('role:Administrador|Supervisor|Monitoreador|Coordinador');

}

when what it seems to do is the opposite of what i need.

I need the the roles Administrador and Supervisor can access all actions and roles Monitoreador y Coordinador can only access to index and show actions. But the way it works seems to declare that Administrador and Supervisor can access all except for index and show actions.

Why could this be happening?

Upvotes: 0

apokryfos
apokryfos

Reputation: 40673

Middleware must all pass before the request is processed. If you want index and show to pass one of the roles you need to explicitly put all roles in the parameters.

In your controller:

class TampaController extends Controller {

    public function __construct() {
          $this->middleware('role:Administrador|Supervisor');
          $this->middleware('role:Administrador|Supervisor|Monitoreador|Coordinador')->only('index', 'show');
    }
    // ...
}

Upvotes: 2

Related Questions