X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/1cc7c649dc3a5e1adc0c1bbcee1e7adb61bbd968..refs/pull/4467/head:/app/References/ReferenceFetcher.php diff --git a/app/References/ReferenceFetcher.php b/app/References/ReferenceFetcher.php index a73463a95..c4a7d31b6 100644 --- a/app/References/ReferenceFetcher.php +++ b/app/References/ReferenceFetcher.php @@ -2,9 +2,10 @@ namespace BookStack\References; -use BookStack\Auth\Permissions\PermissionApplicator; use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Page; +use BookStack\Permissions\PermissionApplicator; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\Relation; @@ -23,8 +24,7 @@ class ReferenceFetcher */ public function getPageReferencesToEntity(Entity $entity): Collection { - $baseQuery = $entity->referencesTo() - ->where('from_type', '=', (new Page())->getMorphClass()) + $baseQuery = $this->queryPageReferencesToEntity($entity) ->with([ 'from' => fn (Relation $query) => $query->select(Page::$listAttributes), 'from.book' => fn (Relation $query) => $query->scopes('visible'), @@ -47,11 +47,8 @@ class ReferenceFetcher */ public function getPageReferenceCountToEntity(Entity $entity): int { - $baseQuery = $entity->referencesTo() - ->where('from_type', '=', (new Page())->getMorphClass()); - $count = $this->permissions->restrictEntityRelationQuery( - $baseQuery, + $this->queryPageReferencesToEntity($entity), 'references', 'from_id', 'from_type' @@ -59,4 +56,12 @@ class ReferenceFetcher return $count; } + + protected function queryPageReferencesToEntity(Entity $entity): Builder + { + return Reference::query() + ->where('to_type', '=', $entity->getMorphClass()) + ->where('to_id', '=', $entity->id) + ->where('from_type', '=', (new Page())->getMorphClass()); + } }