entrust icon indicating copy to clipboard operation
entrust copied to clipboard

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

Open phoenixz0024 opened this issue 7 years ago • 13 comments

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);
	}
}

phoenixz0024 avatar Dec 15 '17 13:12 phoenixz0024

Hi @phoenixz0024 Are you getting any specific error?

mayuripansuriya avatar Dec 18 '17 09:12 mayuripansuriya

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

phoenixz0024 avatar Dec 18 '17 09:12 phoenixz0024

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

mayuripansuriya avatar Dec 18 '17 09:12 mayuripansuriya

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,

phoenixz0024 avatar Dec 18 '17 09:12 phoenixz0024

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

mayuripansuriya avatar Dec 18 '17 09:12 mayuripansuriya

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

phoenixz0024 avatar Dec 18 '17 09:12 phoenixz0024

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

mayuripansuriya avatar Dec 18 '17 09:12 mayuripansuriya

Output:

false true

phoenixz0024 avatar Dec 18 '17 10:12 phoenixz0024

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 .

mayuripansuriya avatar Dec 18 '17 10:12 mayuripansuriya

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?

phoenixz0024 avatar Dec 18 '17 10:12 phoenixz0024

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.

mayuripansuriya avatar Dec 18 '17 10:12 mayuripansuriya

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?

phoenixz0024 avatar Dec 18 '17 10:12 phoenixz0024

yes you actually don't need this.

mayuripansuriya avatar Dec 18 '17 10:12 mayuripansuriya