Php 用政策限制行动

Php 用政策限制行动,php,laravel,Php,Laravel,在my Laravel应用程序中,用户可以拥有一个配置文件,他们或具有权限的用户可以更新该配置文件 这两个模型的关系在此方法中定义: /** * Get the profile associated with this user */ public function profile() { return $this->hasOne(Profile::class, 'user_username', 'username'); } 这是更新用户配置文件的方法: /** * Upd

在my Laravel应用程序中,
用户
可以拥有一个
配置文件
,他们或具有权限的用户可以更新该配置文件

这两个模型的关系在此方法中定义:

/**
 * Get the profile associated with this user
 */
public function profile()
{
    return $this->hasOne(Profile::class, 'user_username', 'username');
}
这是更新用户配置文件的方法:

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Profile  $profile
 * @return \Illuminate\Http\Response
 */
public function update(UpdateProfile $request, User $user)
{
    if ($user) {
        // Only proceed if there is a logged in user
        $profile = $user->profile;

        // If there is no profile, create one for this user as they'll need one.
        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/*')) {
        //
    }
    
    我可以检查一下这是否符合POST请求吗

    更新2

    添加了一个简单的检查,以确保登录用户具有更新配置文件的权限

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Profile  $profile
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateProfile $request, User $user)
    {
        // Check this user
        if(auth()->user() == $user || auth()->user()->can('Approve user profile')){
            if ($user) {
                // Only proceed if there is a logged in user
                $profile = $user->profile;
    
                // If there is no profile, create one for this user as they'll need one.
                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');
            }
        }
    }
    

    依我看,在服务器端检查。HTML DOM将被攻击者篡改。因此,您将拥有
    权限
    用户
    配置文件
    表,以及一个映射表,该映射表的列为
    权限id、配置文件id、用户id
    。然后,在更新之前,您可以使用类似
    Auth::user()->hasPermissions($profile->id)
    使用模型中定义的这些方法。我还使用包Spatie Laravel权限分配用户角色