Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

Route::group(['middleware' => ['role:admin|coach']], function() {} - Multiple Roles not working #883

Open phoenixz0024 opened 6 years ago

phoenixz0024 commented 6 years ago

Hello,

Ive been using Entrust in my application. Ive done everything according to the installation.

Anyhow;

Route::group(['middleware' => ['role:admin|coach']], function() { // my routes }

Ive been logged in as an user with the role Admin. However I always get redirected to ('/'). Any suggestions?

My middleware handler function

`

<?php namespace Zizaco\Entrust\Middleware;
/**
 * This file is part of Entrust,
 * a role & permission management solution for Laravel.
 *
 * @license MIT
 * @package Zizaco\Entrust
 */

use Closure;
use Illuminate\Contracts\Auth\Guard;

class EntrustRole
{
    protected $auth;

    /**
     * Creates a new instance of the middleware.
     *
     * @param Guard $auth
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  Closure $next
     * @param  $roles
     * @return mixed
     */
    public function handle($request, Closure $next, $roles)
    {

        if ($this->auth->guest() || !$request->user()->hasRole(explode('|', $roles)) {

             return redirect('/');
        }

        return $next($request);
    }
}
mayuripansuriya commented 6 years ago

Hi @phoenixz0024 Are you getting any specific error?

phoenixz0024 commented 6 years ago

Thanks for answering, no I am not getting an error. It is just redirecting to ('/') so I assume hasRole() returns false; I've tested this and hasRole() returns true when i have either coach or the admin role.

Any other suggestions? I am using laravel 5.2

mayuripansuriya commented 6 years ago

have you included 'role' => \Zizaco\Entrust\Middleware\EntrustRole::class, in your kernel.php

phoenixz0024 commented 6 years ago

I've added everything according to the setup

  'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
       'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
       'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
mayuripansuriya commented 6 years ago

try putting dd($roles); in your middleware and let me know what u are getting there.

phoenixz0024 commented 6 years ago

dd($roles); in EntrustRole class outputs the following; "coach|manager"

mayuripansuriya commented 6 years ago

try dd($this->auth->guest() , $request->user()->hasRole(explode('|', $roles)) in middleware

phoenixz0024 commented 6 years ago

Output:

false true

mayuripansuriya commented 6 years ago

That means everything is working according to the response you are getting.Issue is something else. You are not getting redirected to ('/') route from middleware.you can check by putting dd(123) inside if and dd(456) outside if condition you will get 456 .

phoenixz0024 commented 6 years ago

Ok so according to this, it doesnt go to redirect('/') but its returning the $next($request);. What will be the next step to look for an error.

Do I have to mention the middleware in the __construct method?

mayuripansuriya commented 6 years ago

Now the only solution is you have to check line by line .So firstly go to the function from your route .And try to print there.If it is getting there or not.

phoenixz0024 commented 6 years ago

I think I fixed it. In my controller i had this in the __construct()

public function __construct()
    {
        $this->middleware('auth' );
        $this->middleware(['role:coach'] , ['except' => ['index','show']]);

    }

This isn't working, but if comment these its working... Could you explain this?

mayuripansuriya commented 6 years ago

yes you actually don't need this.