2017-06-16 60 views
0

我想創建兩個中間件來重定向經過身份驗證的用戶,如果它是管理員,它將被重定向到後臺,否則它將被重定向到簡單用戶的簡單儀表板。Multi Auth Laravel 5.4僅使用兩個中間件

但我想只使用用戶表而不添加管理員的其他表。

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\[email protected]')->name('auth.login'); 
    $this->post('login', 'Auth\[email protected]')->name('auth.login'); 
    $this->post('register', 'Auth\[email protected]')->name('auth.register'); 

    $this->post('logout', 'Auth\[email protected]')->name('auth.logout'); 


    // Change Password Routes... 
    $this->get('change_password', 'Auth\[email protected]')->name('auth.change_password'); 
    $this->patch('change_password', 'Auth\[email protected]')->name('auth.change_password'); 

    // Password Reset Routes... 
    $this->get('password/reset', 'Auth\[email protected]')->name('auth.password.reset'); 
    $this->post('password/email', 'Auth\[email protected]')->name('auth.password.reset'); 
    $this->get('password/reset/{token}', 'Auth\[email protected]')->name('password.reset'); 
    $this->post('password/reset', 'Auth\[email protected]')->name('auth.password.reset'); 

    Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function() { 
    Route::get('/home', 'Home[email protected]'); 

}); 
    Route::get('/dashboard', '[email protected]'); 
+1

主叫用戶簡單是不會爲你贏得任何朋友:)說真的,儘管除了一個好主意,你還有什麼其他的東西?你卡在哪裏?你面臨什麼問題? – Dale

+0

謝謝@戴爾,我有一個後臺辦公室已經存在的管理員,現在我想,如果用戶想通過我的網站的前端進行身份驗證,它將被重定向到一個小的後臺配置文件和其他元素我不希望用戶訪問管理員的後臺。 –

+0

只是讓列存儲用戶角色(管理員,超級管理員,用戶) –

回答

1

你需要建立在這種情況下,用戶角色,或有一個可選的布爾場在用戶表上爲is_admin。然後,在中間件它僅僅是這樣的:

class RedirectIfNotAdmin 
{ 
    /** 
    * 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::user()->admin()) { 
      return redirect()->to('user-dashboard'); 
     } 
     return $next($request); 
    } 
} 

所以你的情況我會爲每一個角色後授權一個獨立的中間件。然後,例如,您可以將AdminMiddleware應用於所有以「admin」,SimpleUserMiddleware爲前綴的路由。

+0

我已經設置了角色管理,在用戶表中帶有「role_id」字段的外鍵,但是我已經嘗試創建一個適用於簡單用戶但不起作用的middware,但它的工作方式不是 –

+0

,而是如何?你能幫忙嗎 –

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 array

'admin' => \App\Http\Middleware\AuthenticateAdmin::class, 

routes/web.php

Route::middleware('admin')->prefix('admin')->group(function() { 
    Route::get('/home', '[email protected]')->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')); 
} 

希望這有助於你

+0

小心這個,如果你將它應用到組中的所有路由,它會將你重定向回到內部頁面的角色主頁。 – btl

+0

這是不是您所使用申請每路線中間件,它是默認的中間件,當你進行身份驗證,檢查。要申請,你必須創建另一個簡單的管理中間件 –

+0

所有路由重定向我沒有找到 –

相關問題