]> BookStack Code Mirror - bookstack/blobdiff - app/References/ReferenceFetcher.php
Implemented alternate approach to current joint_permissions
[bookstack] / app / References / ReferenceFetcher.php
index a73463a950a44d4edcd28c8e0b44e06858523326..41579185798ed541d7d8970c5f7cb2023c1fdcb7 100644 (file)
@@ -5,6 +5,7 @@ namespace BookStack\References;
 use BookStack\Auth\Permissions\PermissionApplicator;
 use BookStack\Entities\Models\Entity;
 use BookStack\Entities\Models\Page;
+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());
+    }
 }