【问题标题】:How to restrict a user to only see their own profile如何限制用户只能查看自己的个人资料
【发布时间】:2023-04-05 04:29:02
【问题描述】:

我有一个视图 (resources/view/front/auth/profile.blade.php),我在文件 web.php 中的路线是:

Route::get('/profile/{user}','UserController@edit')
    ->name('profile')
    ->middleware('profilecheck');

我的问题是当用户登录并被重定向到他们自己的个人资料页面 (http://exmaple.com/profile/2) 时,他/她可以将 URL 更改为 http://exmaple.com/profile/3 并查看其他用户的个人资料。

我想使用中间件来检查带有 URL 参数 {user} 的经过身份验证的用户 ID。 $user->id 将传递给 {user},但我不知道如何。

中间件UserProfile.php:

<?php

namespace App\Http\Middleware;

use App\User;
use Closure;

class UserProfile
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // $request->user()->id
        // Auth::user()->id

        return $next($request);

    }
}

【问题讨论】:

  • fi 已记录,只需使用“/profile”作为 url,在控制器中使用return view(...)-&gt;with('user', auth()-&gt;user()),或定义一些策略laravel.com/docs/5.8/authorization
  • 去掉在url中传递id的选项,直接通过Auth::user()访问用户,使用控制器中的auth门面返回当前用户
  • @Berto99,谢谢你,我只是按照你说的做了,而且成功了。

标签: php laravel laravel-6


【解决方案1】:

您可以简单地通过从 URL 中删除用户 ID 来保护路由,而是通过身份验证会话获取它。

所以,你的路由签名应该来自:

Route::get('/profile/{user}', 'UserController@edit')->name('profile');

到这里:

Route::get('/profile', 'UserController@edit')->name('profile');

因此,在您的控制器中,而不是从请求中获取用户 ID:

public function edit(Request $request)
{
     $user = User::findOrFail($request->id);
     // ...
}

你可以通过Auth门面获得登录的User

use Illuminate\Support\Facades\Auth;

public function edit(Request $request)
{
     $user = Auth::user();
     // ...
}

或者只是 auth() 助手:

public function edit(Request $request)
{
     $user = auth()->user();
     // ...
}

这样,您可以屏蔽 URL,以避免恶意用户做他/她不应该做的事情。

【讨论】:

  • 这是一种方法,但是您会错过诸如路由模型绑定之类的东西。从长远来看,使用 auth 中间件和策略可能会更好
  • @Spholt 为什么会错过模型绑定?如果您需要绑定其他内容,只需在您的路线中定义它并在函数中接收它。我并不是说应该在每个请求中都这样做,只是在阻止用户修改他的个人资料的这种特殊情况下。这种方法也适用于编辑与用户相关的内容。
【解决方案2】:

你需要做这样的事情。

你的路线

Route::get('/profile', [
    'uses' => 'UserController@profile',
    'middleware' => 'profilecheck'
]);

你的中间件

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

    if(!auth()->user()) {
        return redirect()->route('login');
    }

    return $next($request);
  }
}

【讨论】:

    【解决方案3】:
    // Controller 
     public function index()
        {
            if (Auth::check() && Auth::user()->role->id == 2) {
                return view('author.setting.settings');
            } else {
                Toastr::info('you are not authorized to access', 'Info');
                return redirect()->route('login');
            }
        }
    
    // Route 
    Route::group(['as'=>'user.','prefix'=>'user','namespace'=>'Author','middleware'=>['auth','user']], function (){
    
        Route::get('/setting','SettingsController@index')->name('settings.settings');
    
    });
    

    【讨论】:

    • 解释一下。请通过editing (changing) your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
    猜你喜欢
    • 1970-01-01
    • 2022-07-29
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    相关资源
    最近更新 更多