自己修改自己的帖子始终 403

所有其他网友碰到问题的地方都检查N遍了,路由 bindings 没问题,路由 api.auth 没问题,policy 绑定没问题,控制器没问题,policy 文件没问题。

感觉根本没进到 policy 文件里面去:

    public function update(User $user, Coordinate $coordinate)
    {
        dd($user);
        return $user->id === $coordinate->user_id;
    }

dd 都出不来,说明没进到 update 里面来,但是登录是正常没问题的,控制器里 $this->user() 是有的,用的 passport 验证。

再三确定是自己的帖子!

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 8

@liyu001989 求大神帮忙!

5年前 评论
liyu001989

完整的报错呢,controller 的 update 方法进去了吗,你首先得确定问题是不是出在 policy

5年前 评论

我这个业务逻辑是一切正常的,app 已经上线。之前赶进度,policy 的控制在 app 里做了,所以服务端没做 policy。现在加了一个 policy 就出问题了。不加 $this->authorize('update', $coordinate); 这句的话 update 一切正常,$this->user(); 也能获取

file

5年前 评论

@liyu001989 帮忙看看吧

5年前 评论
liyu001989

分析问题,一定是 $this->authorize('update', $coordinate); 的问题,但是又没有尽到方法中。

那么沿着 authorize 一点一点调试呗,可以进去一点一点加日志。是policy before 的问题,还是什么问题,我看不太出来

5年前 评论

谢谢了!我现在在 CoordinateController 里用:

    $this->authorizeForUser($this->user(), 'update', $coordinate);

可以正常授权了。

在 CoordinatePolicy 里面能够获得 $user

    public function update(User $user, Item $item)
    {
        dd($user);
        return $user->id === $item->user_id;
    }

我是沿着 authorize 一点一点 dd 下去,最终发现在 Gate.php 里:

    /**
     * Get the raw result from the authorization callback.
     *
     * @param  string  $ability
     * @param  array|mixed  $arguments
     * @return mixed
     */
    public function raw($ability, $arguments = [])
    {
        $arguments = Arr::wrap($arguments);

        $user = $this->resolveUser();

        // First we will call the "before" callbacks for the Gate. If any of these give
        // back a non-null response, we will immediately return that result in order
        // to let the developers override all checks for some authorization cases.
        $result = $this->callBeforeCallbacks(
            $user, $ability, $arguments
        );

        if (is_null($result)) {
            $result = $this->callAuthCallback($user, $ability, $arguments);
        }

        // After calling the authorization callback, we will call the "after" callbacks
        // that are registered with the Gate, which allows a developer to do logging
        // if that is required for this application. Then we'll return the result.
        return $this->callAfterCallbacks(
            $user, $ability, $arguments, $result
        );
    }

$user = $this->resolveUser(); 没有返回 $user。

我是 Dingo + Passport,可能 PassportDingoProvider 在初始化的时候没有实例化 Gate,导致登录用户信息没有放进 Gate 中。我不知道 JWT 是怎么解决这个问题的。

5年前 评论

现在用了另外的授权方法也算是达到效果了

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!