3
我想根据用户是否有权访问模型来筛选我的雄辩模型集合。过滤雄辩
我目前的方法工作,但它实在是太慢了,所以我想知道如果有一个更高性能的方式做到这一点?
我对集合中的每个模型都有一个userHasAccess()
方法。 它使用Laravel的ACL功能,以确定用户是否有权访问模式:
public function userHasAccess()
{
if (Auth::user()->can('show', $this)) {
return true;
}
return false;
}
我再重写newCollection()
方法型号:
public function newCollection(array $models = Array())
{
$collection = new Collection($models);
$collection = $collection->filter(function($model)
{
if($model->userHasAccess())
return true;
});
return $collection;
}
的策略方法是这样的:
public function show(User $user, Quote $quote)
{
if(!$quote->customer)
return false;
if(($user->id === $quote->user_id))
return true;
if($user->hasRole(['super-admin','admin']))
return true;
return false;
}
有没有更好的方式来做到这一点?特别是在性能方面?
谢谢 - 我最终做了类似的事情,所以谢谢你确认我的方向。我也喜欢使用查询范围的想法。 – HPage