【问题标题】:How to limit delete acces from direct link?如何限制直接链接的删除访问?
【发布时间】:2019-07-15 17:26:39
【问题描述】:

我有两个问题需要你的帮助。首先,我已经为 cmets 做了一个删除路线,但是其他登录的用户也可以从直接链接中删除 cmets...link.com/deleteComment/id。如何使其仅对评论的所有者可用?所有者 ID 保存在数据库中,可以使用{{ $comment->user_id }} 访问。

第二个问题...在我看来,当我点击一张没有 cmets 的照片时,我收到了undefined variable comment,但我不知道为什么,因为在带有 cmets 的照片上,我没有问题。可以我做了类似if comments != empty, dont show it 之类的东西?

评论控制器:

 public function store(Request $request, $post_id)
    {
        $this->validate($request, array(

            'comment' => 'required|min:5|max:2000',
        ));


        $post = Post::find($post_id);

        $comment = new Comment();
        $comment->username = Auth::user()->username;
        $comment->email = Auth::user()->email;
        $comment->user_id = Auth::user()->id;
        $comment->comment = $request->comment;
        $comment->approved = true;
        $comment->post()->associate($post);

        $comment->save();
        Session::flash('message', "Message posted successfully!");
        return Redirect::back();
    }

PostsController:

    public function delete($id){

        DB::table('posts')->where('id',$id)->delete();

        return redirect('/profile/' . auth()->user()->id);
    }

我的看法

@foreach($post->comments as $comment)

          <div class="comment d-flex ">

            <p><strong><a class="text-dark" href="/profile/{{ $comment->user_id }}">{{ $comment->username}}</a>: </strong> {{ $comment->comment}}</p>
       @can('update', $post->user->profile)
    <div class="dropdown col-md-6">
    <button type="button" class="btn btn-primary dropdown-toggle btn-sm" style="background-color: #ffffff00;border: 1px solid #555;color: black;padding: 0 5px" data-toggle="dropdown">
      Select
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">Edit comment</a>
      <a class="dropdown-item" title="Options" style="text-decoration: none;" href="/deleteComment/{{$comment->id}}">Delete comment</a>
    </div>
  </div>

          </div>
@endcan
          @endforeach

我的路线

Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);

Route::get('/deleteComment/{id}', 'CommentsController@delete');

【问题讨论】:

  • 如何使此内容仅对评论的所有者可用?使用 laravel 策略,laravel.com/docs/5.8/authorization#creating-policies
  • 对于第二期,用@if($post-&gt;comments-&gt;count() &gt; 0) @endif 包装你的foreach
  • 谢谢,但是这个@if 不起作用,有什么解决办法吗?
  • 你有没有把@if放在@foreach之前如果是的话,我认为问题出在$comment-&gt;comment这一行,使用{{ isset($comment-&gt;comment) ? $comment-&gt;comment : "--" }}
  • 是的...也许我说错了?你能看看吗? codepen.io/anon/pen/BgeXPV

标签: php database laravel


【解决方案1】:

尝试此操作以仅对拥有评论的经过身份验证的用户限制删除:

/**
 *  Comments Controller Method Delete
 */
public function delete($id){

    if(!DB::table('comments')->where('id',$id)->where('user_id',auth()->user()->id)->delete()){
        Session::flash('remove', "You do not have permission to delete the comment!");
    }else{
        Session::flash('remove', "Message removed successfully!");
    }

    return Redirect::back();
}

对于您的第二个问题,我认为发生的情况是,没有 cmets 您正在使用没有结果的变量。

您可以尝试使用 this 将使用变量 $cmets 的语句括起来。

对于Controller或其他文件php

if (!$comment->isEmpty()) { 
//your code 
}
if ($comment->count()) { 
//your code 
}
if (count($comment)) { 
//your code 
}

对于刀片

@if(!$comment->isEmpty()) 
//your code 
@endif
@if($comment->count()) 
//your code 
@endif
@if(count($comment)) 
//your code
@endif

我希望我能帮助你,如果没有,请附上更多的代码,它们完全符合他所说的,因为 cmets 并删除图片,因为我没有在你附加的代码中看到。谢谢,祝你好运。

参考文献

$comment->isEmpty

$comment->count() and count($comment)

更新

<div class="row">
    <div class="col-md-12">
        @if(!$post->comments->isEmpty()) //****Added
            @if($post->comments->count() > 0)
                @foreach($post->comments as $comment)

                    <div class="comment d-flex ">

                        <p><strong><a class="text-dark"
                                      href="/profile/{{ $comment->user_id }}">{{ $comment->username}}</a>:
                            </strong> {{ isset($comment->comment) ? $comment->comment : "--" }}</p>
                        @can('update', $post->user->profile)

                            <div class="dropdown col-md-6">
                                <button type="button" class="btn btn-primary dropdown-toggle btn-sm"
                                        style="background-color: #ffffff00;border: 1px solid #555;color: black;padding: 0 5px"
                                        data-toggle="dropdown">
                                    Select
                                </button>
                                <div class="dropdown-menu">
                                    <a class="dropdown-item" href="#">Edit comment</a>
                                    <a class="dropdown-item" title="Options" style="text-decoration: none;"
                                       href="/deleteComment/{{$comment->id}}">Delete comment</a>
                                </div>
                            </div>

                    </div>
                    @endcan

                @endforeach
            @endif
        @endif //****Added
    </div>
</div>

更新为管理员或非管理员删除

/**
 *  Comments Controller Method Delete
 */
public function delete($id)
{
    $comment = DB::table('comments')->where('id', $id):

    if(!auth()->user()->admin){
        $comment->where('user_id', auth()->user()->id);
    }

    if (!$comment->delete()) {
        Session::flash('remove', "You do not have permission to delete the comment!");
    } else {
        Session::flash('remove', "Message removed successfully!");
    }

    return Redirect::back();
}

【讨论】:

  • @AndreiNagy,请在此处替换 (codepen.io/anon/pen/VJJZjQ) 我在答案中修改的删除功能。
  • 你是对的,现在删除功能在链接中不起作用:) 我要解决第二个问题...使用 $comment 未定义变量。我不明白.. .how to use that if's?我应该在哪里以及如何发布?
  • @AndreiNagy,请您返回发送图片出错的view的codepen吗?
  • 这是我的代码:codepen.io/anon/pen/MMMgdm,这是错误照片:imgur.com/a/KhgTITG。奇怪的是,在索引中,我有相同的照片,我点击没有 cmets 的照片没有任何错误,但是......从我的个人资料中我不能。我应该在我的个人资料索引中添加我的功能吗?因为,从个人资料中,我按下照片,即show.blade.php
  • @AndreiNagy,你使用角色系统吗?虽然我会想,我会给你一个示例代码并在我的答案中更新它。
【解决方案2】:

第一个问题可以很容易地完成。在您的 destroy() 函数中,只需检查评论所有者:

// Check comment owner    
if($comment->user_id != \Auth::id()){
   return abort(401);
}

// Do logic code to delete comment.

第二个问题,你可以像这样检查存在的评论:

if(! $comments->isEmpty()) {
  // Do logic code to show comment
}

【讨论】:

  • 第一次功能,我收到Undefined variable: comment
  • 你应该首先得到$comment变量,像这样:$comment = Comment::findOrFail($comment_id);
猜你喜欢
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 2023-04-07
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
相关资源
最近更新 更多