Dan Brown

Refactored permission system components

Split joint permission creation into chunks

Fixes #374
......@@ -95,17 +95,6 @@ class Entity extends Ownable
}
/**
* Check if this entity has live (active) restrictions in place.
* @param $role_id
* @param $action
* @return bool
*/
public function hasActiveRestriction($role_id, $action)
{
return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
}
/**
* Get the entity jointPermissions this is connected to.
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
......@@ -176,5 +165,11 @@ class Entity extends Ownable
*/
public function entityRawQuery(){return '';}
/**
* Get the url of this entity
* @param $path
* @return string
*/
public function getUrl($path){return '/';}
}
......
<?php namespace BookStack\Http\Controllers;
use Activity;
use BookStack\Book;
use BookStack\Repos\EntityRepo;
use BookStack\Repos\UserRepo;
use BookStack\Services\ExportService;
......@@ -207,13 +208,12 @@ class BookController extends Controller
// Add activity for books
foreach ($sortedBooks as $bookId) {
/** @var Book $updatedBook */
$updatedBook = $this->entityRepo->getById('book', $bookId);
$this->entityRepo->buildJointPermissionsForBook($updatedBook);
Activity::add($updatedBook, 'book_sort', $updatedBook->id);
}
// Update permissions on changed models
if (count($updatedModels) === 0) $this->entityRepo->buildJointPermissions($updatedModels);
return redirect($book->getUrl());
}
......
......@@ -533,11 +533,11 @@ class EntityRepo
/**
* Alias method to update the book jointPermissions in the PermissionService.
* @param Collection $collection collection on entities
* @param Book $book
*/
public function buildJointPermissions(Collection $collection)
public function buildJointPermissionsForBook(Book $book)
{
$this->permissionService->buildJointPermissionsForEntities($collection);
$this->permissionService->buildJointPermissionsForEntity($book);
}
/**
......
......@@ -28,6 +28,12 @@ class DummyContentSeeder extends Seeder
$book->pages()->saveMany($pages);
});
$largeBook = factory(\BookStack\Book::class)->create(['name' => 'Large book' . str_random(10), 'created_by' => $user->id, 'updated_by' => $user->id]);
$pages = factory(\BookStack\Page::class, 200)->make(['created_by' => $user->id, 'updated_by' => $user->id]);
$chapters = factory(\BookStack\Chapter::class, 50)->make(['created_by' => $user->id, 'updated_by' => $user->id]);
$largeBook->pages()->saveMany($pages);
$largeBook->chapters()->saveMany($chapters);
app(\BookStack\Services\PermissionService::class)->buildJointPermissions();
app(\BookStack\Services\SearchService::class)->indexAllEntities();
}
......
......@@ -42,7 +42,7 @@ class EntitySearchTest extends TestCase
public function test_book_search()
{
$book = \BookStack\Book::all()->first();
$book = \BookStack\Book::first();
$page = $book->pages->last();
$chapter = $book->chapters->last();
......
<?php namespace Tests;
use BookStack\Role;
use BookStack\Tag;
use BookStack\Page;
use BookStack\Services\PermissionService;
......
......@@ -226,6 +226,7 @@ class RestrictionsTest extends BrowserKitTest
->type('test content', 'html')
->press('Save Page')
->seePageIs($chapter->book->getUrl() . '/page/test-page');
$this->visit($chapterUrl)->seeInElement('.action-buttons', 'New Page');
}
......