duanpo1498 2017-04-10 11:16
浏览 52

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

How i do that when which type of users login its stay its own dashboard/profile. User and SuperAdmin is okay,but when i login with branchAdmin account and write other users url in browser it go on its dashboard it not stay in its own account.

Routes:

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

View:

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

Middlewares:

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条回答 默认 最新

  • doushi8187 2017-04-10 12:14
    关注

    in BranchAdmin class in handle method where you are redirecting user to branch you should check that the path of the url is not /branch like this.

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

    hope this helps :)

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题