]> BookStack Code Mirror - bookstack/blob - app/References/ReferenceFetcher.php
Fixed local_secure_restricted preventing attachment uploads
[bookstack] / app / References / ReferenceFetcher.php
1 <?php
2
3 namespace BookStack\References;
4
5 use BookStack\Auth\Permissions\PermissionApplicator;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
8 use Illuminate\Database\Eloquent\Collection;
9 use Illuminate\Database\Eloquent\Relations\Relation;
10
11 class ReferenceFetcher
12 {
13     protected PermissionApplicator $permissions;
14
15     public function __construct(PermissionApplicator $permissions)
16     {
17         $this->permissions = $permissions;
18     }
19
20     /**
21      * Query and return the page references pointing to the given entity.
22      * Loads the commonly required relations while taking permissions into account.
23      */
24     public function getPageReferencesToEntity(Entity $entity): Collection
25     {
26         $baseQuery = $entity->referencesTo()
27             ->where('from_type', '=', (new Page())->getMorphClass())
28             ->with([
29                 'from'         => fn (Relation $query) => $query->select(Page::$listAttributes),
30                 'from.book'    => fn (Relation $query) => $query->scopes('visible'),
31                 'from.chapter' => fn (Relation $query) => $query->scopes('visible'),
32             ]);
33
34         $references = $this->permissions->restrictEntityRelationQuery(
35             $baseQuery,
36             'references',
37             'from_id',
38             'from_type'
39         )->get();
40
41         return $references;
42     }
43
44     /**
45      * Returns the count of page references pointing to the given entity.
46      * Takes permissions into account.
47      */
48     public function getPageReferenceCountToEntity(Entity $entity): int
49     {
50         $baseQuery = $entity->referencesTo()
51             ->where('from_type', '=', (new Page())->getMorphClass());
52
53         $count = $this->permissions->restrictEntityRelationQuery(
54             $baseQuery,
55             'references',
56             'from_id',
57             'from_type'
58         )->count();
59
60         return $count;
61     }
62 }