jeudi 4 août 2022

Apply model policy in a global scope query

I have a complexe user policy (extending the PolicyAbstract class) granting reading/editing access for a given user to other user based on several rules and other parameters of the current user.

I am currently applying the policy on each records and filtering out the user the given does not have access after the query has been performed by walking the collection. This has the adverse effect of messing up the pagination collection results as I remove records after the pagination was set.

I would like to apply the policy in a global scope so the forbiden user are already filtered from the paginated collection. Something like this:

public static function boot() {

    parent::boot();
            
    static::addGlobalScope('user_selection', function(Builder $builder) {
        $builder->[involve the user policy here]
    }

}

Is this possible in any way? Any help or pointer to achieve it would be greatly appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire