2017-04-06 108 views
0

我已經創建了3個用戶('Super-Admin','Branch-Admin','User')。我試圖這樣做,當'超級管理員'登錄它沒有去其他2用戶儀表板用戶('分支管理,用戶')。但它顯示的頁面「太多重定向」,並且當我給瀏覽器中的任何其他用戶的URL時,它將其儀表板從它自己的儀表板重定向。和其他2個用戶一樣?如何使用laravel管理中間件?

路線:

Route::group(['middleware' => [ 'auth', 'isNotAdmin']], function(){ 
     Route::get('/profile','[email protected]'); 
    }); 

    Route::group(['middleware' => [ 'auth', 'isBranchAdmin']], function(){ 
     Route::get('/branch','[email protected]'); 
    }); 

    Route::group(['middleware' => [ 'auth', 'isAdmin']], function(){ 
    Route::get('/Super/admin', '[email protected]'); 
     }); 

查看:

<div class="col-xs-12 col-sm-12 col-md-3 col-lg-3"> 
    @if(Auth::check() && Auth::user()->type === 'User') 
     <ul class="nav nav-pills nav-stacked"> 
      <li role="presentation" class="active"> 
       <a id="bootstrap-overrides" href="/home"> 
        Home 
       </a> 
      </li> 
      <li role="presentation"> 
       <a id="bootstrap-overrides" href="/contact"> 
        Contact 
       </a> 
      </li> 
      <li role="presentation"> 
       <a id="bootstrap-overrides" href="/about"> 
        About 
       </a> 
      </li> 
      <li role="presentation"> 
       <a id="bootstrap-overrides" href="/blog"> 
        Blog 
       </a> 
      </li> 
      <li role="presentation"> 
       <a id="bootstrap-overrides" href="/faqs"> 
        FAQs 
       </a> 
      </li> 
     </ul> 
    @elseif(Auth::check() && Auth::user()->type === 'Admin') 
     <ul class="nav nav-pills nav-stacked"> 
      <li role="presentation" @if(Request::path() === 'companies') class="active" @endif> 
       <a href="/companies"> 
        Companies 
       </a> 
      </li> 
      <li role="presentation" @if(Request::path() === 'branchies') class="active" @endif> 
       <a href="/branchies"> 
        Branchies 
       </a> 
      </li> 
     </ul> 
    @elseif(Auth::check() && Auth::user()->type === 'BranchAdmin') 
     <ul class="nav nav-pills nav-stacked"> 
      <li role="presentation" @if(Request::path() === 'medicines') class="active" @endif> 
       <a href="/medicines"> 
        Medicines 
       </a> 
      </li> 
      <li role="presentation" @if(Request::path() === 'stock') class="active" @endif> 
       <a href="/stock"> 
        Stock_details 
       </a> 
      </li> 
     </ul> 
    @endif 
</div> 

中間件:

BranchAdmin:

class BranchAdmin 
{ 
    public function handle($request, Closure $next){ 
     if(Auth::user()->type === 'BranchAdmin'){ 
      return redirect('/branch/'.Auth::user()->branch->id); 
     } 
     return $next($request); 
    } 
} 

UserIsAdmin:

class UserIsAdmin 
{ 
    public function handle($request, Closure $next) 
    { 
     if(Auth::user()->type === 'Admin'){ 
      return redirect('/Super/admin'); 
     } 
     return $next($request); 
    } 
} 

UserIsNotAdmin:

class UserIsNotAdmin 
{ 
    public function handle($request, Closure $next) 
    { 
     if(Auth::user()->type === 'User'){ 
      return redirect('/profile'); 
     } 
     return $next($request); 
    } 
} 

回答

1

中間件的邏輯似乎並不正確。我認爲你應該有太多的重定向。我以一箇中間件爲例。

class UserIsNotAdmin 
{ 
    public function handle($request, Closure $next) 
    { 
     if(Auth::user()->type === 'User'){ 
      return redirect('/profile'); 
     } 
     return $next($request); 
    } 
} 

你所說的是

If the user is of type 'User', always redirect them to '/profile' 

因此,如果鍵入「用戶」的用戶去http://website/profile,它一直把它們放在剖析了一遍又一遍。

你應該做的是實際做中間件的用途:阻止入侵者:)。 E.g:在UserIsNotAdmin中間件,這樣做

if(!Auth::user()->type === 'User'){ 
    redirect('/home'); 
} 
return $next($request); 

轉化爲

if the user IS NOT of type 'User', send them home. Else, let them in.