我有一个Board.php
模型,其方法为checkViewAccess()
即
@if($board->checkViewAccess())
render board
@endif
它进行了一些检查,在最后一次检查时,它需要调用策略方法来确定用户的授权。
@can('view', $board)
正常工作$this->authorize('view', $board)
正常工作但我不能为我的生活找到任何方式从Board
模型中调用政策授权
我尝试过的事情:
Gate::check('view', $this)
//非静态方法,很好,不希望它起作用auth()->user()->can('view', $this)
//无论我从政策中返回什么,这都不会触及政策并返回false。即使我只是在before()
政策方法中返回true,它也是假的。 Laravel文档声称can()
方法可用于用户对象$user->can('view', $this);
//认为这是auth facade的一个问题,我直接从数据库中获取用户,但它仍然没有达到策略那我怎么能做到这一点?如何从模型中访问laravels授权?
答案 0 :(得分:0)
原来是Laratrust
包在特征中使用can()
方法,因此我无法将自己的可授权特征添加到我的用户模型中
修正:
use LaratrustUserTrait;
use Authorizable {
LaratrustUserTrait::can insteadof Authorizable;
Authorizable::can as authorize;
}