Fixes model visibility (#2580)

Model Visibility extender does not take into consideration missing
dependencies. For instance flarum/tags adds a policy on the Flag model
from flarum/flags. But because flarum/flags might as well not be
installed we need to check for the existence of that model. Otherwise
the exception is thrown or flarum fails to boot.
This commit is contained in:
Daniël Klabbers 2021-01-29 14:13:16 +01:00 committed by GitHub
parent 843daf633d
commit 964f827ee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,7 +44,7 @@ class ModelVisibility implements ExtenderInterface
{
$this->modelClass = $modelClass;
if (! method_exists($modelClass, 'registerVisibilityScoper')) {
if (class_exists($this->modelClass) && ! is_callable([$modelClass, 'registerVisibilityScoper'])) {
throw new Exception("Model $modelClass cannot be visibility scoped as it does not use Flarum\Database\ScopeVisibilityTrait.");
}
}
@ -89,6 +89,10 @@ class ModelVisibility implements ExtenderInterface
public function extend(Container $container, Extension $extension = null)
{
if (! class_exists($this->modelClass)) {
return;
}
foreach ($this->scopers as $ability => $scopers) {
foreach ($scopers as $scoper) {
$this->modelClass::registerVisibilityScoper(ContainerUtil::wrapCallback($scoper, $container), $ability);