【问题标题】:How to restrict authenticated users to only access their on profile如何限制经过身份验证的用户只能访问他们的个人资料
【发布时间】:2020-07-26 13:50:06
【问题描述】:

我被用户个人资料功能所困扰,我只希望经过身份验证的用户只能访问他们自己的个人资料。

id: 1的用户只能访问路由/applicants/profile/1,否则返回404 Not found

class ApplicantProfileController extends Controller
{
    public function show(Applicant $applicant)
    {
        return view('applicant.show', compact('applicant'));
    }
}
route::group(['prefix' => 'applicants', 'middleware' => 'auth:applicant'], function() {
    Route::get('/profile/{applicant}', 'Profiles\ApplicantProfileController@show');
});

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您可以使用Illuminate/Support/Facades/Auth 外观检查登录用户和参数用户是否相同,如下所示:

    public function show(Applicant $applicant)
    {
        if (Auth::id() == $applicant->id) {
            return view('applicant.show', compact('applicant'));
        }
    
        return abort(404);
    }
    

    【讨论】:

    • 我曾尝试使用 Auth 外观进行检查,但没有接近这一点。谢谢,它成功了:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    相关资源
    最近更新 更多