lundi 20 juin 2016

how to assign a hasMany role check in Laravel

In my website

I have a InitiatorController which looks like thi

class InitiatorController extends Controller
{

 use ExchangerateTrait;
 public function __construct(){

    $this->middleware('auth');
    $this->middleware('role:sales'); // replace 'collector' with whatever role you need.
}

 public function getIndex(){


            return redirect('initiator/home');
 }

}

Right now After authenticating I am checking if the user role is sales or not in Role middleware .

My role middleware looks like this.

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

            if ($request->user()->role != $role){

                    if($role=="collector" || $role=="admin" )
                    return Redirect::to('/');  
                    if($role=="director" || $role=="admin" )
                    return Redirect::to('/');  
                    if($role=="admin1" || $role=="admin" )
                    return Redirect::to('/');
                    if($role=="admin2" || $role=="admin" )
                    return Redirect::to('/');
                    if($role=="sales" || $role=="admin" )
                     return Redirect::to('/');
                    if($role=="developer" || $role=="admin" )
                     return Redirect::to('/');


            }

            return $next($request);
        }

}

Now I have a situation where role is director but he is also a sales man , How do i tackle this.

First Idea in controller

if I some how send an array of roles to the middleware something like this

$roles = array('director,sales,teamlead');
$this->middleware('role:$roles');

and then in Middle ware I can check like

if(in_array ($request->user()->role,$roles)){
      //do some thing
}
else
{
   //redirect to login 
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire