这是教程的第三遍,所以想弄的更加清楚。删除回复权限控制方法,源码疑问 ??
首先创建 ReplyPolicy 策略类,然后创建指定的方法
ReplyPolicy.php
public function destroy(User $user, Reply $reply)
{
// 在策略类中加入 destroy 方法
return $user->isAuthorOf($reply) || $user->isAuthorOf($reply->topic);
}
创建完成后, 在 RepliesController 类中使用策略类定义的方法
public function destroy(Reply $reply)
{
开始分析这行代码
$this->authorize('destroy', $reply);
$reply->delete();
return redirect()->to($reply->topic->link())->with('success', '删除成功');
}
$this->authorize('destroy',$reply)
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
是调用 AuthorizesRequests 这个类中的方法
}
AuthorizesRequests 类中 authorize() 方法
public function authorize($ability, $arguments = [])
{
// $this->authorize('destroy', $reply);
list($ability, $arguments) = $this->parseAbilityAndArguments($ability, $arguments);
dd($arguments);
我 dd 了 app(Gate::class) 打印出一大堆东西没有看明白
dd($ability) ==> 'destroy'
dd($arguments) ==> Reply 这个类
return app(Gate::class)->authorize($ability, $arguments);
}
现在我找到这个方法,但是我不知道这个方法怎么和 ReplyPolicy 中的 destroy() 方法做关联
希望 哪位好心人,给解答下嘛,谢谢谢谢啦。
关于 LearnKu
这方面的分析还没研究过,没法回答啊。反正我只是了解怎么用(1.生成策略;2.注册策略;3.使用策略判断是否授权)具体怎么实现的逻辑没关注。用框架除非你要对框架2次开发,否则,会用就好。
你dd(app('xxx'))出来应该可以看到是某个类吧,该类里面应该有authorize这个方法吧,进去看看?
这里调用了Gate了,看这个类的authorize这个方法
@shmily 但是怎么和 ReplyPolicy 联系起来呢?
@Gebriel
provider这里绑定了,应该是根据authorize传进去的$order识别到OrderPolicy
@shmily 对耶,我早上看到,我也没有想到 传进去的类,来绑定这个,权限模型。谢谢谢谢,
脑筋有时候就不会转,所以才会在阅读教程的时候多想想,然思维开阔起来。
你确定是这样的吗?
哈哈
@shmily 我刚刚又看了下。
实际上app函数解析得到
Illuminate\Auth\Access\Gate类实例,关键是raw方法的使用然后在该
authorize方法内,又调用了raw方法,主要做了以下三件事其中步骤2会通过参数推断(通过在auth服务提供者时注
$policies属性,模型类(参数类型)/策略类数组映射信息),分析得到策略类实例,然后调用对应的策略方法。