Php 如何保护其他用户的配置文件表单更新laravel 5.3
这次我有这样的Url 我与一个id为51的用户一起登录,但是如果我将此id更改为50,它将显示带有id为50的用户数据的表单,我想停止此操作,我如何保护帮助,请。 在执行其他命令之前,如何检查此条件Php 如何保护其他用户的配置文件表单更新laravel 5.3,php,laravel,Php,Laravel,这次我有这样的Url 我与一个id为51的用户一起登录,但是如果我将此id更改为50,它将显示带有id为50的用户数据的表单,我想停止此操作,我如何保护帮助,请。 在执行其他命令之前,如何检查此条件 public function profile($id, Request $request) { ***if($id == Auth::user()->id){ Now Check get or post method }else{
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.'));
}
我有一个想法,我可以检查身份验证用户id等于这个id,但不知道如何实现我沙丁网址这里作为
<form action="/user/profile/{{$user->id}}" method="POST">
现在,在向用户显示表单之前,如何检查该表单是否是其相关表单
谢谢如果您想将用户配置文件设置为私有,则不应将其绑定到
GET
参数(仅对公共配置文件页面执行此操作)。您应该做什么将配置文件路由放入auth
中间件组:
Route::group(['middleware' => 'auth'], function () {
Route::get('profile', 'UserController@showProfile');
}
并使用auth()->user()
对象获取要显示的数据:
{{ auth()->user()->name }}
{{ auth()->user()->email }}
Laravel中间件() 中间件将帮助您,它将允许您检查请求url的用户是否具有与其会话中存储的相同id。如果他们匹配,他是配置文件的“所有者”,如果不匹配,你可以重定向他,并提供错误消息 创建中间件(App\Http\Middleware\IsOwnerOfProfile.php) 更新您的路线 注意和更新:
正如@Alexey Mezenin所提到的,
auth
middelware需要在这之前执行。否则您将无法访问auth()->user()
,中间件将出现异常。请您帮助我设置如果($id==auth::user()->id){/////code>如果它是确定的,则检查方法get或post@Alexey的条件Mezenin@recoverymen,我不明白这个问题。你能描述一下你到底需要什么吗?我已经更新了我的问题,请你帮我在check get或post mean if else之前用正确的方式检查一下吗if($id==Auth::user()->id){Now check get或post method}else{return('Out');}
@Alexey MezeninI不会建议你这么做,这是一个坏习惯。您应该为公共配置文件和私有配置文件使用单独的操作(控制器方法)和路由。为什么要向用户51显示用户50的形式。实际上,当用户51试图查看用户50的配置文件时,显示404。
Route::group(['middleware' => 'auth'], function () {
Route::get('profile', 'UserController@showProfile');
}
{{ auth()->user()->name }}
{{ auth()->user()->email }}
<?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);
}
}
protected $routeMiddleware = [
// other defined middlewares
'profileOwner' => \App\Http\Middleware\IsOwnerOfProfile::class,
]
Route::any('/user/profile/{id}', [
'middleware' => 'profileOwner'
'as' => 'user.profile',
'uses' => 'UserController@profile'
]);