samedi 30 mars 2019

Unable to group the two Middlewares

I am trying to implement 3 user types in to my application, I am using the default Auth of Laravel.

I created 2 extra middlewares to acheive the to extra user(admin & Districtlogin) conditions along with user approval middleware, and using the default for laravel user

Admin middleware

class Admin
{
   tion handle($request, Closure $next)
    {
        if( Auth::check() && Auth::user()->isAdmin == 1)
        {
            return $next($request);
        }
            Auth::logout();
            return redirect('login')->with('error','You have not admin access');
        }
}

Districtlogin Middleware

class Districtlogin
{
    public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->isAdmin == 2) {
            return $next($request);
        }
        Auth::logout();
        return redirect('login')->with('error', 'You have not admin access');
    }
}

One additional middleware to check if the user is approved

class ApproveUser
{

    public function handle($request, Closure $next)
    {

        if( Auth::check() && Auth::user()->row_status == 1)
    {
        return $next($request);

    }
            return redirect('/userapprove');

    }

I am able to redirect them based on the login

Auth/LoginController

 public function redirectPath()
    {
        if (Auth::user()->isAdmin == 1 && Auth::user()->row_status == 1) {
            return '/adminindex';
        } elseif (Auth::user()->row_status == 0) {
            return '/userapprove';
        } elseif (Auth::user()->isAdmin == 2 && Auth::user()->row_status == 1) {
            return '/districtindex';
        }
        return '/engineerdashboard';
    }

Here is my

kernel.php

entry

'admin' => \App\Http\Middleware\Admin::class,
'districtlogin' => \App\Http\Middleware\Districtlogin::class,
'userapprove' => \App\Http\Middleware\ApproveUser::class,

I would like to group the admin and Districtlogin middleware.

I am using CRUD, I would like Districtlogin to have access to few controllers - index,store,update

When I try to group the middleware, I am redirected back to the login screen. Neither of the middleware works

Here's what I tried,

1)

Route::middleware(['admin'])->group(function () {

    //Admin Dashboard
    Route::resource('adminindex', 'AdminindexController')->middleware('districtlogin');
    Route::get('adminindex/new', 'AdminindexController@admindashboard');
});

2)

Route::middleware(['admin','districtlogin'])->group(function () {

    //Admin Dashboard
    Route::resource('adminindex', 'AdminindexController');
    Route::get('adminindex/new', 'AdminindexController@admindashboard');

});

3)

 Route::middleware(['admin',''])->group(function () {

        //Admin Dashboard
        Route::resource('adminindex', 'AdminindexController');
        Route::get('adminindex/new', 'AdminindexController@admindashboard');

        Route::middleware(['districtlogin'])->group(function () {
        Route::resource('adminindex', 'AdminindexController');

           });

    });

But when I use the 'OR' condition, I am able to access the pages

Route::group(['middleware' => ['admin' OR 'districtlogin']], function () {

    //Admin Dashboard
    Route::resource('adminindex', 'AdminindexController');
    Route::get('adminindex/new', 'AdminindexController@admindashboard');
});

The controllers are accessible by both the middlewares. But how do I limit the Crud functions based on the middleware.

I am completely clueless on how to achieve it.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2HXXIPy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire