Sunday, March 11, 2018

Check auth with role type in middleware laravel5.5 - development

Check auth with role type in middleware laravel5.5

I know this is very common question but in am stuck in this. I am using Laravel 5.5 and developing an ERP. In ERP some URL only access by Super admin so i create a middleware.

namespace AppHttpMiddleware;

use Closure;
use IlluminateSupportFacadesAuth;

class CheckAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Auth::user();
        if($user->user_role_id == 1) {
            return $next($request);
        } else {
            return redirect('/');
        }

    }
}

I have register this middleware in Kernel.php

protected $routeMiddleware = [
        'auth' => IlluminateAuthMiddlewareAuthenticate::class,
        'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
        'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
        'can' => IlluminateAuthMiddlewareAuthorize::class,
        'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
        'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
        'admin' => AppHttpMiddlewareCheckAdmin::class
    ];

and usign this middleware in routes

$routes = [
    "students" => "StudentsController",
    "teachers" => "TeachersController",
    "courses" => "CoursesController",
    "subjects" => "SubjectsController",
    "colleges" => "CollegesController",
    "branches" => "BranchesController",
];
Route::group(['middleware' => ['web', 'auth']], function () use ($routes){
    Route::match(["get","post"],'/users','UsersController@index')->middleware('admin');
    Route::get('/users/status/{id}','UsersController@setStatus')->middleware('admin');
    Route::get('/users/delete/{id}','UsersController@delete')->middleware('admin');
    Route::match(["get","post"],'/users/add','UsersController@add')->middleware('admin');
    Route::match(["get","post"],'/users/edit/{id}','UsersController@edit')->middleware('admin');
    Route::match(["get","post"],'/users/view/{id}','UsersController@view')->middleware('admin');


    foreach($routes as $route => $class){
        Route::match(["get","post"],'/'.$route,$class.'@index');
        Route::get('/'.$route.'/status/{id}',$class.'@setStatus');
        Route::get('/'.$route.'/delete/{id}',$class.'@delete');
        Route::match(["get","post"],'/'.$route.'/add',$class.'@add');
        Route::match(["get","post"],'/'.$route.'/edit/{id}',$class.'@edit');
        Route::match(["get","post"],'/'.$route.'/view/{id}',$class.'@view');
    }

    // additions routes
    Route::match(["get","post"],'/courses/assign-subjects','CoursesController@assign_subjects');
    Route::match(["get"],'/courses/already-assign-subjects/{id}','CoursesController@already_assigned_subjects');
});

This middleware is not called with users/* routes. When i register this $middleware in kernel.php then its works but Auth::user() return null every time. So how can i check logged in user role?
I read somewhere that in L5.5 version, Session not works in constructor then what is the best approach to check user role in middleware.



from Laravel Questions and Answers https://laravelquestions.com/php/check-auth-with-role-type-in-middleware-laravel5-5/
via Lzo Media

No comments:

Post a Comment