2016-11-04 18 views
1

這時候我已經網址這樣 http://Blog/user/profile/51 和我與他們ID的用戶登錄= 51,但如果我改變這個ID來50它顯示用戶的ID爲50的數據的形式我想停止這個,我可以如何保護幫助。 哪能的othere我如何可以被其他用戶保護個人資料的形式更新laravel 5.3

public function profile($id, Request $request) 
    {  

    ***if($id == Auth::user()->id){ 
     Now Check get or post method 
    }else{ 
     return ('Out'); 
    }*** 

$method = $request->method(); 
    if ($request->isMethod('get')) { 
      $user = User::findOrFail($id); 
      $users = DB::table('users')->select('id', 'name')->get(); 
      $user_fields = DB::table('users')->where('id', $user->id)->get(); 
    $page_data = [ 
          'title' => 'Edit User Account', 
          'action' => 'edit' 
         ]; 

      return view('user.profile')->with(compact('user', 'page_data', 'users')); 

    }else{ 
    $this->validate($request, [ 
      'name' => 'required|max:255', 

     ]); 

     $user = User::findOrFail($id); 
     $input = $request->all(); 
     $user->fill([ 
     'name'   => $request->input('name'), 
     'password'  => $request->password ? bcrypt($request->input('password')) : $user->password, 
     'time_zone' => $request->input('time_zone'), 
     'address_line_1' => $request->input('address_line_1'), 
     'address_line_2' => $request->input('address_line_2'), 
     ])->save(); 
     session()->flash('msg',trans('successfully Updated.')); 
} 

執行前檢查這個條件我有想法,我可以檢查auth用戶ID等於這個ID,但不知道如何實現我在這裏打磨網址爲

<form action="/user/profile/{{$user->id}}" method="POST"> 

我在這裏取auth用戶id作爲

<a href="{{ route('user.profile', ['id' => Auth::user()->id ]) }}"> 

路線如下

Route::any('/user/profile/{id}', ['as' => 'user.profile', 'uses' => '[email protected]']); 

現在,我怎麼表演形式,以用戶是其相關的形式或不 感謝

+0

您爲什麼要向用戶51顯示用戶50的表單。當用戶51嘗試查看用戶50的用戶時,顯示404。 –

回答

1

如果你想使用戶的個人資料私有的,你不應該把它綁GET參數之前檢查(這樣做只爲公共個人資料頁面)。你應該做的是把配置文件路徑裏面auth中間件組:

Route::group(['middleware' => 'auth'], function() { 
    Route::get('profile', '[email protected]'); 
} 

並使用auth()->user()對象的數據得到顯示:

{{ auth()->user()->name }} 
{{ auth()->user()->email }} 
+0

請問你可以幫我設置 '如果($ id == Auth :: user() - > id){/////}' 如果沒問題,那麼檢查方法獲取或發佈的條件@Alexey Mezenin –

+0

@recoverymen,我不明白這個問題。你能描述一下你需要什麼嗎? –

+0

我有更新我的問題,請你可以幫我檢查之前檢查得到或後意思如果其他以正確的方式 'if($ id == Auth :: user() - > id){ Now Check get or post方法 } else { return('Out'); }' @Alexey Mezenin –

0

Laravel中間件docs

中間件會幫助你,它會允許你檢查請求url的用戶是否與他的會話中存儲的id相同。如果他們匹配,他就是配置文件的「所有者」,否則可以重定向他提供錯誤消息。

創建middelware(應用程序\ HTTP \中間件\ IsOwnerOfProfile.php)

<?php 

namespace App\Http\Middleware; 

use Closure; 

class IsOwnerOfProfile { 
    /** 
    * Check if the user accessing the profile is the owner of the profile 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next){ 
     if ($request->id != auth()->user()->id) { 
      return redirect('home')->with('error','You are not the owner of this profile'); 
     } 

     return $next($request); 
    } 
} 

註冊middelware(應用程序\ HTTP \ Kernel.php)

protected $routeMiddleware = [ 
    // other defined middlewares 
    'profileOwner' => \App\Http\Middleware\IsOwnerOfProfile::class, 
] 

更新您的路線

Route::any('/user/profile/{id}', [ 
    'middleware' => 'profileOwner' 
    'as' => 'user.profile', 
    'uses' => '[email protected]' 
]); 

備註&更新: 正如@Alexey Mezenin所述,auth middelware需要在此之前執行。否則,您將無法訪問auth()->user(),中間件將拋出異常。

相關問題