Dan Brown

Made more efficiency improvements to permission system

...@@ -49,6 +49,7 @@ class RegeneratePermissions extends Command ...@@ -49,6 +49,7 @@ class RegeneratePermissions extends Command
49 $connection = \DB::getDefaultConnection(); 49 $connection = \DB::getDefaultConnection();
50 if ($this->option('database') !== null) { 50 if ($this->option('database') !== null) {
51 \DB::setDefaultConnection($this->option('database')); 51 \DB::setDefaultConnection($this->option('database'));
52 + $this->permissionService->setConnection(\DB::connection($this->option('database')));
52 } 53 }
53 54
54 $this->permissionService->buildJointPermissions(); 55 $this->permissionService->buildJointPermissions();
......
...@@ -44,6 +44,7 @@ class RegenerateSearch extends Command ...@@ -44,6 +44,7 @@ class RegenerateSearch extends Command
44 $connection = \DB::getDefaultConnection(); 44 $connection = \DB::getDefaultConnection();
45 if ($this->option('database') !== null) { 45 if ($this->option('database') !== null) {
46 \DB::setDefaultConnection($this->option('database')); 46 \DB::setDefaultConnection($this->option('database'));
47 + $this->searchService->setConnection(\DB::connection($this->option('database')));
47 } 48 }
48 49
49 $this->searchService->indexAllEntities(); 50 $this->searchService->indexAllEntities();
......
...@@ -11,8 +11,8 @@ use BookStack\Role; ...@@ -11,8 +11,8 @@ use BookStack\Role;
11 use BookStack\User; 11 use BookStack\User;
12 use Illuminate\Database\Connection; 12 use Illuminate\Database\Connection;
13 use Illuminate\Database\Eloquent\Builder; 13 use Illuminate\Database\Eloquent\Builder;
14 +use Illuminate\Database\Query\Builder as QueryBuilder;
14 use Illuminate\Support\Collection; 15 use Illuminate\Support\Collection;
15 -use Illuminate\Support\Facades\Log;
16 16
17 class PermissionService 17 class PermissionService
18 { 18 {
...@@ -57,6 +57,15 @@ class PermissionService ...@@ -57,6 +57,15 @@ class PermissionService
57 } 57 }
58 58
59 /** 59 /**
60 + * Set the database connection
61 + * @param Connection $connection
62 + */
63 + public function setConnection(Connection $connection)
64 + {
65 + $this->db = $connection;
66 + }
67 +
68 + /**
60 * Prepare the local entity cache and ensure it's empty 69 * Prepare the local entity cache and ensure it's empty
61 */ 70 */
62 protected function readyEntityCache() 71 protected function readyEntityCache()
...@@ -138,10 +147,14 @@ class PermissionService ...@@ -138,10 +147,14 @@ class PermissionService
138 $this->readyEntityCache(); 147 $this->readyEntityCache();
139 148
140 // Get all roles (Should be the most limited dimension) 149 // Get all roles (Should be the most limited dimension)
141 - $roles = $this->role->with('permissions')->get(); 150 + $roles = $this->role->with('permissions')->get()->all();
142 151
143 // Chunk through all books 152 // Chunk through all books
144 - $this->book->newQuery()->with('chapters', 'pages')->chunk(5, function ($books) use ($roles) { 153 + $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
154 + $query->select(['id', 'restricted', 'created_by', 'book_id']);
155 + }, 'pages' => function($query) {
156 + $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
157 + }])->chunk(5, function ($books) use ($roles) {
145 $this->buildJointPermissionsForBooks($books, $roles); 158 $this->buildJointPermissionsForBooks($books, $roles);
146 }); 159 });
147 } 160 }
...@@ -149,17 +162,18 @@ class PermissionService ...@@ -149,17 +162,18 @@ class PermissionService
149 /** 162 /**
150 * Build joint permissions for an array of books 163 * Build joint permissions for an array of books
151 * @param Collection $books 164 * @param Collection $books
152 - * @param Collection $roles 165 + * @param array $roles
153 * @param bool $deleteOld 166 * @param bool $deleteOld
154 */ 167 */
155 protected function buildJointPermissionsForBooks($books, $roles, $deleteOld = false) { 168 protected function buildJointPermissionsForBooks($books, $roles, $deleteOld = false) {
156 $entities = clone $books; 169 $entities = clone $books;
157 170
158 - foreach ($books as $book) { 171 + /** @var Book $book */
159 - foreach ($book->chapters as $chapter) { 172 + foreach ($books->all() as $book) {
173 + foreach ($book->getRelation('chapters') as $chapter) {
160 $entities->push($chapter); 174 $entities->push($chapter);
161 } 175 }
162 - foreach ($book->pages as $page) { 176 + foreach ($book->getRelation('pages') as $page) {
163 $entities->push($page); 177 $entities->push($page);
164 } 178 }
165 } 179 }
...@@ -176,7 +190,12 @@ class PermissionService ...@@ -176,7 +190,12 @@ class PermissionService
176 { 190 {
177 $roles = $this->role->newQuery()->get(); 191 $roles = $this->role->newQuery()->get();
178 $book = ($entity->isA('book')) ? $entity : $entity->book; 192 $book = ($entity->isA('book')) ? $entity : $entity->book;
179 - $this->buildJointPermissionsForBooks(collect([$book]), $roles, true); 193 + $book = $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
194 + $query->select(['id', 'restricted', 'created_by', 'book_id']);
195 + }, 'pages' => function($query) {
196 + $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
197 + }])->where('id', '=', $book->id)->get();
198 + $this->buildJointPermissionsForBooks($book, $roles, true);
180 } 199 }
181 200
182 /** 201 /**
...@@ -196,12 +215,15 @@ class PermissionService ...@@ -196,12 +215,15 @@ class PermissionService
196 */ 215 */
197 public function buildJointPermissionForRole(Role $role) 216 public function buildJointPermissionForRole(Role $role)
198 { 217 {
199 - $roles = collect([$role]); 218 + $roles = [$role];
200 -
201 $this->deleteManyJointPermissionsForRoles($roles); 219 $this->deleteManyJointPermissionsForRoles($roles);
202 220
203 // Chunk through all books 221 // Chunk through all books
204 - $this->book->with('chapters', 'pages')->chunk(5, function ($books) use ($roles) { 222 + $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
223 + $query->select(['id', 'restricted', 'created_by', 'book_id']);
224 + }, 'pages' => function($query) {
225 + $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
226 + }])->chunk(5, function ($books) use ($roles) {
205 $this->buildJointPermissionsForBooks($books, $roles); 227 $this->buildJointPermissionsForBooks($books, $roles);
206 }); 228 });
207 } 229 }
...@@ -221,9 +243,10 @@ class PermissionService ...@@ -221,9 +243,10 @@ class PermissionService
221 */ 243 */
222 protected function deleteManyJointPermissionsForRoles($roles) 244 protected function deleteManyJointPermissionsForRoles($roles)
223 { 245 {
224 - foreach ($roles as $role) { 246 + $roleIds = array_map(function($role) {
225 - $role->jointPermissions()->delete(); 247 + return $role->id;
226 - } 248 + }, $roles);
249 + $this->jointPermission->newQuery()->whereIn('id', $roleIds)->delete();
227 } 250 }
228 251
229 /** 252 /**
...@@ -242,22 +265,27 @@ class PermissionService ...@@ -242,22 +265,27 @@ class PermissionService
242 protected function deleteManyJointPermissionsForEntities($entities) 265 protected function deleteManyJointPermissionsForEntities($entities)
243 { 266 {
244 if (count($entities) === 0) return; 267 if (count($entities) === 0) return;
245 - $query = $this->jointPermission->newQuery();
246 268
247 - foreach ($entities as $entity) { 269 + $this->db->transaction(function() use ($entities) {
248 - $query->orWhere(function($query) use ($entity) { 270 +
249 - $query->where('entity_id', '=', $entity->id) 271 + foreach (array_chunk($entities, 1000) as $entityChunk) {
250 - ->where('entity_type', '=', $entity->getMorphClass()); 272 + $query = $this->db->table('joint_permissions');
251 - }); 273 + foreach ($entityChunk as $entity) {
252 - } 274 + $query->orWhere(function(QueryBuilder $query) use ($entity) {
275 + $query->where('entity_id', '=', $entity->id)
276 + ->where('entity_type', '=', $entity->getMorphClass());
277 + });
278 + }
279 + $query->delete();
280 + }
253 281
254 - $query->delete(); 282 + });
255 } 283 }
256 284
257 /** 285 /**
258 * Create & Save entity jointPermissions for many entities and jointPermissions. 286 * Create & Save entity jointPermissions for many entities and jointPermissions.
259 * @param Collection $entities 287 * @param Collection $entities
260 - * @param Collection $roles 288 + * @param array $roles
261 */ 289 */
262 protected function createManyJointPermissions($entities, $roles) 290 protected function createManyJointPermissions($entities, $roles)
263 { 291 {
...@@ -299,9 +327,12 @@ class PermissionService ...@@ -299,9 +327,12 @@ class PermissionService
299 } 327 }
300 } 328 }
301 } 329 }
302 - foreach (array_chunk($jointPermissions, 5000) as $jointPermissionChunk) { 330 +
303 - $this->jointPermission->insert($jointPermissionChunk); 331 + $this->db->transaction(function() use ($jointPermissions) {
304 - } 332 + foreach (array_chunk($jointPermissions, 1000) as $jointPermissionChunk) {
333 + $this->db->table('joint_permissions')->insert($jointPermissionChunk);
334 + }
335 + });
305 } 336 }
306 337
307 338
...@@ -494,7 +525,7 @@ class PermissionService ...@@ -494,7 +525,7 @@ class PermissionService
494 * @param integer $book_id 525 * @param integer $book_id
495 * @param bool $filterDrafts 526 * @param bool $filterDrafts
496 * @param bool $fetchPageContent 527 * @param bool $fetchPageContent
497 - * @return \Illuminate\Database\Query\Builder 528 + * @return QueryBuilder
498 */ 529 */
499 public function bookChildrenQuery($book_id, $filterDrafts = false, $fetchPageContent = false) { 530 public function bookChildrenQuery($book_id, $filterDrafts = false, $fetchPageContent = false) {
500 $pageSelect = $this->db->table('pages')->selectRaw($this->page->entityRawQuery($fetchPageContent))->where('book_id', '=', $book_id)->where(function($query) use ($filterDrafts) { 531 $pageSelect = $this->db->table('pages')->selectRaw($this->page->entityRawQuery($fetchPageContent))->where('book_id', '=', $book_id)->where(function($query) use ($filterDrafts) {
......
...@@ -51,6 +51,15 @@ class SearchService ...@@ -51,6 +51,15 @@ class SearchService
51 } 51 }
52 52
53 /** 53 /**
54 + * Set the database connection
55 + * @param Connection $connection
56 + */
57 + public function setConnection(Connection $connection)
58 + {
59 + $this->db = $connection;
60 + }
61 +
62 + /**
54 * Search all entities in the system. 63 * Search all entities in the system.
55 * @param string $searchString 64 * @param string $searchString
56 * @param string $entityType 65 * @param string $entityType
......
1 <?php namespace Tests; 1 <?php namespace Tests;
2 2
3 use BookStack\Role; 3 use BookStack\Role;
4 +use BookStack\Services\PermissionService;
4 use Illuminate\Contracts\Console\Kernel; 5 use Illuminate\Contracts\Console\Kernel;
5 use Illuminate\Foundation\Testing\DatabaseTransactions; 6 use Illuminate\Foundation\Testing\DatabaseTransactions;
6 use Laravel\BrowserKitTesting\TestCase; 7 use Laravel\BrowserKitTesting\TestCase;
...@@ -105,11 +106,9 @@ abstract class BrowserKitTest extends TestCase ...@@ -105,11 +106,9 @@ abstract class BrowserKitTest extends TestCase
105 { 106 {
106 if ($updaterUser === false) $updaterUser = $creatorUser; 107 if ($updaterUser === false) $updaterUser = $creatorUser;
107 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]); 108 $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
108 - $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]); 109 + $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
109 - $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]); 110 + $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
110 - $book->chapters()->saveMany([$chapter]); 111 + $restrictionService = $this->app[PermissionService::class];
111 - $chapter->pages()->saveMany([$page]);
112 - $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
113 $restrictionService->buildJointPermissionsForEntity($book); 112 $restrictionService->buildJointPermissionsForEntity($book);
114 return [ 113 return [
115 'book' => $book, 114 'book' => $book,
......
1 <?php namespace Tests; 1 <?php namespace Tests;
2 2
3 +use BookStack\Repos\PermissionsRepo;
4 +use BookStack\Role;
5 +
3 class RolesTest extends BrowserKitTest 6 class RolesTest extends BrowserKitTest
4 { 7 {
5 protected $user; 8 protected $user;
...@@ -34,11 +37,11 @@ class RolesTest extends BrowserKitTest ...@@ -34,11 +37,11 @@ class RolesTest extends BrowserKitTest
34 /** 37 /**
35 * Create a new basic role for testing purposes. 38 * Create a new basic role for testing purposes.
36 * @param array $permissions 39 * @param array $permissions
37 - * @return static 40 + * @return Role
38 */ 41 */
39 protected function createNewRole($permissions = []) 42 protected function createNewRole($permissions = [])
40 { 43 {
41 - $permissionRepo = app('BookStack\Repos\PermissionsRepo'); 44 + $permissionRepo = app(PermissionsRepo::class);
42 $roleData = factory(\BookStack\Role::class)->make()->toArray(); 45 $roleData = factory(\BookStack\Role::class)->make()->toArray();
43 $roleData['permissions'] = array_flip($permissions); 46 $roleData['permissions'] = array_flip($permissions);
44 return $permissionRepo->saveNewRole($roleData); 47 return $permissionRepo->saveNewRole($roleData);
...@@ -107,16 +110,16 @@ class RolesTest extends BrowserKitTest ...@@ -107,16 +110,16 @@ class RolesTest extends BrowserKitTest
107 110
108 public function test_manage_user_permission() 111 public function test_manage_user_permission()
109 { 112 {
110 - $this->actingAs($this->user)->visit('/')->visit('/settings/users') 113 + $this->actingAs($this->user)->visit('/settings/users')
111 ->seePageIs('/'); 114 ->seePageIs('/');
112 $this->giveUserPermissions($this->user, ['users-manage']); 115 $this->giveUserPermissions($this->user, ['users-manage']);
113 - $this->actingAs($this->user)->visit('/')->visit('/settings/users') 116 + $this->actingAs($this->user)->visit('/settings/users')
114 ->seePageIs('/settings/users'); 117 ->seePageIs('/settings/users');
115 } 118 }
116 119
117 public function test_user_roles_manage_permission() 120 public function test_user_roles_manage_permission()
118 { 121 {
119 - $this->actingAs($this->user)->visit('/')->visit('/settings/roles') 122 + $this->actingAs($this->user)->visit('/settings/roles')
120 ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/'); 123 ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/');
121 $this->giveUserPermissions($this->user, ['user-roles-manage']); 124 $this->giveUserPermissions($this->user, ['user-roles-manage']);
122 $this->actingAs($this->user)->visit('/settings/roles') 125 $this->actingAs($this->user)->visit('/settings/roles')
...@@ -126,10 +129,10 @@ class RolesTest extends BrowserKitTest ...@@ -126,10 +129,10 @@ class RolesTest extends BrowserKitTest
126 129
127 public function test_settings_manage_permission() 130 public function test_settings_manage_permission()
128 { 131 {
129 - $this->actingAs($this->user)->visit('/')->visit('/settings') 132 + $this->actingAs($this->user)->visit('/settings')
130 ->seePageIs('/'); 133 ->seePageIs('/');
131 $this->giveUserPermissions($this->user, ['settings-manage']); 134 $this->giveUserPermissions($this->user, ['settings-manage']);
132 - $this->actingAs($this->user)->visit('/')->visit('/settings') 135 + $this->actingAs($this->user)->visit('/settings')
133 ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved'); 136 ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved');
134 } 137 }
135 138
...@@ -181,27 +184,26 @@ class RolesTest extends BrowserKitTest ...@@ -181,27 +184,26 @@ class RolesTest extends BrowserKitTest
181 * @param string $permission 184 * @param string $permission
182 * @param array $accessUrls Urls that are only accessible after having the permission 185 * @param array $accessUrls Urls that are only accessible after having the permission
183 * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission 186 * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission
184 - * @param null $callback
185 */ 187 */
186 private function checkAccessPermission($permission, $accessUrls = [], $visibles = []) 188 private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
187 { 189 {
188 foreach ($accessUrls as $url) { 190 foreach ($accessUrls as $url) {
189 - $this->actingAs($this->user)->visit('/')->visit($url) 191 + $this->actingAs($this->user)->visit($url)
190 ->seePageIs('/'); 192 ->seePageIs('/');
191 } 193 }
192 foreach ($visibles as $url => $text) { 194 foreach ($visibles as $url => $text) {
193 - $this->actingAs($this->user)->visit('/')->visit($url) 195 + $this->actingAs($this->user)->visit($url)
194 ->dontSeeInElement('.action-buttons',$text); 196 ->dontSeeInElement('.action-buttons',$text);
195 } 197 }
196 198
197 $this->giveUserPermissions($this->user, [$permission]); 199 $this->giveUserPermissions($this->user, [$permission]);
198 200
199 foreach ($accessUrls as $url) { 201 foreach ($accessUrls as $url) {
200 - $this->actingAs($this->user)->visit('/')->visit($url) 202 + $this->actingAs($this->user)->visit($url)
201 ->seePageIs($url); 203 ->seePageIs($url);
202 } 204 }
203 foreach ($visibles as $url => $text) { 205 foreach ($visibles as $url => $text) {
204 - $this->actingAs($this->user)->visit('/')->visit($url) 206 + $this->actingAs($this->user)->visit($url)
205 ->see($text); 207 ->see($text);
206 } 208 }
207 } 209 }
...@@ -391,8 +393,8 @@ class RolesTest extends BrowserKitTest ...@@ -391,8 +393,8 @@ class RolesTest extends BrowserKitTest
391 393
392 public function test_page_create_own_permissions() 394 public function test_page_create_own_permissions()
393 { 395 {
394 - $book = \BookStack\Book::take(1)->get()->first(); 396 + $book = \BookStack\Book::first();
395 - $chapter = \BookStack\Chapter::take(1)->get()->first(); 397 + $chapter = \BookStack\Chapter::first();
396 398
397 $entities = $this->createEntityChainBelongingToUser($this->user); 399 $entities = $this->createEntityChainBelongingToUser($this->user);
398 $ownBook = $entities['book']; 400 $ownBook = $entities['book'];
...@@ -405,7 +407,7 @@ class RolesTest extends BrowserKitTest ...@@ -405,7 +407,7 @@ class RolesTest extends BrowserKitTest
405 $accessUrls = [$createUrl, $createUrlChapter]; 407 $accessUrls = [$createUrl, $createUrlChapter];
406 408
407 foreach ($accessUrls as $url) { 409 foreach ($accessUrls as $url) {
408 - $this->actingAs($this->user)->visit('/')->visit($url) 410 + $this->actingAs($this->user)->visit($url)
409 ->seePageIs('/'); 411 ->seePageIs('/');
410 } 412 }
411 413
...@@ -417,7 +419,7 @@ class RolesTest extends BrowserKitTest ...@@ -417,7 +419,7 @@ class RolesTest extends BrowserKitTest
417 $this->giveUserPermissions($this->user, ['page-create-own']); 419 $this->giveUserPermissions($this->user, ['page-create-own']);
418 420
419 foreach ($accessUrls as $index => $url) { 421 foreach ($accessUrls as $index => $url) {
420 - $this->actingAs($this->user)->visit('/')->visit($url); 422 + $this->actingAs($this->user)->visit($url);
421 $expectedUrl = \BookStack\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl(); 423 $expectedUrl = \BookStack\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
422 $this->seePageIs($expectedUrl); 424 $this->seePageIs($expectedUrl);
423 } 425 }
...@@ -449,7 +451,7 @@ class RolesTest extends BrowserKitTest ...@@ -449,7 +451,7 @@ class RolesTest extends BrowserKitTest
449 $accessUrls = [$createUrl, $createUrlChapter]; 451 $accessUrls = [$createUrl, $createUrlChapter];
450 452
451 foreach ($accessUrls as $url) { 453 foreach ($accessUrls as $url) {
452 - $this->actingAs($this->user)->visit('/')->visit($url) 454 + $this->actingAs($this->user)->visit($url)
453 ->seePageIs('/'); 455 ->seePageIs('/');
454 } 456 }
455 457
...@@ -461,7 +463,7 @@ class RolesTest extends BrowserKitTest ...@@ -461,7 +463,7 @@ class RolesTest extends BrowserKitTest
461 $this->giveUserPermissions($this->user, ['page-create-all']); 463 $this->giveUserPermissions($this->user, ['page-create-all']);
462 464
463 foreach ($accessUrls as $index => $url) { 465 foreach ($accessUrls as $index => $url) {
464 - $this->actingAs($this->user)->visit('/')->visit($url); 466 + $this->actingAs($this->user)->visit($url);
465 $expectedUrl = \BookStack\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl(); 467 $expectedUrl = \BookStack\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
466 $this->seePageIs($expectedUrl); 468 $this->seePageIs($expectedUrl);
467 } 469 }
......