Multi Auth Laravel 5.4仅使用两个中间件

时间:2017-06-16 13:48:34

标签: php laravel authentication laravel-5 laravel-middleware

我想创建两个中间件来重定向经过身份验证的用户,如果是管理员,它将被重定向到后台,否则它将被重定向到简单用户的简单仪表板。

但是我想只使用users表而不为管理员添加另一个表。

RedirectIfAuthenticated.php

<?php

 class RedirectIfAuthenticated
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        if (Auth::user()->role_id == 1)
        {
            return redirect('/admin/home');
        }
        return redirect('/dashboard');
    }

    return $next($request);
}
}

DashboardController.php

class DashboardController extends Controller
{

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    return view('authFront.layoutAuthenticatedUser.dashboard');
}


}

web.php

    Route::get('/us-admin', function () { return redirect('/admin/home');    })->name('admin.dashboard');

   // Authentication Routes...
   $this->get('login',    'Auth\LoginController@showLoginForm')->name('auth.login');
    $this->post('login', 'Auth\LoginController@login')->name('auth.login');
   $this->post('register', 'Auth\RegisterController@register')->name('auth.register');

   $this->post('logout', 'Auth\LoginController@logout')->name('auth.logout');


   // Change Password Routes...
   $this->get('change_password',   'Auth\ChangePasswordController@showChangePasswordForm')->name('auth.change_password');
   $this->patch('change_password', 'Auth\ChangePasswordController@changePassword')->name('auth.change_password');

   // Password Reset Routes...
   $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('auth.password.reset');
   $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('auth.password.reset');
  $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
  $this->post('password/reset', 'Auth\ResetPasswordController@reset')->name('auth.password.reset');

   Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
    Route::get('/home', 'HomeController@index');

});
   Route::get('/dashboard', 'DashboardController@index');

2 个答案:

答案 0 :(得分:1)

在这种情况下,您需要建立用户角色,或者在users表的is_admin上有一个可选的布尔字段。然后在中间件中,它就像是:

let ageForRhr: [CountableClosedRange<Int>] = [18...25,26...35,36...45,46...55,56...65,65...100]

let menRhrScore: [[CountableClosedRange<Int>]] = [ [49...55 , 56...61,62...65,66...69,70...73,74...81,82...200] as [CountableClosedRange<Int>],
                [49...54 , 55...61,62...65,66...70,71...74,75...81,82...200] as [CountableClosedRange<Int>],
                [50...56 , 57...62,63...66,67...70,71...75,76...82,83...200] as [CountableClosedRange<Int>],
                [50...57 , 58...63,64...67,68...71,72...76,77...83,84...200] as [CountableClosedRange<Int>],
                [51...56 , 57...61,62...67,68...71,72...75,76...81,82...200] as [CountableClosedRange<Int>],
                [50...55 , 56...61,62...65,66...69,70...73,74...79,80...200] as [CountableClosedRange<Int>]]

let womenRhrScore: [[CountableClosedRange<Int>]] = [ [54...60 , 61...65,66...69,70...73,74...78,79...84,85...200] as [CountableClosedRange<Int>],
                      [54...59 , 60...64,65...68,69...72,73...76,77...82,83...200] as [CountableClosedRange<Int>],
                      [54...59 , 60...64,65...69,70...73,74...78,79...84,85...200] as [CountableClosedRange<Int>],
                      [54...60 , 61...65,66...69,70...73,74...77,78...83,84...200] as [CountableClosedRange<Int>],
                      [54...59 , 60...64,65...68,69...73,74...77,78...83,84...200] as [CountableClosedRange<Int>],
                      [54...59 , 60...64,65...68,69...72,73...77,78...84,85...200] as [CountableClosedRange<Int>]]

因此,在您的情况下,我会为每个角色授权后创建一个单独的中间件。然后,您可以将AdminMiddleware应用于所有前缀为“admin”的路由,例如,将SimpleUserMiddleware应用于所有其他路由。

答案 1 :(得分:0)

您可以简单地检查RedirectIfAuthenticated中间件中默认已存在的角色,并通过创建管理中间件来限制路由

app/Http/Middleware/RedirectIfAuthenticated.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            if (Auth::user()->role_id == 1)
            {
                return redirect('/admin/home');    
            }
            return redirect('/dashboard');
        }

        return $next($request);
    }
}

app/Http/Middleware/AuthenticateAdmin.php

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class AuthenticateAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!Auth::check() || Auth::user()->role_id != 1)
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('login');
            }
        }
        return $next($request);
    }
}

app/Http/Kernal.php中,在$routeMiddleware数组

中添加此行
'admin' => \App\Http\Middleware\AuthenticateAdmin::class,

routes/web.php

Route::middleware('admin')->prefix('admin')->group(function() {
    Route::get('/home', 'HomeController@index')->name('admin.home');
});

app/Http/Controllers/Auth/LoginController.php中添加此功能

protected function authenticated(Request $request, $user)
{
    if ($user->role_id == 1) {
        return redirect()->intended(route('admin.home'));
    }
    return redirect()->intended(route('dashboard'));
}   

希望这有助于你