优雅地退出 Laravel 作用域

我有一个范围,它根据用户角色以限制方式起作用,您可以将一组规则转发到限制数据库最终输出的范围。


一个非常简化的角色限制示例:


{

    "first_name": "=foo%"

}

只会返回first_name以 开头的记录foo%,这实际上意味着我已禁止具有该角色的用户查看任何不以 开头的记录foo%。


这种方法的问题在于,如果某人没有施加任何限制,他就会看到一切。我知道这是一个有效的假设,但如果没有施加限制,我想禁止一切,这样如果创建了新角色,它就没有任何权利。


目前我正在针对这种情况抛出异常:


public function apply(Builder $builder, Model $model)

{

    $input = $this->getAuthValues(get_class($model));


    if (count($input) < 1) {

        throw new \Exception('No rights for you');

    }


    $jsonQuery = new JsonQuery($builder, $input); <-- class responsible for assembling the queries based on the input

    $jsonQuery->search();

}

但这当然也不例外。在这种情况下我希望返回空数组。


我可以使用某种方法来优雅地退出范围并返回空结果,而无需在之后实际执行查询吗?


繁星点点滴滴
浏览 114回答 2
2回答

Cats萌萌

我找到了一个解决方案来进行不可能的查询,该查询将返回 0 结果。public function apply(Builder $builder, Model $model){&nbsp; &nbsp; $input = $this->getAuthValues(get_class($model));&nbsp; &nbsp; if (count($input) < 1) {&nbsp; &nbsp; &nbsp; &nbsp; $builder->whereRaw('1 = 0');&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $jsonQuery = new JsonQuery($builder, $input);&nbsp; &nbsp; $jsonQuery->search();}

明月笑刀无情

由于作用域通常是链接的(称为流畅),并且作用域不直接负责执行查询,因此我认为您不能“优雅地退出”,因为下一个调用仍然需要一个对象来处理Builder。一种可能的解决方案(并不简单)是创建一个扩展该类的类Builder,并重写负责实际从数据库获取结果的所有方法。我不确定您需要重写的所有方法才能在所有情况下都没有错误地工作。您可能还想处理其中的一些插入和更新情况AbortedBuilder。class AbortedBuilder extends Builder{&nbsp; &nbsp; ...&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Run the query as a "select" statement against the connection.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected function runSelect()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [];&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; ...&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Run a pagination count query.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; array&nbsp; $columns&nbsp; &nbsp; &nbsp;* @return array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected function runPaginationCountQuery($columns = ['*'])&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [];&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; ...&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Insert a new record and get the value of the primary key.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; array&nbsp; $values&nbsp; &nbsp; &nbsp;* @param&nbsp; string|null&nbsp; $sequence&nbsp; &nbsp; &nbsp;* @return int&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function insertGetId(array $values, $sequence = null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw new \Exception('You are trying to insert using an aborted query!');&nbsp; &nbsp; }&nbsp; &nbsp; ...}然后在你的范围内你可以这样做:public function apply(Builder $builder, Model $model){&nbsp; &nbsp; $input = $this->getAuthValues(get_class($model));&nbsp; &nbsp; if (count($input) < 1) {&nbsp; &nbsp; &nbsp; &nbsp; $builder = new AbortedBuilder();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $jsonQuery = new JsonQuery($builder, $input);&nbsp; &nbsp; &nbsp; &nbsp; $jsonQuery->search();}
打开App,查看更多内容
随时随地看视频慕课网APP