如何使用laravel管理中间件以使用自己的仪表板/配置文件保留不同的用户?

时间:2017-04-10 11:16:52

标签: php laravel

当我使用哪种类型的用户登录其自己的仪表板/配置文件时,我该怎么做。用户和SuperAdmin是可以的,但是当我使用branchAdmin帐户登录并在浏览器中写入其他用户URL时,它会在其仪表板上显示,而不是保留在自己的帐户中。

路线:

Route::group(['middleware'  =>  [ 'auth', 'isAdmin']], function(){
        Route::get('/profile','ProfileController@getIndex');
    });

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

    Route::group(['middleware'  =>  [ 'auth', 'isNotAdmin']], function(){
    Route::get('/Super/admin', 'AdminController@getIndex');
        });

查看:

 <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>

中间件:

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);
    }
}

BranchAdmin:

class BranchAdmin 
{                note-> (" This middleware is not working ")

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

kernal.php:

protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'check_auth' => \App\Http\Middleware\CheckIfUserIsAuthenticated::class,
        'isAdmin' => \App\Http\Middleware\UserIsAdmin::class,
        'isNotAdmin' => \App\Http\Middleware\UserIsNotAdmin::class,
        'isBranchAdmin' => \App\Http\Middleware\BranchAdmin::class
    ];

1 个答案:

答案 0 :(得分:0)

在用于将用户重定向到BranchAdmin的句柄方法的branch类中的

,您应该检查网址的路径是不是/branch

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

    if(Auth::user()->type === 'BranchAdmin' && $request->path() != 'branch'){
        return redirect('/branch');
    }
    return $next($request);
 }

希望这有助于:)