代码之家  ›  专栏  ›  技术社区  ›  Jesse Orange

用策略限制操作

  •  0
  • Jesse Orange  · 技术社区  · 6 年前

    在我的Laravel申请中 User 可以有一个 Profile 他们或有权限的用户可以更新。

    这两个模型的关系用这种方法定义:

    /**
     * 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的情况下,我可以制定一个策略来检查:

    1. 用户有权更新所述配置文件
    2. 正在更新的配置文件是正确的配置文件(请求未被篡改)。

    或者,我是否应该更改URL并有一种方法来编辑管理区域中的配置文件?

    另外,由于配置文件与用户关联,特权用户如何访问其他用户的配置文件?

    也许是一个隐藏的输入?

    更新:

    if ($request->is('admin/*')) {
        //
    }
    

    我能检查一下这是否符合邮寄要求吗?

    更新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');
            }
        }
    }
    
    0 回复  |  直到 6 年前