class ModelVisibility implements ExtenderInterface

Model visibility scoping allows us to scope queries based on the current user.

The main usage of this is only showing model instances that a user is allowed to see.

This is done by running a query through a series of "scoper" callbacks, which apply additional wheres to the query based on the user.

Scopers are classified under an ability. Calling whereVisibleTo on a query will apply scopers under the view ability. Generally, the main view scopers can request scoping with other abilities, which provides an entrypoint for extensions to modify some restriction to a query.

Scopers registered via scopeAll will apply to all queries under a model, regardless of the ability, and will accept the ability name as an additional argument.

Methods

__construct(string $modelClass)

No description

scope($callback, $ability = 'view')

Add a scoper for a given ability.

scopeAll(callable|string $callback)

Add a scoper scoper that will always run for this model, regardless of requested ability.

extend(Container $container, Extension $extension = null)

No description

Details

__construct(string $modelClass)

Parameters

string $modelClass The ::class attribute of the model you are applying scopers to. This model must extend from \Flarum\Database\AbstractModel, and use \Flarum\Database\ScopeVisibilityTrait.

ModelVisibility scope($callback, $ability = 'view')

Add a scoper for a given ability.

Parameters

$callback
$ability

Return Value

ModelVisibility

ModelVisibility scopeAll(callable|string $callback)

Add a scoper scoper that will always run for this model, regardless of requested ability.

Parameters

callable|string $callback The callback can be a closure or invokable class, and should accept: - \Flarum\User\User $actor - \Illuminate\Database\Eloquent\Builder $query - string $ability

Return Value

ModelVisibility

extend(Container $container, Extension $extension = null)

Parameters

Container $container
Extension $extension