dimanche 6 décembre 2015

Laravel 5.1 Middleware users types redirections

I am trying to make an app in Laravel 5.1.

In my users table I have 3 types of users, 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 and 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, meaning agent is getting all the powers of admin. is there any logical error? here is the code.

agentAuthenticate.php (middleware)

 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 != 2) {
         return redirect()->guest('auth/login');   
        }
        return $next($request);
    } 

Authenticate.php

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 != 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'); 
       });
    });



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire