]> BookStack Code Mirror - bookstack/blob - app/Repos/EntityRepo.php
Updated ldap so extension not required in testing
[bookstack] / app / Repos / EntityRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Entity;
6 use BookStack\Page;
7 use BookStack\Services\PermissionService;
8 use BookStack\User;
9
10 class EntityRepo
11 {
12
13     /**
14      * @var Book $book
15      */
16     public $book;
17
18     /**
19      * @var Chapter
20      */
21     public $chapter;
22
23     /**
24      * @var Page
25      */
26     public $page;
27
28     /**
29      * @var PermissionService
30      */
31     protected $permissionService;
32
33     /**
34      * EntityService constructor.
35      */
36     public function __construct()
37     {
38         $this->book = app(Book::class);
39         $this->chapter = app(Chapter::class);
40         $this->page = app(Page::class);
41         $this->permissionService = app(PermissionService::class);
42     }
43
44     /**
45      * Get the latest books added to the system.
46      * @param int $count
47      * @param int $page
48      * @param bool $additionalQuery
49      * @return
50      */
51     public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false)
52     {
53         $query = $this->permissionService->enforceBookRestrictions($this->book)
54             ->orderBy('created_at', 'desc');
55         if ($additionalQuery !== false && is_callable($additionalQuery)) {
56             $additionalQuery($query);
57         }
58         return $query->skip($page * $count)->take($count)->get();
59     }
60
61     /**
62      * Get the most recently updated books.
63      * @param $count
64      * @param int $page
65      * @return mixed
66      */
67     public function getRecentlyUpdatedBooks($count = 20, $page = 0)
68     {
69         return $this->permissionService->enforceBookRestrictions($this->book)
70             ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
71     }
72
73     /**
74      * Get the latest pages added to the system.
75      * @param int $count
76      * @param int $page
77      * @param bool $additionalQuery
78      * @return
79      */
80     public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false)
81     {
82         $query = $this->permissionService->enforcePageRestrictions($this->page)
83             ->orderBy('created_at', 'desc')->where('draft', '=', false);
84         if ($additionalQuery !== false && is_callable($additionalQuery)) {
85             $additionalQuery($query);
86         }
87         return $query->with('book')->skip($page * $count)->take($count)->get();
88     }
89
90     /**
91      * Get the latest chapters added to the system.
92      * @param int $count
93      * @param int $page
94      * @param bool $additionalQuery
95      * @return
96      */
97     public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false)
98     {
99         $query = $this->permissionService->enforceChapterRestrictions($this->chapter)
100             ->orderBy('created_at', 'desc');
101         if ($additionalQuery !== false && is_callable($additionalQuery)) {
102             $additionalQuery($query);
103         }
104         return $query->skip($page * $count)->take($count)->get();
105     }
106
107     /**
108      * Get the most recently updated pages.
109      * @param $count
110      * @param int $page
111      * @return mixed
112      */
113     public function getRecentlyUpdatedPages($count = 20, $page = 0)
114     {
115         return $this->permissionService->enforcePageRestrictions($this->page)
116             ->where('draft', '=', false)
117             ->orderBy('updated_at', 'desc')->with('book')->skip($page * $count)->take($count)->get();
118     }
119
120     /**
121      * Get draft pages owned by the current user.
122      * @param int $count
123      * @param int $page
124      */
125     public function getUserDraftPages($count = 20, $page = 0)
126     {
127         $user = auth()->user();
128         return $this->page->where('draft', '=', true)
129             ->where('created_by', '=', $user->id)
130             ->orderBy('updated_at', 'desc')
131             ->skip($count * $page)->take($count)->get();
132     }
133
134     /**
135      * Updates entity restrictions from a request
136      * @param $request
137      * @param Entity $entity
138      */
139     public function updateEntityPermissionsFromRequest($request, Entity $entity)
140     {
141         $entity->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
142         $entity->permissions()->delete();
143         if ($request->has('restrictions')) {
144             foreach ($request->get('restrictions') as $roleId => $restrictions) {
145                 foreach ($restrictions as $action => $value) {
146                     $entity->permissions()->create([
147                         'role_id' => $roleId,
148                         'action'  => strtolower($action)
149                     ]);
150                 }
151             }
152         }
153         $entity->save();
154         $this->permissionService->buildJointPermissionsForEntity($entity);
155     }
156
157     /**
158      * Prepare a string of search terms by turning
159      * it into an array of terms.
160      * Keeps quoted terms together.
161      * @param $termString
162      * @return array
163      */
164     protected function prepareSearchTerms($termString)
165     {
166         preg_match_all('/"(.*?)"/', $termString, $matches);
167         if (count($matches[1]) > 0) {
168             $terms = $matches[1];
169             $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
170         } else {
171             $terms = [];
172         }
173         if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));
174         return $terms;
175     }
176
177
178 }