2017-04-06 106 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.