在我的Laravel申请中
User
可以有一个
Profile
他们或有权限的用户可以更新。
这两个模型的关系用这种方法定义:
public function profile()
{
return $this->hasOne(Profile::class, 'user_username', 'username');
}
这是更新用户配置文件的方法:
public function update(UpdateProfile $request, User $user)
{
if ($user) {
$profile = $user->profile;
if (!empty(request()->get('background'))) {
$profile->background = clean($request->get('background'));
}
if (!empty(request()->get('skills'))) {
$profile->skills = clean($request->get('skills'));
}
if (!empty(request()->get('filepath'))) {
$profile->displayPicture = $request->get('filepath');
}
if (!empty(request()->get('linkedInUrl'))) {
$socialProfilesDecoded = json_decode($user->profile->socialProfiles, true);
$socialProfilesDecoded["LinkedIn"] = $request->get('linkedInUrl');
$profile->socialProfiles = json_encode($socialProfilesDecoded);
}
if (!empty(request()->get('twitterUrl'))) {
$socialProfilesDecoded = json_decode($user->profile->socialProfiles, true);
$socialProfilesDecoded["Twitter"] = $request->get('twitterUrl');
$profile->socialProfiles = json_encode($socialProfilesDecoded);
}
$user->profile()->save($profile);
return redirect()->back()->withSuccess('Your profile has been successfully updated');
}
}
更新配置文件的路径是:
Route::post('profile/{user}', 'ProfileController@update');
我注意到,暴露用户名会带来一个漏洞,就好像您可以使用web代理获取请求一样,您只需更改用户名并更新另一个用户的配置文件。
在不更改URL的情况下,我可以制定一个策略来检查:
-
用户有权更新所述配置文件
-
正在更新的配置文件是正确的配置文件(请求未被篡改)。
或者,我是否应该更改URL并有一种方法来编辑管理区域中的配置文件?
另外,由于配置文件与用户关联,特权用户如何访问其他用户的配置文件?
也许是一个隐藏的输入?
更新:
if ($request->is('admin/*')) {
}
我能检查一下这是否符合邮寄要求吗?
更新2
添加了一个简单的检查,以确保登录用户具有更新配置文件的权限。
public function update(UpdateProfile $request, User $user)
{
if(auth()->user() == $user || auth()->user()->can('Approve user profile')){
if ($user) {
$profile = $user->profile;
if (!empty(request()->get('background'))) {
$profile->background = clean($request->get('background'));
}
if (!empty(request()->get('skills'))) {
$profile->skills = clean($request->get('skills'));
}
if (!empty(request()->get('filepath'))) {
$profile->displayPicture = $request->get('filepath');
}
if (!empty(request()->get('linkedInUrl'))) {
$socialProfilesDecoded = json_decode($user->profile->socialProfiles, true);
$socialProfilesDecoded["LinkedIn"] = $request->get('linkedInUrl');
$profile->socialProfiles = json_encode($socialProfilesDecoded);
}
if (!empty(request()->get('twitterUrl'))) {
$socialProfilesDecoded = json_decode($user->profile->socialProfiles, true);
$socialProfilesDecoded["Twitter"] = $request->get('twitterUrl');
$profile->socialProfiles = json_encode($socialProfilesDecoded);
}
$user->profile()->save($profile);
return redirect()->back()->withSuccess('Your profile has been successfully updated');
}
}
}