samedi 5 décembre 2015

Laravel 5.1 middleware help please

trying to make an app in laravel5.1. in my users table i save 3 types of user. Admin, Agent and farmer.in the users table there is a column named user_type_id where Admin is user_type_id=1,Agent is user_type_id=2, farmer is user_type_id=3. Admin has permission to do everything where Agent has few permission. Problem is while using middleware, my Authenticate.php and agentAuthenticate.php middleware files are acting as if they are the same. here is the code.

Authenticate.php

<?php

namespace App\Http\Middleware;

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


class AgentAuthenticate
{    
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('auth/login');
            }
        }

        if(! $this->auth->user()->user_type_id == 2) {
         return redirect()->guest('auth/login');   
        }
        return $next($request);
    }
}

AgentAuthenticate.php

<?php

namespace App\Http\Middleware;

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

class Authenticate
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('auth/login');
            }
        }

        if(! $this->auth->user()->user_type_id == 1) {
         return redirect()->guest('auth/login');   
        }
         return $next($request);
    }
}

routes.php

//guest routes
    Route::resource('/farmerPoint','farmerPointController',['only' => ['index', 'show']]);
    Route::resource('/crop','cropController',['only' => ['index', 'show']]);

    //Admin routes

    Route::group(['middleware' => 'auth'], function () {
    Route::resource('agent','agentController');
    Route::resource('farmer','farmerController');
    Route::resource('farmer.crop','farmerCropController');
    Route::resource('cropType','cropTypeController');
    Route::resource('crop','cropController',['except' => ['index','show']]);
    Route::resource('farmerPoint','farmerPointController',['except' => ['index','show']]);
    Route::get('/AdminPanel',function(){
       return view('frontend.AdminPanel');
      });
    });

    //agent routes
       Route::group(['middleware' => 'agent'], function () {
       Route::resource('farmer','farmerController');
       Route::resource('farmer.crop','farmerCropController');
       Route::resource('agent','agentController',['only' => ['index','show']]);
       Route::get('/AgentPanel',function(){
        return view('frontend.AgentPanel'); 
       });
    }); 
       Route::get('auth/login',function(){
    return view('auth/login');
  });
       Route::post('auth/login', 'Auth\AuthController@postLogin'); 
       Route::get('auth/logout', 'Auth\AuthController@getLogout'); 



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire