4 use BookStack\Repos\PermissionsRepo;
6 use Laravel\BrowserKitTesting\HttpException;
7 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9 class RolesTest extends BrowserKitTest
13 public function setUp()
16 $this->user = $this->getViewer();
19 protected function getViewer()
21 $role = \BookStack\Role::getRole('viewer');
22 $viewer = $this->getNewBlankUser();
23 $viewer->attachRole($role);;
28 * Give the given user some permissions.
29 * @param \BookStack\User $user
30 * @param array $permissions
32 protected function giveUserPermissions(\BookStack\User $user, $permissions = [])
34 $newRole = $this->createNewRole($permissions);
35 $user->attachRole($newRole);
37 $user->permissions(false);
41 * Create a new basic role for testing purposes.
42 * @param array $permissions
45 protected function createNewRole($permissions = [])
47 $permissionRepo = app(PermissionsRepo::class);
48 $roleData = factory(\BookStack\Role::class)->make()->toArray();
49 $roleData['permissions'] = array_flip($permissions);
50 return $permissionRepo->saveNewRole($roleData);
53 public function test_admin_can_see_settings()
55 $this->asAdmin()->visit('/settings')->see('Settings');
58 public function test_cannot_delete_admin_role()
60 $adminRole = \BookStack\Role::getRole('admin');
61 $deletePageUrl = '/settings/roles/delete/' . $adminRole->id;
62 $this->asAdmin()->visit($deletePageUrl)
64 ->seePageIs($deletePageUrl)
65 ->see('cannot be deleted');
68 public function test_role_cannot_be_deleted_if_default()
70 $newRole = $this->createNewRole();
71 $this->setSettings(['registration-role' => $newRole->id]);
73 $deletePageUrl = '/settings/roles/delete/' . $newRole->id;
74 $this->asAdmin()->visit($deletePageUrl)
76 ->seePageIs($deletePageUrl)
77 ->see('cannot be deleted');
80 public function test_role_create_update_delete_flow()
82 $testRoleName = 'Test Role';
83 $testRoleDesc = 'a little test description';
84 $testRoleUpdateName = 'An Super Updated role';
87 $this->asAdmin()->visit('/settings')
89 ->seePageIs('/settings/roles')
90 ->click('Create New Role')
91 ->type('Test Role', 'display_name')
92 ->type('A little test description', 'description')
94 ->seeInDatabase('roles', ['display_name' => $testRoleName, 'name' => 'test-role', 'description' => $testRoleDesc])
95 ->seePageIs('/settings/roles');
97 $this->asAdmin()->visit('/settings/roles')
99 ->click($testRoleName)
100 ->type($testRoleUpdateName, '#display_name')
102 ->seeInDatabase('roles', ['display_name' => $testRoleUpdateName, 'name' => 'test-role', 'description' => $testRoleDesc])
103 ->seePageIs('/settings/roles');
105 $this->asAdmin()->visit('/settings/roles')
106 ->click($testRoleUpdateName)
107 ->click('Delete Role')
108 ->see($testRoleUpdateName)
110 ->seePageIs('/settings/roles')
111 ->dontSee($testRoleUpdateName);
114 public function test_manage_user_permission()
116 $this->actingAs($this->user)->visit('/settings/users')
118 $this->giveUserPermissions($this->user, ['users-manage']);
119 $this->actingAs($this->user)->visit('/settings/users')
120 ->seePageIs('/settings/users');
123 public function test_user_roles_manage_permission()
125 $this->actingAs($this->user)->visit('/settings/roles')
126 ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/');
127 $this->giveUserPermissions($this->user, ['user-roles-manage']);
128 $this->actingAs($this->user)->visit('/settings/roles')
129 ->seePageIs('/settings/roles')->click('Admin')
133 public function test_settings_manage_permission()
135 $this->actingAs($this->user)->visit('/settings')
137 $this->giveUserPermissions($this->user, ['settings-manage']);
138 $this->actingAs($this->user)->visit('/settings')
139 ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved');
142 public function test_restrictions_manage_all_permission()
144 $page = \BookStack\Page::take(1)->get()->first();
145 $this->actingAs($this->user)->visit($page->getUrl())
146 ->dontSee('Permissions')
147 ->visit($page->getUrl() . '/permissions')
149 $this->giveUserPermissions($this->user, ['restrictions-manage-all']);
150 $this->actingAs($this->user)->visit($page->getUrl())
152 ->click('Permissions')
153 ->see('Page Permissions')->seePageIs($page->getUrl() . '/permissions');
156 public function test_restrictions_manage_own_permission()
158 $otherUsersPage = \BookStack\Page::first();
159 $content = $this->createEntityChainBelongingToUser($this->user);
160 // Check can't restrict other's content
161 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
162 ->dontSee('Permissions')
163 ->visit($otherUsersPage->getUrl() . '/permissions')
165 // Check can't restrict own content
166 $this->actingAs($this->user)->visit($content['page']->getUrl())
167 ->dontSee('Permissions')
168 ->visit($content['page']->getUrl() . '/permissions')
171 $this->giveUserPermissions($this->user, ['restrictions-manage-own']);
173 // Check can't restrict other's content
174 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
175 ->dontSee('Permissions')
176 ->visit($otherUsersPage->getUrl() . '/permissions')
178 // Check can restrict own content
179 $this->actingAs($this->user)->visit($content['page']->getUrl())
181 ->click('Permissions')
182 ->seePageIs($content['page']->getUrl() . '/permissions');
186 * Check a standard entity access permission
187 * @param string $permission
188 * @param array $accessUrls Urls that are only accessible after having the permission
189 * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission
191 private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
193 foreach ($accessUrls as $url) {
194 $this->actingAs($this->user)->visit($url)
197 foreach ($visibles as $url => $text) {
198 $this->actingAs($this->user)->visit($url)
199 ->dontSeeInElement('.action-buttons',$text);
202 $this->giveUserPermissions($this->user, [$permission]);
204 foreach ($accessUrls as $url) {
205 $this->actingAs($this->user)->visit($url)
208 foreach ($visibles as $url => $text) {
209 $this->actingAs($this->user)->visit($url)
214 public function test_books_create_all_permissions()
216 $this->checkAccessPermission('book-create-all', [
219 '/books' => 'Create New Book'
222 $this->visit('/create-book')
223 ->type('test book', 'name')
224 ->type('book desc', 'description')
226 ->seePageIs('/books/test-book');
229 public function test_books_edit_own_permission()
231 $otherBook = \BookStack\Book::take(1)->get()->first();
232 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
233 $this->checkAccessPermission('book-update-own', [
234 $ownBook->getUrl() . '/edit'
236 $ownBook->getUrl() => 'Edit'
239 $this->visit($otherBook->getUrl())
240 ->dontSeeInElement('.action-buttons', 'Edit')
241 ->visit($otherBook->getUrl() . '/edit')
245 public function test_books_edit_all_permission()
247 $otherBook = \BookStack\Book::take(1)->get()->first();
248 $this->checkAccessPermission('book-update-all', [
249 $otherBook->getUrl() . '/edit'
251 $otherBook->getUrl() => 'Edit'
255 public function test_books_delete_own_permission()
257 $this->giveUserPermissions($this->user, ['book-update-all']);
258 $otherBook = \BookStack\Book::take(1)->get()->first();
259 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
260 $this->checkAccessPermission('book-delete-own', [
261 $ownBook->getUrl() . '/delete'
263 $ownBook->getUrl() => 'Delete'
266 $this->visit($otherBook->getUrl())
267 ->dontSeeInElement('.action-buttons', 'Delete')
268 ->visit($otherBook->getUrl() . '/delete')
270 $this->visit($ownBook->getUrl())->visit($ownBook->getUrl() . '/delete')
272 ->seePageIs('/books')
273 ->dontSee($ownBook->name);
276 public function test_books_delete_all_permission()
278 $this->giveUserPermissions($this->user, ['book-update-all']);
279 $otherBook = \BookStack\Book::take(1)->get()->first();
280 $this->checkAccessPermission('book-delete-all', [
281 $otherBook->getUrl() . '/delete'
283 $otherBook->getUrl() => 'Delete'
286 $this->visit($otherBook->getUrl())->visit($otherBook->getUrl() . '/delete')
288 ->seePageIs('/books')
289 ->dontSee($otherBook->name);
292 public function test_chapter_create_own_permissions()
294 $book = \BookStack\Book::take(1)->get()->first();
295 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
296 $this->checkAccessPermission('chapter-create-own', [
297 $ownBook->getUrl('/create-chapter')
299 $ownBook->getUrl() => 'New Chapter'
302 $this->visit($ownBook->getUrl('/create-chapter'))
303 ->type('test chapter', 'name')
304 ->type('chapter desc', 'description')
305 ->press('Save Chapter')
306 ->seePageIs($ownBook->getUrl('/chapter/test-chapter'));
308 $this->visit($book->getUrl())
309 ->dontSeeInElement('.action-buttons', 'New Chapter')
310 ->visit($book->getUrl('/create-chapter'))
314 public function test_chapter_create_all_permissions()
316 $book = \BookStack\Book::take(1)->get()->first();
317 $this->checkAccessPermission('chapter-create-all', [
318 $book->getUrl('/create-chapter')
320 $book->getUrl() => 'New Chapter'
323 $this->visit($book->getUrl('/create-chapter'))
324 ->type('test chapter', 'name')
325 ->type('chapter desc', 'description')
326 ->press('Save Chapter')
327 ->seePageIs($book->getUrl('/chapter/test-chapter'));
330 public function test_chapter_edit_own_permission()
332 $otherChapter = \BookStack\Chapter::take(1)->get()->first();
333 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
334 $this->checkAccessPermission('chapter-update-own', [
335 $ownChapter->getUrl() . '/edit'
337 $ownChapter->getUrl() => 'Edit'
340 $this->visit($otherChapter->getUrl())
341 ->dontSeeInElement('.action-buttons', 'Edit')
342 ->visit($otherChapter->getUrl() . '/edit')
346 public function test_chapter_edit_all_permission()
348 $otherChapter = \BookStack\Chapter::take(1)->get()->first();
349 $this->checkAccessPermission('chapter-update-all', [
350 $otherChapter->getUrl() . '/edit'
352 $otherChapter->getUrl() => 'Edit'
356 public function test_chapter_delete_own_permission()
358 $this->giveUserPermissions($this->user, ['chapter-update-all']);
359 $otherChapter = \BookStack\Chapter::take(1)->get()->first();
360 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
361 $this->checkAccessPermission('chapter-delete-own', [
362 $ownChapter->getUrl() . '/delete'
364 $ownChapter->getUrl() => 'Delete'
367 $bookUrl = $ownChapter->book->getUrl();
368 $this->visit($otherChapter->getUrl())
369 ->dontSeeInElement('.action-buttons', 'Delete')
370 ->visit($otherChapter->getUrl() . '/delete')
372 $this->visit($ownChapter->getUrl())->visit($ownChapter->getUrl() . '/delete')
374 ->seePageIs($bookUrl)
375 ->dontSeeInElement('.book-content', $ownChapter->name);
378 public function test_chapter_delete_all_permission()
380 $this->giveUserPermissions($this->user, ['chapter-update-all']);
381 $otherChapter = \BookStack\Chapter::take(1)->get()->first();
382 $this->checkAccessPermission('chapter-delete-all', [
383 $otherChapter->getUrl() . '/delete'
385 $otherChapter->getUrl() => 'Delete'
388 $bookUrl = $otherChapter->book->getUrl();
389 $this->visit($otherChapter->getUrl())->visit($otherChapter->getUrl() . '/delete')
391 ->seePageIs($bookUrl)
392 ->dontSeeInElement('.book-content', $otherChapter->name);
395 public function test_page_create_own_permissions()
397 $book = \BookStack\Book::first();
398 $chapter = \BookStack\Chapter::first();
400 $entities = $this->createEntityChainBelongingToUser($this->user);
401 $ownBook = $entities['book'];
402 $ownChapter = $entities['chapter'];
404 $createUrl = $ownBook->getUrl('/create-page');
405 $createUrlChapter = $ownChapter->getUrl('/create-page');
406 $accessUrls = [$createUrl, $createUrlChapter];
408 foreach ($accessUrls as $url) {
409 $this->actingAs($this->user)->visit($url)
413 $this->checkAccessPermission('page-create-own', [], [
414 $ownBook->getUrl() => 'New Page',
415 $ownChapter->getUrl() => 'New Page'
418 $this->giveUserPermissions($this->user, ['page-create-own']);
420 foreach ($accessUrls as $index => $url) {
421 $this->actingAs($this->user)->visit($url);
422 $expectedUrl = \BookStack\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
423 $this->seePageIs($expectedUrl);
426 $this->visit($createUrl)
427 ->type('test page', 'name')
428 ->type('page desc', 'html')
430 ->seePageIs($ownBook->getUrl('/page/test-page'));
432 $this->visit($book->getUrl())
433 ->dontSeeInElement('.action-buttons', 'New Page')
434 ->visit($book->getUrl() . '/create-page')
436 $this->visit($chapter->getUrl())
437 ->dontSeeInElement('.action-buttons', 'New Page')
438 ->visit($chapter->getUrl() . '/create-page')
442 public function test_page_create_all_permissions()
444 $book = \BookStack\Book::take(1)->get()->first();
445 $chapter = \BookStack\Chapter::take(1)->get()->first();
446 $baseUrl = $book->getUrl() . '/page';
447 $createUrl = $book->getUrl('/create-page');
449 $createUrlChapter = $chapter->getUrl('/create-page');
450 $accessUrls = [$createUrl, $createUrlChapter];
452 foreach ($accessUrls as $url) {
453 $this->actingAs($this->user)->visit($url)
457 $this->checkAccessPermission('page-create-all', [], [
458 $book->getUrl() => 'New Page',
459 $chapter->getUrl() => 'New Page'
462 $this->giveUserPermissions($this->user, ['page-create-all']);
464 foreach ($accessUrls as $index => $url) {
465 $this->actingAs($this->user)->visit($url);
466 $expectedUrl = \BookStack\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
467 $this->seePageIs($expectedUrl);
470 $this->visit($createUrl)
471 ->type('test page', 'name')
472 ->type('page desc', 'html')
474 ->seePageIs($book->getUrl('/page/test-page'));
476 $this->visit($chapter->getUrl('/create-page'))
477 ->type('new test page', 'name')
478 ->type('page desc', 'html')
480 ->seePageIs($book->getUrl('/page/new-test-page'));
483 public function test_page_edit_own_permission()
485 $otherPage = \BookStack\Page::take(1)->get()->first();
486 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
487 $this->checkAccessPermission('page-update-own', [
488 $ownPage->getUrl() . '/edit'
490 $ownPage->getUrl() => 'Edit'
493 $this->visit($otherPage->getUrl())
494 ->dontSeeInElement('.action-buttons', 'Edit')
495 ->visit($otherPage->getUrl() . '/edit')
499 public function test_page_edit_all_permission()
501 $otherPage = \BookStack\Page::take(1)->get()->first();
502 $this->checkAccessPermission('page-update-all', [
503 $otherPage->getUrl() . '/edit'
505 $otherPage->getUrl() => 'Edit'
509 public function test_page_delete_own_permission()
511 $this->giveUserPermissions($this->user, ['page-update-all']);
512 $otherPage = \BookStack\Page::take(1)->get()->first();
513 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
514 $this->checkAccessPermission('page-delete-own', [
515 $ownPage->getUrl() . '/delete'
517 $ownPage->getUrl() => 'Delete'
520 $bookUrl = $ownPage->book->getUrl();
521 $this->visit($otherPage->getUrl())
522 ->dontSeeInElement('.action-buttons', 'Delete')
523 ->visit($otherPage->getUrl() . '/delete')
525 $this->visit($ownPage->getUrl())->visit($ownPage->getUrl() . '/delete')
527 ->seePageIs($bookUrl)
528 ->dontSeeInElement('.book-content', $ownPage->name);
531 public function test_page_delete_all_permission()
533 $this->giveUserPermissions($this->user, ['page-update-all']);
534 $otherPage = \BookStack\Page::take(1)->get()->first();
535 $this->checkAccessPermission('page-delete-all', [
536 $otherPage->getUrl() . '/delete'
538 $otherPage->getUrl() => 'Delete'
541 $bookUrl = $otherPage->book->getUrl();
542 $this->visit($otherPage->getUrl())->visit($otherPage->getUrl() . '/delete')
544 ->seePageIs($bookUrl)
545 ->dontSeeInElement('.book-content', $otherPage->name);
548 public function test_public_role_visible_in_user_edit_screen()
550 $user = \BookStack\User::first();
551 $this->asAdmin()->visit('/settings/users/' . $user->id)
552 ->seeElement('#roles-admin')
553 ->seeElement('#roles-public');
556 public function test_public_role_visible_in_role_listing()
558 $this->asAdmin()->visit('/settings/roles')
563 public function test_public_role_visible_in_default_role_setting()
565 $this->asAdmin()->visit('/settings')
566 ->seeElement('[data-role-name="admin"]')
567 ->seeElement('[data-role-name="public"]');
571 public function test_public_role_not_deleteable()
573 $this->asAdmin()->visit('/settings/roles')
576 ->click('Delete Role')
579 ->see('Cannot be deleted');
582 public function test_image_delete_own_permission()
584 $this->giveUserPermissions($this->user, ['image-update-all']);
585 $page = \BookStack\Page::first();
586 $image = factory(\BookStack\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $this->user->id, 'updated_by' => $this->user->id]);
588 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
589 ->seeStatusCode(403);
591 $this->giveUserPermissions($this->user, ['image-delete-own']);
593 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
595 ->dontSeeInDatabase('images', ['id' => $image->id]);
598 public function test_image_delete_all_permission()
600 $this->giveUserPermissions($this->user, ['image-update-all']);
601 $admin = $this->getAdmin();
602 $page = \BookStack\Page::first();
603 $image = factory(\BookStack\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
605 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
606 ->seeStatusCode(403);
608 $this->giveUserPermissions($this->user, ['image-delete-own']);
610 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
611 ->seeStatusCode(403);
613 $this->giveUserPermissions($this->user, ['image-delete-all']);
615 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
617 ->dontSeeInDatabase('images', ['id' => $image->id]);
620 public function test_role_permission_removal()
622 // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
623 $page = Page::first();
624 $viewerRole = \BookStack\Role::getRole('viewer');
625 $viewer = $this->getViewer();
626 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(200);
628 $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
629 'display_name' => $viewerRole->display_name,
630 'description' => $viewerRole->description,
632 ])->assertResponseStatus(302);
634 $this->expectException(HttpException::class);
635 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(404);
638 public function test_empty_state_actions_not_visible_without_permission()
640 $admin = $this->getAdmin();
642 $book = factory(\BookStack\Book::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
643 $this->updateEntityPermissions($book);
644 $this->actingAs($this->getViewer())->visit($book->getUrl())
645 ->dontSee('Create a new page')
646 ->dontSee('Add a chapter');
649 $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
650 $this->updateEntityPermissions($chapter);
651 $this->actingAs($this->getViewer())->visit($chapter->getUrl())
652 ->dontSee('Create a new page')
653 ->dontSee('Sort the current book');
656 public function test_comment_create_permission () {
657 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
659 $this->actingAs($this->user)->addComment($ownPage);
661 $this->assertResponseStatus(403);
663 $this->giveUserPermissions($this->user, ['comment-create-all']);
665 $this->actingAs($this->user)->addComment($ownPage);
666 $this->assertResponseStatus(200);
670 public function test_comment_update_own_permission () {
671 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
672 $this->giveUserPermissions($this->user, ['comment-create-all']);
673 $commentId = $this->actingAs($this->user)->addComment($ownPage);
675 // no comment-update-own
676 $this->actingAs($this->user)->updateComment($commentId);
677 $this->assertResponseStatus(403);
679 $this->giveUserPermissions($this->user, ['comment-update-own']);
681 // now has comment-update-own
682 $this->actingAs($this->user)->updateComment($commentId);
683 $this->assertResponseStatus(200);
686 public function test_comment_update_all_permission () {
687 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
688 $commentId = $this->asAdmin()->addComment($ownPage);
690 // no comment-update-all
691 $this->actingAs($this->user)->updateComment($commentId);
692 $this->assertResponseStatus(403);
694 $this->giveUserPermissions($this->user, ['comment-update-all']);
696 // now has comment-update-all
697 $this->actingAs($this->user)->updateComment($commentId);
698 $this->assertResponseStatus(200);
701 public function test_comment_delete_own_permission () {
702 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
703 $this->giveUserPermissions($this->user, ['comment-create-all']);
704 $commentId = $this->actingAs($this->user)->addComment($ownPage);
706 // no comment-delete-own
707 $this->actingAs($this->user)->deleteComment($commentId);
708 $this->assertResponseStatus(403);
710 $this->giveUserPermissions($this->user, ['comment-delete-own']);
712 // now has comment-update-own
713 $this->actingAs($this->user)->deleteComment($commentId);
714 $this->assertResponseStatus(200);
717 public function test_comment_delete_all_permission () {
718 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
719 $commentId = $this->asAdmin()->addComment($ownPage);
721 // no comment-delete-all
722 $this->actingAs($this->user)->deleteComment($commentId);
723 $this->assertResponseStatus(403);
725 $this->giveUserPermissions($this->user, ['comment-delete-all']);
727 // now has comment-delete-all
728 $this->actingAs($this->user)->deleteComment($commentId);
729 $this->assertResponseStatus(200);
732 private function addComment($page) {
733 $comment = factory(\BookStack\Comment::class)->make();
734 $url = "/ajax/page/$page->id/comment";
736 'text' => $comment->text,
737 'html' => $comment->html
740 $this->postJson($url, $request);
741 $comment = $page->comments()->first();
742 return $comment === null ? null : $comment->id;
745 private function updateComment($commentId) {
746 $comment = factory(\BookStack\Comment::class)->make();
747 $url = "/ajax/comment/$commentId";
749 'text' => $comment->text,
750 'html' => $comment->html
753 return $this->putJson($url, $request);
756 private function deleteComment($commentId) {
757 $url = '/ajax/comment/' . $commentId;
758 return $this->json('DELETE', $url);