Laravel: 如何确保用户在注册和登录后进入特定角色的主页视图?

Laravel: How to make sure a user goes to role-specific home view after registration and after login?

我在 laravel-5.8 中为一个大学网站创建了一个自定义的多授权系统,每个用户都注册了一个特定的角色:管理员、教员或 HOD(系主任)。

我正在使用我们在 运行

之后获得的控制器和视图
php artisan make:auth

注册后,所有用户都将重定向到默认主页,无论其角色如何。这 3 个角色作为 0、1 和 2 列角色存储在用户 table 中。我修改了默认的用户table。

我的登录机制运行良好。用户被重定向到其角色的主页。这是我的做法:

LoginController.php

public function login(Request $request){
if(Auth::attempt(['PID' => $request->PID, 'password' => $request->password])){

    $user = User::where('PID',$request->PID)->first();

    if($user->role == '0'){
        return redirect('home');
    }
    else{
        if($user->role == '1'){
            return redirect('hodhome');
        }
        else{
            return redirect('adminhome');
        }
    }
}
return redirect()->back();
}

这是 login.blade.php

的 POST 请求时使用的路由
Route::post('/login/custom', 'auth\LoginController@login')->name('login.custom');

但每次我注册一个新用户时,它都会将用户带到教师主页路径,即使他们是管理员或 HOD。这是注册机制:

RegisterController.php(以下仅展示相关功能)

use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
    $this->middleware('guest');
}
//validator function and create function after this line

我试图通过这样做使其正常工作:将变量 redirectTo 更改为函数。 //一个想法:将 "protected $redirectTo = '/home';" 替换为...

protected function redirectTo(array $data){
    if($data['role']=='0'){ return redirect('home'); }
    elseif($data['role']=='1'){ return redirect('hodhome'); }
    else return redirect('adminhome');
}

但是没有用,因为我好像漏掉了什么。

另一个遗憾是用户登录后可以访问其他角色的主页。我的路由中的 middleware('auth') 有助于防止在未登录时访问 URL,但它不会阻止登录用户访问其他主页。

主页路径如下:

Auth::routes();

//-------------------MULTI AUTH SYSTEM------------------------------------------

Route::post('/login/custom', 'auth\LoginController@login')->name('login.custom');

Route::get('/home', 'HomeController@index')->name('home');

route::get('/adminhome', function () {
    return view('adminhome');
})->middleware('auth');

route::get('/hodhome', function () {
    return view('hodhome');
})->middleware('auth');
//----------------------------------------------------------------------------

默认 welcome.blade.php 也会将登录用户重定向到教师主页:

@if (Route::has('login'))
        <div class="top-right links">
            @auth
                <a href="{{ url('/home') }}">Home</a>
            @else
                <a href="{{ route('login') }}">Login</a>

                @if (Route::has('register'))
                    <a href="{{ route('register') }}">Register</a>
                @endif
            @endauth
        </div>
    @endif

  1. 如何确保用户在新注册和登录时转到正确的主页?
  2. 如何确保其他角色无法访问一个角色的主页?

编辑 1:

合并@N69S 建议的更改后,情况如下:

该解决方案有效。但是当试图非法访问 URL 时,就会产生一个错误。 但是相反,用户被发送到欢迎页面。这个页面只有一个右上角的link,叫做"HOME"。当管理员或 HOD 用户登录时,此 link 不起作用,因为它路由到 '/home',如图所示:

//welcome.blade.php
<body>
    <div class="flex-center position-ref full-height">
        @if (Route::has('login'))
            <div class="top-right links">
                @auth
                    <a href="{{ url('/home') }}">Home</a>
                @else
                    <a href="{{ route('login') }}">Login</a>

                    @if (Route::has('register'))
                        <a href="{{ route('register') }}">Register</a>
                    @endif
                @endauth
            </div>
        @endif

        <div class="content">
            <div class="title m-b-md text-center">
                COLLEGE ADMINISTRATION <br> SYSTEM
            </div>
        </div>
    </div>
</body>

如何更改此视图文件以将登录用户发送到其角色的主页?

一个完整且正确的解决方案是分离实体(HOD、管理员、用户)并为每个实体配置身份验证(auth:hod、auth:admin、auth)

另一种解决方案是制作 3 个单独的中间件,并相应地将您的路由与其中一个分组。

顺便说一句,redirectTo() 方法不带参数。来自 Illuminate\Foundation\Auth\RedirectsUsers 特征

/**
 * Get the post register / login redirect path.
 *
 * @return string
 */
public function redirectPath()
{
    if (method_exists($this, 'redirectTo')) {
        return $this->redirectTo();
    }

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

----------------编辑

对于 3 中间件解决方案,您可以使用命令 php artisan make:middleware Admin,这将创建 Class 文件 app/Http/ Middleware/Admin.php.

handle方法中,你可以做类似

的事情
public function handle($request, Closure $next)
{
    if(auth()->user() && auth()->user()->role == 2){
        return $next($request);
    }
    return redirect(‘login’)->with(‘error’,’You have not admin access’);
}

并对 HOD 做同样的事情

要在路由中像auth一样使用,在$routeMiddleware

中添加内核中的中间件App\Http\Kernel.php
protected $routeMiddleware = [
    //....,
    //....,
    'admin' => \App\Http\Middleware\Admin::class,
];