3 namespace Tests\Permissions;
5 use BookStack\Auth\User;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Models\Page;
12 use Illuminate\Support\Str;
15 class EntityPermissionsTest extends TestCase
18 protected User $viewer;
20 protected function setUp(): void
23 $this->user = $this->getEditor();
24 $this->viewer = $this->getViewer();
27 protected function setRestrictionsForTestRoles(Entity $entity, array $actions = [])
30 $this->user->roles->first(),
31 $this->viewer->roles->first(),
33 $this->entities->setPermissions($entity, $actions, $roles);
36 public function test_bookshelf_view_restriction()
38 $shelf = $this->entities->shelf();
40 $this->actingAs($this->user)
41 ->get($shelf->getUrl())
44 $this->setRestrictionsForTestRoles($shelf, []);
46 $this->followingRedirects()->get($shelf->getUrl())
47 ->assertSee('Shelf not found');
49 $this->setRestrictionsForTestRoles($shelf, ['view']);
51 $this->get($shelf->getUrl())
52 ->assertSee($shelf->name);
55 public function test_bookshelf_update_restriction()
57 $shelf = $this->entities->shelf();
59 $this->actingAs($this->user)
60 ->get($shelf->getUrl('/edit'))
61 ->assertSee('Edit Shelf');
63 $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
65 $resp = $this->get($shelf->getUrl('/edit'))
66 ->assertRedirect('/');
67 $this->followRedirects($resp)->assertSee('You do not have permission');
69 $this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
71 $this->get($shelf->getUrl('/edit'))
75 public function test_bookshelf_delete_restriction()
77 $shelf = $this->entities->shelf();
79 $this->actingAs($this->user)
80 ->get($shelf->getUrl('/delete'))
81 ->assertSee('Delete Shelf');
83 $this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
85 $this->get($shelf->getUrl('/delete'))->assertRedirect('/');
86 $this->get('/')->assertSee('You do not have permission');
88 $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
90 $this->get($shelf->getUrl('/delete'))
92 ->assertSee('Delete Shelf');
95 public function test_book_view_restriction()
97 $book = $this->entities->book();
98 $bookPage = $book->pages->first();
99 $bookChapter = $book->chapters->first();
101 $bookUrl = $book->getUrl();
102 $this->actingAs($this->user)
106 $this->setRestrictionsForTestRoles($book, []);
108 $this->followingRedirects()->get($bookUrl)
109 ->assertSee('Book not found');
110 $this->followingRedirects()->get($bookPage->getUrl())
111 ->assertSee('Page not found');
112 $this->followingRedirects()->get($bookChapter->getUrl())
113 ->assertSee('Chapter not found');
115 $this->setRestrictionsForTestRoles($book, ['view']);
118 ->assertSee($book->name);
119 $this->get($bookPage->getUrl())
120 ->assertSee($bookPage->name);
121 $this->get($bookChapter->getUrl())
122 ->assertSee($bookChapter->name);
125 public function test_book_create_restriction()
127 $book = $this->entities->book();
129 $bookUrl = $book->getUrl();
130 $resp = $this->actingAs($this->viewer)->get($bookUrl);
131 $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
132 ->assertElementNotContains('.actions', 'New Chapter');
133 $resp = $this->actingAs($this->user)->get($bookUrl);
134 $this->withHtml($resp)->assertElementContains('.actions', 'New Page')
135 ->assertElementContains('.actions', 'New Chapter');
137 $this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']);
139 $this->get($bookUrl . '/create-chapter')->assertRedirect('/');
140 $this->get('/')->assertSee('You do not have permission');
142 $this->get($bookUrl . '/create-page')->assertRedirect('/');
143 $this->get('/')->assertSee('You do not have permission');
145 $resp = $this->get($bookUrl);
146 $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
147 ->assertElementNotContains('.actions', 'New Chapter');
149 $this->setRestrictionsForTestRoles($book, ['view', 'create']);
151 $resp = $this->post($book->getUrl('/create-chapter'), [
152 'name' => 'test chapter',
153 'description' => 'desc',
155 $resp->assertRedirect($book->getUrl('/chapter/test-chapter'));
157 $this->get($book->getUrl('/create-page'));
158 /** @var Page $page */
159 $page = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first();
160 $resp = $this->post($page->getUrl(), [
161 'name' => 'test page',
162 'html' => 'test content',
164 $resp->assertRedirect($book->getUrl('/page/test-page'));
166 $resp = $this->get($bookUrl);
167 $this->withHtml($resp)->assertElementContains('.actions', 'New Page')
168 ->assertElementContains('.actions', 'New Chapter');
171 public function test_book_update_restriction()
173 $book = $this->entities->book();
174 $bookPage = $book->pages->first();
175 $bookChapter = $book->chapters->first();
177 $bookUrl = $book->getUrl();
178 $this->actingAs($this->user)
179 ->get($bookUrl . '/edit')
180 ->assertSee('Edit Book');
182 $this->setRestrictionsForTestRoles($book, ['view', 'delete']);
184 $this->get($bookUrl . '/edit')->assertRedirect('/');
185 $this->get('/')->assertSee('You do not have permission');
186 $this->get($bookPage->getUrl() . '/edit')->assertRedirect('/');
187 $this->get('/')->assertSee('You do not have permission');
188 $this->get($bookChapter->getUrl() . '/edit')->assertRedirect('/');
189 $this->get('/')->assertSee('You do not have permission');
191 $this->setRestrictionsForTestRoles($book, ['view', 'update']);
193 $this->get($bookUrl . '/edit')->assertOk();
194 $this->get($bookPage->getUrl() . '/edit')->assertOk();
195 $this->get($bookChapter->getUrl() . '/edit')->assertSee('Edit Chapter');
198 public function test_book_delete_restriction()
200 $book = $this->entities->book();
201 $bookPage = $book->pages->first();
202 $bookChapter = $book->chapters->first();
204 $bookUrl = $book->getUrl();
205 $this->actingAs($this->user)->get($bookUrl . '/delete')
206 ->assertSee('Delete Book');
208 $this->setRestrictionsForTestRoles($book, ['view', 'update']);
210 $this->get($bookUrl . '/delete')->assertRedirect('/');
211 $this->get('/')->assertSee('You do not have permission');
212 $this->get($bookPage->getUrl() . '/delete')->assertRedirect('/');
213 $this->get('/')->assertSee('You do not have permission');
214 $this->get($bookChapter->getUrl() . '/delete')->assertRedirect('/');
215 $this->get('/')->assertSee('You do not have permission');
217 $this->setRestrictionsForTestRoles($book, ['view', 'delete']);
219 $this->get($bookUrl . '/delete')->assertOk()->assertSee('Delete Book');
220 $this->get($bookPage->getUrl('/delete'))->assertOk()->assertSee('Delete Page');
221 $this->get($bookChapter->getUrl('/delete'))->assertSee('Delete Chapter');
224 public function test_chapter_view_restriction()
226 $chapter = $this->entities->chapter();
227 $chapterPage = $chapter->pages->first();
229 $chapterUrl = $chapter->getUrl();
230 $this->actingAs($this->user)->get($chapterUrl)->assertOk();
232 $this->setRestrictionsForTestRoles($chapter, []);
234 $this->followingRedirects()->get($chapterUrl)->assertSee('Chapter not found');
235 $this->followingRedirects()->get($chapterPage->getUrl())->assertSee('Page not found');
237 $this->setRestrictionsForTestRoles($chapter, ['view']);
239 $this->get($chapterUrl)->assertSee($chapter->name);
240 $this->get($chapterPage->getUrl())->assertSee($chapterPage->name);
243 public function test_chapter_create_restriction()
245 $chapter = $this->entities->chapter();
247 $chapterUrl = $chapter->getUrl();
248 $resp = $this->actingAs($this->user)->get($chapterUrl);
249 $this->withHtml($resp)->assertElementContains('.actions', 'New Page');
251 $this->setRestrictionsForTestRoles($chapter, ['view', 'delete', 'update']);
253 $this->get($chapterUrl . '/create-page')->assertRedirect('/');
254 $this->get('/')->assertSee('You do not have permission');
255 $this->withHtml($this->get($chapterUrl))->assertElementNotContains('.actions', 'New Page');
257 $this->setRestrictionsForTestRoles($chapter, ['view', 'create']);
259 $this->get($chapter->getUrl('/create-page'));
260 /** @var Page $page */
261 $page = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first();
262 $resp = $this->post($page->getUrl(), [
263 'name' => 'test page',
264 'html' => 'test content',
266 $resp->assertRedirect($chapter->book->getUrl('/page/test-page'));
268 $this->withHtml($this->get($chapterUrl))->assertElementContains('.actions', 'New Page');
271 public function test_chapter_update_restriction()
273 $chapter = $this->entities->chapter();
274 $chapterPage = $chapter->pages->first();
276 $chapterUrl = $chapter->getUrl();
277 $this->actingAs($this->user)->get($chapterUrl . '/edit')
278 ->assertSee('Edit Chapter');
280 $this->setRestrictionsForTestRoles($chapter, ['view', 'delete']);
282 $this->get($chapterUrl . '/edit')->assertRedirect('/');
283 $this->get('/')->assertSee('You do not have permission');
284 $this->get($chapterPage->getUrl() . '/edit')->assertRedirect('/');
285 $this->get('/')->assertSee('You do not have permission');
287 $this->setRestrictionsForTestRoles($chapter, ['view', 'update']);
289 $this->get($chapterUrl . '/edit')->assertOk()->assertSee('Edit Chapter');
290 $this->get($chapterPage->getUrl() . '/edit')->assertOk();
293 public function test_chapter_delete_restriction()
295 $chapter = $this->entities->chapter();
296 $chapterPage = $chapter->pages->first();
298 $chapterUrl = $chapter->getUrl();
299 $this->actingAs($this->user)
300 ->get($chapterUrl . '/delete')
301 ->assertSee('Delete Chapter');
303 $this->setRestrictionsForTestRoles($chapter, ['view', 'update']);
305 $this->get($chapterUrl . '/delete')->assertRedirect('/');
306 $this->get('/')->assertSee('You do not have permission');
307 $this->get($chapterPage->getUrl() . '/delete')->assertRedirect('/');
308 $this->get('/')->assertSee('You do not have permission');
310 $this->setRestrictionsForTestRoles($chapter, ['view', 'delete']);
312 $this->get($chapterUrl . '/delete')->assertOk()->assertSee('Delete Chapter');
313 $this->get($chapterPage->getUrl() . '/delete')->assertOk()->assertSee('Delete Page');
316 public function test_page_view_restriction()
318 $page = $this->entities->page();
320 $pageUrl = $page->getUrl();
321 $this->actingAs($this->user)->get($pageUrl)->assertOk();
323 $this->setRestrictionsForTestRoles($page, ['update', 'delete']);
325 $this->get($pageUrl)->assertSee('Page not found');
327 $this->setRestrictionsForTestRoles($page, ['view']);
329 $this->get($pageUrl)->assertSee($page->name);
332 public function test_page_update_restriction()
334 $page = $this->entities->page();
336 $pageUrl = $page->getUrl();
337 $resp = $this->actingAs($this->user)
338 ->get($pageUrl . '/edit');
339 $this->withHtml($resp)->assertElementExists('input[name="name"][value="' . $page->name . '"]');
341 $this->setRestrictionsForTestRoles($page, ['view', 'delete']);
343 $this->get($pageUrl . '/edit')->assertRedirect('/');
344 $this->get('/')->assertSee('You do not have permission');
346 $this->setRestrictionsForTestRoles($page, ['view', 'update']);
348 $resp = $this->get($pageUrl . '/edit')
350 $this->withHtml($resp)->assertElementExists('input[name="name"][value="' . $page->name . '"]');
353 public function test_page_delete_restriction()
355 $page = $this->entities->page();
357 $pageUrl = $page->getUrl();
358 $this->actingAs($this->user)
359 ->get($pageUrl . '/delete')
360 ->assertSee('Delete Page');
362 $this->setRestrictionsForTestRoles($page, ['view', 'update']);
364 $this->get($pageUrl . '/delete')->assertRedirect('/');
365 $this->get('/')->assertSee('You do not have permission');
367 $this->setRestrictionsForTestRoles($page, ['view', 'delete']);
369 $this->get($pageUrl . '/delete')->assertOk()->assertSee('Delete Page');
372 protected function entityRestrictionFormTest(string $model, string $title, string $permission, string $roleId)
374 /** @var Entity $modelInstance */
375 $modelInstance = $model::query()->first();
376 $this->asAdmin()->get($modelInstance->getUrl('/permissions'))
379 $this->put($modelInstance->getUrl('/permissions'), [
382 $permission => 'true',
387 $this->assertDatabaseHas('entity_permissions', [
388 'entity_id' => $modelInstance->id,
389 'entity_type' => $modelInstance->getMorphClass(),
390 'role_id' => $roleId,
395 public function test_bookshelf_restriction_form()
397 $this->entityRestrictionFormTest(Bookshelf::class, 'Shelf Permissions', 'view', '2');
400 public function test_book_restriction_form()
402 $this->entityRestrictionFormTest(Book::class, 'Book Permissions', 'view', '2');
405 public function test_chapter_restriction_form()
407 $this->entityRestrictionFormTest(Chapter::class, 'Chapter Permissions', 'update', '2');
410 public function test_page_restriction_form()
412 $this->entityRestrictionFormTest(Page::class, 'Page Permissions', 'delete', '2');
415 public function test_restricted_pages_not_visible_in_book_navigation_on_pages()
417 $chapter = $this->entities->chapter();
418 $page = $chapter->pages->first();
419 $page2 = $chapter->pages[2];
421 $this->setRestrictionsForTestRoles($page, []);
423 $resp = $this->actingAs($this->user)->get($page2->getUrl());
424 $this->withHtml($resp)->assertElementNotContains('.sidebar-page-list', $page->name);
427 public function test_restricted_pages_not_visible_in_book_navigation_on_chapters()
429 $chapter = $this->entities->chapter();
430 $page = $chapter->pages->first();
432 $this->setRestrictionsForTestRoles($page, []);
434 $resp = $this->actingAs($this->user)->get($chapter->getUrl());
435 $this->withHtml($resp)->assertElementNotContains('.sidebar-page-list', $page->name);
438 public function test_restricted_pages_not_visible_on_chapter_pages()
440 $chapter = $this->entities->chapter();
441 $page = $chapter->pages->first();
443 $this->setRestrictionsForTestRoles($page, []);
445 $this->actingAs($this->user)
446 ->get($chapter->getUrl())
447 ->assertDontSee($page->name);
450 public function test_restricted_chapter_pages_not_visible_on_book_page()
452 $chapter = $this->entities->chapter();
453 $this->actingAs($this->user)
454 ->get($chapter->book->getUrl())
455 ->assertSee($chapter->pages->first()->name);
457 foreach ($chapter->pages as $page) {
458 $this->setRestrictionsForTestRoles($page, []);
461 $this->actingAs($this->user)
462 ->get($chapter->book->getUrl())
463 ->assertDontSee($chapter->pages->first()->name);
466 public function test_bookshelf_update_restriction_override()
468 $shelf = $this->entities->shelf();
470 $this->actingAs($this->viewer)
471 ->get($shelf->getUrl('/edit'))
472 ->assertDontSee('Edit Book');
474 $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
476 $this->get($shelf->getUrl('/edit'))->assertRedirect('/');
477 $this->get('/')->assertSee('You do not have permission');
479 $this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
481 $this->get($shelf->getUrl('/edit'))->assertOk();
484 public function test_bookshelf_delete_restriction_override()
486 $shelf = $this->entities->shelf();
488 $this->actingAs($this->viewer)
489 ->get($shelf->getUrl('/delete'))
490 ->assertDontSee('Delete Book');
492 $this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
494 $this->get($shelf->getUrl('/delete'))->assertRedirect('/');
495 $this->get('/')->assertSee('You do not have permission');
497 $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
499 $this->get($shelf->getUrl('/delete'))->assertOk()->assertSee('Delete Shelf');
502 public function test_book_create_restriction_override()
504 $book = $this->entities->book();
506 $bookUrl = $book->getUrl();
507 $resp = $this->actingAs($this->viewer)->get($bookUrl);
508 $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
509 ->assertElementNotContains('.actions', 'New Chapter');
511 $this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']);
513 $this->get($bookUrl . '/create-chapter')->assertRedirect('/');
514 $this->get('/')->assertSee('You do not have permission');
515 $this->get($bookUrl . '/create-page')->assertRedirect('/');
516 $this->get('/')->assertSee('You do not have permission');
517 $resp = $this->get($bookUrl);
518 $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
519 ->assertElementNotContains('.actions', 'New Chapter');
521 $this->setRestrictionsForTestRoles($book, ['view', 'create']);
523 $resp = $this->post($book->getUrl('/create-chapter'), [
524 'name' => 'test chapter',
525 'description' => 'test desc',
527 $resp->assertRedirect($book->getUrl('/chapter/test-chapter'));
529 $this->get($book->getUrl('/create-page'));
530 /** @var Page $page */
531 $page = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
532 $resp = $this->post($page->getUrl(), [
533 'name' => 'test page',
534 'html' => 'test desc',
536 $resp->assertRedirect($book->getUrl('/page/test-page'));
538 $resp = $this->get($bookUrl);
539 $this->withHtml($resp)->assertElementContains('.actions', 'New Page')
540 ->assertElementContains('.actions', 'New Chapter');
543 public function test_book_update_restriction_override()
545 $book = $this->entities->book();
546 $bookPage = $book->pages->first();
547 $bookChapter = $book->chapters->first();
549 $bookUrl = $book->getUrl();
550 $this->actingAs($this->viewer)->get($bookUrl . '/edit')
551 ->assertDontSee('Edit Book');
553 $this->setRestrictionsForTestRoles($book, ['view', 'delete']);
555 $this->get($bookUrl . '/edit')->assertRedirect('/');
556 $this->get('/')->assertSee('You do not have permission');
557 $this->get($bookPage->getUrl() . '/edit')->assertRedirect('/');
558 $this->get('/')->assertSee('You do not have permission');
559 $this->get($bookChapter->getUrl() . '/edit')->assertRedirect('/');
560 $this->get('/')->assertSee('You do not have permission');
562 $this->setRestrictionsForTestRoles($book, ['view', 'update']);
564 $this->get($bookUrl . '/edit')->assertOk();
565 $this->get($bookPage->getUrl() . '/edit')->assertOk();
566 $this->get($bookChapter->getUrl() . '/edit')->assertSee('Edit Chapter');
569 public function test_book_delete_restriction_override()
571 $book = $this->entities->book();
572 $bookPage = $book->pages->first();
573 $bookChapter = $book->chapters->first();
575 $bookUrl = $book->getUrl();
576 $this->actingAs($this->viewer)
577 ->get($bookUrl . '/delete')
578 ->assertDontSee('Delete Book');
580 $this->setRestrictionsForTestRoles($book, ['view', 'update']);
582 $this->get($bookUrl . '/delete')->assertRedirect('/');
583 $this->get('/')->assertSee('You do not have permission');
584 $this->get($bookPage->getUrl() . '/delete')->assertRedirect('/');
585 $this->get('/')->assertSee('You do not have permission');
586 $this->get($bookChapter->getUrl() . '/delete')->assertRedirect('/');
587 $this->get('/')->assertSee('You do not have permission');
589 $this->setRestrictionsForTestRoles($book, ['view', 'delete']);
591 $this->get($bookUrl . '/delete')->assertOk()->assertSee('Delete Book');
592 $this->get($bookPage->getUrl() . '/delete')->assertOk()->assertSee('Delete Page');
593 $this->get($bookChapter->getUrl() . '/delete')->assertSee('Delete Chapter');
596 public function test_page_visible_if_has_permissions_when_book_not_visible()
598 $book = $this->entities->book();
599 $bookChapter = $book->chapters->first();
600 $bookPage = $bookChapter->pages->first();
602 foreach ([$book, $bookChapter, $bookPage] as $entity) {
603 $entity->name = Str::random(24);
607 $this->setRestrictionsForTestRoles($book, []);
608 $this->setRestrictionsForTestRoles($bookPage, ['view']);
610 $this->actingAs($this->viewer);
611 $resp = $this->get($bookPage->getUrl());
613 $resp->assertSee($bookPage->name);
614 $resp->assertDontSee(substr($book->name, 0, 15));
615 $resp->assertDontSee(substr($bookChapter->name, 0, 15));
618 public function test_book_sort_view_permission()
620 /** @var Book $firstBook */
621 $firstBook = Book::query()->first();
622 /** @var Book $secondBook */
623 $secondBook = Book::query()->find(2);
625 $this->setRestrictionsForTestRoles($firstBook, ['view', 'update']);
626 $this->setRestrictionsForTestRoles($secondBook, ['view']);
628 // Test sort page visibility
629 $this->actingAs($this->user)->get($secondBook->getUrl('/sort'))->assertRedirect('/');
630 $this->get('/')->assertSee('You do not have permission');
632 // Check sort page on first book
633 $this->actingAs($this->user)->get($firstBook->getUrl('/sort'));
636 public function test_can_create_page_if_chapter_has_permissions_when_book_not_visible()
638 $book = $this->entities->book();
639 $this->setRestrictionsForTestRoles($book, []);
640 $bookChapter = $book->chapters->first();
641 $this->setRestrictionsForTestRoles($bookChapter, ['view']);
643 $this->actingAs($this->user)->get($bookChapter->getUrl())
644 ->assertDontSee('New Page');
646 $this->setRestrictionsForTestRoles($bookChapter, ['view', 'create']);
648 $this->get($bookChapter->getUrl('/create-page'));
649 /** @var Page $page */
650 $page = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
651 $resp = $this->post($page->getUrl(), [
652 'name' => 'test page',
653 'html' => 'test content',
655 $resp->assertRedirect($book->getUrl('/page/test-page'));
658 public function test_book_permissions_can_be_generated_without_error_if_child_chapter_is_in_recycle_bin()
660 $book = $this->entities->bookHasChaptersAndPages();
661 /** @var Chapter $chapter */
662 $chapter = $book->chapters()->first();
664 $this->asAdmin()->delete($chapter->getUrl());
668 $this->entities->setPermissions($book, ['view'], []);
669 } catch (Exception $e) {
673 $this->assertNull($error);