drny60365 2016-08-01 15:46
浏览 85

试图编写政策以启用Laravel中的帖子评论

In laravel I have a Follower table that I use to check if a User is folowing another User and also if he can comment on Posts.

The table is like this:

Schema::create('followers', function (Blueprint $table) {

            $table->unsignedInteger('publisher_id')->unsigned();
            $table->unsignedInteger('follower_id')->unsigned();
            $table->boolean('enable_follow')->default('1');
            $table->unique(['publisher_id', 'follower_id']);
            $table->timestamps();


            $table->foreign('publisher_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->foreign('follower_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');


        });

and these are the checks that I make to decide if a User can comment a Post:

public function canComment(User $user, Post $post)
{

    $following = Follower::where('follower_id', $user->id)->where('publisher_id', $post->user_id)->select('enable_follow')->get();

    if (!$following->isEmpty()) {

        $enabled = $following[0]['enable_follow'];

        if ($enabled != '0') {

            return true;

        } else {

            return false;

        }
    } else if ($following->isEmpty()) {

        return true;

    }

}

And this is the controller part for storing, as You can see I'm trying to authorize like this: $this->authorize('canComment', $post[0]);

public function store(Request $request)
    {


        //on_post, from_user, body
        // define rules
        $rules = array(

            'post_id' => 'required',
            'body' => 'required'
        );

        $validator = Validator::make(Input::all(), $rules);

        $post_id = $request->input('post_id');

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

        if ($validator->fails()) {
            return Response()->json($validator);
        } else {

            $this->authorize('canComment', $post);

            //prepares object to be stored in DB
            $comment = new Comment();

            $comment['user_id'] = $request->user()->id;
            $comment['post_id'] = $post_id;
            $comment['body'] = $request->input('body');
            $comment->save();
            if ($comment) {

                $comment['user_name'] = $request->user()->username;
                $comment['comment_id'] = $comment->id;
                $comment['token'] = $request->input('_token');
            }

            return Response()->json($comment);


        }
    }

The problem here is I get a 403 (Forbidden) error in a situation where I have $following empty and where following is enabled. The Policy is not working as expected.

Source code for authorize method in Gate facade:

public function authorize($ability, $arguments = [])
    {
        $result = $this->raw($ability, $arguments);

        if ($result instanceof Response) {
            return $result;
        }

        return $result ? $this->allow() : $this->deny();
    }

Maybe I am not correct returing true or false in the policy as this code expect the result to be an instance of Response but so what do you return to grant or deny access??

  • 写回答

1条回答 默认 最新

  • dsm17496 2016-08-03 15:17
    关注

    The problem was putting the policy inside commentPolicy and so it expected to receive a Comment not a Post, moving it to postPolicy solved it.

    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题