Refactored permission system components
Split joint permission creation into chunks Fixes #374
Showing
8 changed files
with
126 additions
and
105 deletions
| ... | @@ -95,17 +95,6 @@ class Entity extends Ownable | ... | @@ -95,17 +95,6 @@ class Entity extends Ownable |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | /** | 97 | /** |
| 98 | - * Check if this entity has live (active) restrictions in place. | ||
| 99 | - * @param $role_id | ||
| 100 | - * @param $action | ||
| 101 | - * @return bool | ||
| 102 | - */ | ||
| 103 | - public function hasActiveRestriction($role_id, $action) | ||
| 104 | - { | ||
| 105 | - return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action); | ||
| 106 | - } | ||
| 107 | - | ||
| 108 | - /** | ||
| 109 | * Get the entity jointPermissions this is connected to. | 98 | * Get the entity jointPermissions this is connected to. |
| 110 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany | 99 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
| 111 | */ | 100 | */ |
| ... | @@ -176,5 +165,11 @@ class Entity extends Ownable | ... | @@ -176,5 +165,11 @@ class Entity extends Ownable |
| 176 | */ | 165 | */ |
| 177 | public function entityRawQuery(){return '';} | 166 | public function entityRawQuery(){return '';} |
| 178 | 167 | ||
| 168 | + /** | ||
| 169 | + * Get the url of this entity | ||
| 170 | + * @param $path | ||
| 171 | + * @return string | ||
| 172 | + */ | ||
| 173 | + public function getUrl($path){return '/';} | ||
| 179 | 174 | ||
| 180 | } | 175 | } | ... | ... |
| 1 | <?php namespace BookStack\Http\Controllers; | 1 | <?php namespace BookStack\Http\Controllers; |
| 2 | 2 | ||
| 3 | use Activity; | 3 | use Activity; |
| 4 | +use BookStack\Book; | ||
| 4 | use BookStack\Repos\EntityRepo; | 5 | use BookStack\Repos\EntityRepo; |
| 5 | use BookStack\Repos\UserRepo; | 6 | use BookStack\Repos\UserRepo; |
| 6 | use BookStack\Services\ExportService; | 7 | use BookStack\Services\ExportService; |
| ... | @@ -207,13 +208,12 @@ class BookController extends Controller | ... | @@ -207,13 +208,12 @@ class BookController extends Controller |
| 207 | 208 | ||
| 208 | // Add activity for books | 209 | // Add activity for books |
| 209 | foreach ($sortedBooks as $bookId) { | 210 | foreach ($sortedBooks as $bookId) { |
| 211 | + /** @var Book $updatedBook */ | ||
| 210 | $updatedBook = $this->entityRepo->getById('book', $bookId); | 212 | $updatedBook = $this->entityRepo->getById('book', $bookId); |
| 213 | + $this->entityRepo->buildJointPermissionsForBook($updatedBook); | ||
| 211 | Activity::add($updatedBook, 'book_sort', $updatedBook->id); | 214 | Activity::add($updatedBook, 'book_sort', $updatedBook->id); |
| 212 | } | 215 | } |
| 213 | 216 | ||
| 214 | - // Update permissions on changed models | ||
| 215 | - if (count($updatedModels) === 0) $this->entityRepo->buildJointPermissions($updatedModels); | ||
| 216 | - | ||
| 217 | return redirect($book->getUrl()); | 217 | return redirect($book->getUrl()); |
| 218 | } | 218 | } |
| 219 | 219 | ... | ... |
| ... | @@ -533,11 +533,11 @@ class EntityRepo | ... | @@ -533,11 +533,11 @@ class EntityRepo |
| 533 | 533 | ||
| 534 | /** | 534 | /** |
| 535 | * Alias method to update the book jointPermissions in the PermissionService. | 535 | * Alias method to update the book jointPermissions in the PermissionService. |
| 536 | - * @param Collection $collection collection on entities | 536 | + * @param Book $book |
| 537 | */ | 537 | */ |
| 538 | - public function buildJointPermissions(Collection $collection) | 538 | + public function buildJointPermissionsForBook(Book $book) |
| 539 | { | 539 | { |
| 540 | - $this->permissionService->buildJointPermissionsForEntities($collection); | 540 | + $this->permissionService->buildJointPermissionsForEntity($book); |
| 541 | } | 541 | } |
| 542 | 542 | ||
| 543 | /** | 543 | /** | ... | ... |
| ... | @@ -3,6 +3,7 @@ | ... | @@ -3,6 +3,7 @@ |
| 3 | use BookStack\Book; | 3 | use BookStack\Book; |
| 4 | use BookStack\Chapter; | 4 | use BookStack\Chapter; |
| 5 | use BookStack\Entity; | 5 | use BookStack\Entity; |
| 6 | +use BookStack\EntityPermission; | ||
| 6 | use BookStack\JointPermission; | 7 | use BookStack\JointPermission; |
| 7 | use BookStack\Ownable; | 8 | use BookStack\Ownable; |
| 8 | use BookStack\Page; | 9 | use BookStack\Page; |
| ... | @@ -11,6 +12,7 @@ use BookStack\User; | ... | @@ -11,6 +12,7 @@ use BookStack\User; |
| 11 | use Illuminate\Database\Connection; | 12 | use Illuminate\Database\Connection; |
| 12 | use Illuminate\Database\Eloquent\Builder; | 13 | use Illuminate\Database\Eloquent\Builder; |
| 13 | use Illuminate\Support\Collection; | 14 | use Illuminate\Support\Collection; |
| 15 | +use Illuminate\Support\Facades\Log; | ||
| 14 | 16 | ||
| 15 | class PermissionService | 17 | class PermissionService |
| 16 | { | 18 | { |
| ... | @@ -28,22 +30,25 @@ class PermissionService | ... | @@ -28,22 +30,25 @@ class PermissionService |
| 28 | 30 | ||
| 29 | protected $jointPermission; | 31 | protected $jointPermission; |
| 30 | protected $role; | 32 | protected $role; |
| 33 | + protected $entityPermission; | ||
| 31 | 34 | ||
| 32 | protected $entityCache; | 35 | protected $entityCache; |
| 33 | 36 | ||
| 34 | /** | 37 | /** |
| 35 | * PermissionService constructor. | 38 | * PermissionService constructor. |
| 36 | * @param JointPermission $jointPermission | 39 | * @param JointPermission $jointPermission |
| 40 | + * @param EntityPermission $entityPermission | ||
| 37 | * @param Connection $db | 41 | * @param Connection $db |
| 38 | * @param Book $book | 42 | * @param Book $book |
| 39 | * @param Chapter $chapter | 43 | * @param Chapter $chapter |
| 40 | * @param Page $page | 44 | * @param Page $page |
| 41 | * @param Role $role | 45 | * @param Role $role |
| 42 | */ | 46 | */ |
| 43 | - public function __construct(JointPermission $jointPermission, Connection $db, Book $book, Chapter $chapter, Page $page, Role $role) | 47 | + public function __construct(JointPermission $jointPermission, EntityPermission $entityPermission, Connection $db, Book $book, Chapter $chapter, Page $page, Role $role) |
| 44 | { | 48 | { |
| 45 | $this->db = $db; | 49 | $this->db = $db; |
| 46 | $this->jointPermission = $jointPermission; | 50 | $this->jointPermission = $jointPermission; |
| 51 | + $this->entityPermission = $entityPermission; | ||
| 47 | $this->role = $role; | 52 | $this->role = $role; |
| 48 | $this->book = $book; | 53 | $this->book = $book; |
| 49 | $this->chapter = $chapter; | 54 | $this->chapter = $chapter; |
| ... | @@ -136,19 +141,31 @@ class PermissionService | ... | @@ -136,19 +141,31 @@ class PermissionService |
| 136 | $roles = $this->role->with('permissions')->get(); | 141 | $roles = $this->role->with('permissions')->get(); |
| 137 | 142 | ||
| 138 | // Chunk through all books | 143 | // Chunk through all books |
| 139 | - $this->book->with('permissions')->chunk(500, function ($books) use ($roles) { | 144 | + $this->book->newQuery()->with('chapters', 'pages')->chunk(5, function ($books) use ($roles) { |
| 140 | - $this->createManyJointPermissions($books, $roles); | 145 | + $this->buildJointPermissionsForBooks($books, $roles); |
| 141 | }); | 146 | }); |
| 147 | + } | ||
| 142 | 148 | ||
| 143 | - // Chunk through all chapters | 149 | + /** |
| 144 | - $this->chapter->with('book', 'permissions')->chunk(500, function ($chapters) use ($roles) { | 150 | + * Build joint permissions for an array of books |
| 145 | - $this->createManyJointPermissions($chapters, $roles); | 151 | + * @param Collection $books |
| 146 | - }); | 152 | + * @param Collection $roles |
| 153 | + * @param bool $deleteOld | ||
| 154 | + */ | ||
| 155 | + protected function buildJointPermissionsForBooks($books, $roles, $deleteOld = false) { | ||
| 156 | + $entities = clone $books; | ||
| 147 | 157 | ||
| 148 | - // Chunk through all pages | 158 | + foreach ($books as $book) { |
| 149 | - $this->page->with('book', 'chapter', 'permissions')->chunk(500, function ($pages) use ($roles) { | 159 | + foreach ($book->chapters as $chapter) { |
| 150 | - $this->createManyJointPermissions($pages, $roles); | 160 | + $entities->push($chapter); |
| 151 | - }); | 161 | + } |
| 162 | + foreach ($book->pages as $page) { | ||
| 163 | + $entities->push($page); | ||
| 164 | + } | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + if ($deleteOld) $this->deleteManyJointPermissionsForEntities($entities->all()); | ||
| 168 | + $this->createManyJointPermissions($entities, $roles); | ||
| 152 | } | 169 | } |
| 153 | 170 | ||
| 154 | /** | 171 | /** |
| ... | @@ -157,18 +174,9 @@ class PermissionService | ... | @@ -157,18 +174,9 @@ class PermissionService |
| 157 | */ | 174 | */ |
| 158 | public function buildJointPermissionsForEntity(Entity $entity) | 175 | public function buildJointPermissionsForEntity(Entity $entity) |
| 159 | { | 176 | { |
| 160 | - $roles = $this->role->get(); | 177 | + $roles = $this->role->newQuery()->get(); |
| 161 | - $entities = collect([$entity]); | 178 | + $book = ($entity->isA('book')) ? $entity : $entity->book; |
| 162 | - | 179 | + $this->buildJointPermissionsForBooks(collect([$book]), $roles, true); |
| 163 | - if ($entity->isA('book')) { | ||
| 164 | - $entities = $entities->merge($entity->chapters); | ||
| 165 | - $entities = $entities->merge($entity->pages); | ||
| 166 | - } elseif ($entity->isA('chapter')) { | ||
| 167 | - $entities = $entities->merge($entity->pages); | ||
| 168 | - } | ||
| 169 | - | ||
| 170 | - $this->deleteManyJointPermissionsForEntities($entities); | ||
| 171 | - $this->createManyJointPermissions($entities, $roles); | ||
| 172 | } | 180 | } |
| 173 | 181 | ||
| 174 | /** | 182 | /** |
| ... | @@ -178,7 +186,7 @@ class PermissionService | ... | @@ -178,7 +186,7 @@ class PermissionService |
| 178 | public function buildJointPermissionsForEntities(Collection $entities) | 186 | public function buildJointPermissionsForEntities(Collection $entities) |
| 179 | { | 187 | { |
| 180 | $roles = $this->role->get(); | 188 | $roles = $this->role->get(); |
| 181 | - $this->deleteManyJointPermissionsForEntities($entities); | 189 | + $this->deleteManyJointPermissionsForEntities($entities->all()); |
| 182 | $this->createManyJointPermissions($entities, $roles); | 190 | $this->createManyJointPermissions($entities, $roles); |
| 183 | } | 191 | } |
| 184 | 192 | ||
| ... | @@ -193,18 +201,8 @@ class PermissionService | ... | @@ -193,18 +201,8 @@ class PermissionService |
| 193 | $this->deleteManyJointPermissionsForRoles($roles); | 201 | $this->deleteManyJointPermissionsForRoles($roles); |
| 194 | 202 | ||
| 195 | // Chunk through all books | 203 | // Chunk through all books |
| 196 | - $this->book->with('permissions')->chunk(500, function ($books) use ($roles) { | 204 | + $this->book->with('chapters', 'pages')->chunk(5, function ($books) use ($roles) { |
| 197 | - $this->createManyJointPermissions($books, $roles); | 205 | + $this->buildJointPermissionsForBooks($books, $roles); |
| 198 | - }); | ||
| 199 | - | ||
| 200 | - // Chunk through all chapters | ||
| 201 | - $this->chapter->with('book', 'permissions')->chunk(500, function ($books) use ($roles) { | ||
| 202 | - $this->createManyJointPermissions($books, $roles); | ||
| 203 | - }); | ||
| 204 | - | ||
| 205 | - // Chunk through all pages | ||
| 206 | - $this->page->with('book', 'chapter', 'permissions')->chunk(500, function ($books) use ($roles) { | ||
| 207 | - $this->createManyJointPermissions($books, $roles); | ||
| 208 | }); | 206 | }); |
| 209 | } | 207 | } |
| 210 | 208 | ||
| ... | @@ -245,12 +243,14 @@ class PermissionService | ... | @@ -245,12 +243,14 @@ class PermissionService |
| 245 | { | 243 | { |
| 246 | if (count($entities) === 0) return; | 244 | if (count($entities) === 0) return; |
| 247 | $query = $this->jointPermission->newQuery(); | 245 | $query = $this->jointPermission->newQuery(); |
| 246 | + | ||
| 248 | foreach ($entities as $entity) { | 247 | foreach ($entities as $entity) { |
| 249 | $query->orWhere(function($query) use ($entity) { | 248 | $query->orWhere(function($query) use ($entity) { |
| 250 | $query->where('entity_id', '=', $entity->id) | 249 | $query->where('entity_id', '=', $entity->id) |
| 251 | ->where('entity_type', '=', $entity->getMorphClass()); | 250 | ->where('entity_type', '=', $entity->getMorphClass()); |
| 252 | }); | 251 | }); |
| 253 | } | 252 | } |
| 253 | + | ||
| 254 | $query->delete(); | 254 | $query->delete(); |
| 255 | } | 255 | } |
| 256 | 256 | ||
| ... | @@ -263,33 +263,58 @@ class PermissionService | ... | @@ -263,33 +263,58 @@ class PermissionService |
| 263 | { | 263 | { |
| 264 | $this->readyEntityCache(); | 264 | $this->readyEntityCache(); |
| 265 | $jointPermissions = []; | 265 | $jointPermissions = []; |
| 266 | + | ||
| 267 | + // Fetch Entity Permissions and create a mapping of entity restricted statuses | ||
| 268 | + $entityRestrictedMap = []; | ||
| 269 | + $permissionFetch = $this->entityPermission->newQuery(); | ||
| 270 | + foreach ($entities as $entity) { | ||
| 271 | + $entityRestrictedMap[$entity->getMorphClass() . ':' . $entity->id] = boolval($entity->getRawAttribute('restricted')); | ||
| 272 | + $permissionFetch->orWhere(function($query) use ($entity) { | ||
| 273 | + $query->where('restrictable_id', '=', $entity->id)->where('restrictable_type', '=', $entity->getMorphClass()); | ||
| 274 | + }); | ||
| 275 | + } | ||
| 276 | + $permissions = $permissionFetch->get(); | ||
| 277 | + | ||
| 278 | + // Create a mapping of explicit entity permissions | ||
| 279 | + $permissionMap = []; | ||
| 280 | + foreach ($permissions as $permission) { | ||
| 281 | + $key = $permission->restrictable_type . ':' . $permission->restrictable_id . ':' . $permission->role_id . ':' . $permission->action; | ||
| 282 | + $isRestricted = $entityRestrictedMap[$permission->restrictable_type . ':' . $permission->restrictable_id]; | ||
| 283 | + $permissionMap[$key] = $isRestricted; | ||
| 284 | + } | ||
| 285 | + | ||
| 286 | + // Create a mapping of role permissions | ||
| 287 | + $rolePermissionMap = []; | ||
| 288 | + foreach ($roles as $role) { | ||
| 289 | + foreach ($role->getRelationValue('permissions') as $permission) { | ||
| 290 | + $rolePermissionMap[$role->getRawAttribute('id') . ':' . $permission->getRawAttribute('name')] = true; | ||
| 291 | + } | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + // Create Joint Permission Data | ||
| 266 | foreach ($entities as $entity) { | 295 | foreach ($entities as $entity) { |
| 267 | foreach ($roles as $role) { | 296 | foreach ($roles as $role) { |
| 268 | foreach ($this->getActions($entity) as $action) { | 297 | foreach ($this->getActions($entity) as $action) { |
| 269 | - $jointPermissions[] = $this->createJointPermissionData($entity, $role, $action); | 298 | + $jointPermissions[] = $this->createJointPermissionData($entity, $role, $action, $permissionMap, $rolePermissionMap); |
| 299 | + } | ||
| 270 | } | 300 | } |
| 271 | } | 301 | } |
| 302 | + foreach (array_chunk($jointPermissions, 5000) as $jointPermissionChunk) { | ||
| 303 | + $this->jointPermission->insert($jointPermissionChunk); | ||
| 272 | } | 304 | } |
| 273 | - $this->jointPermission->insert($jointPermissions); | ||
| 274 | } | 305 | } |
| 275 | 306 | ||
| 276 | 307 | ||
| 277 | /** | 308 | /** |
| 278 | * Get the actions related to an entity. | 309 | * Get the actions related to an entity. |
| 279 | - * @param $entity | 310 | + * @param Entity $entity |
| 280 | * @return array | 311 | * @return array |
| 281 | */ | 312 | */ |
| 282 | - protected function getActions($entity) | 313 | + protected function getActions(Entity $entity) |
| 283 | { | 314 | { |
| 284 | $baseActions = ['view', 'update', 'delete']; | 315 | $baseActions = ['view', 'update', 'delete']; |
| 285 | - | 316 | + if ($entity->isA('chapter') || $entity->isA('book')) $baseActions[] = 'page-create'; |
| 286 | - if ($entity->isA('chapter')) { | 317 | + if ($entity->isA('book')) $baseActions[] = 'chapter-create'; |
| 287 | - $baseActions[] = 'page-create'; | ||
| 288 | - } else if ($entity->isA('book')) { | ||
| 289 | - $baseActions[] = 'page-create'; | ||
| 290 | - $baseActions[] = 'chapter-create'; | ||
| 291 | - } | ||
| 292 | - | ||
| 293 | return $baseActions; | 318 | return $baseActions; |
| 294 | } | 319 | } |
| 295 | 320 | ||
| ... | @@ -298,14 +323,16 @@ class PermissionService | ... | @@ -298,14 +323,16 @@ class PermissionService |
| 298 | * for a particular action. | 323 | * for a particular action. |
| 299 | * @param Entity $entity | 324 | * @param Entity $entity |
| 300 | * @param Role $role | 325 | * @param Role $role |
| 301 | - * @param $action | 326 | + * @param string $action |
| 327 | + * @param array $permissionMap | ||
| 328 | + * @param array $rolePermissionMap | ||
| 302 | * @return array | 329 | * @return array |
| 303 | */ | 330 | */ |
| 304 | - protected function createJointPermissionData(Entity $entity, Role $role, $action) | 331 | + protected function createJointPermissionData(Entity $entity, Role $role, $action, $permissionMap, $rolePermissionMap) |
| 305 | { | 332 | { |
| 306 | $permissionPrefix = (strpos($action, '-') === false ? ($entity->getType() . '-') : '') . $action; | 333 | $permissionPrefix = (strpos($action, '-') === false ? ($entity->getType() . '-') : '') . $action; |
| 307 | - $roleHasPermission = $role->hasPermission($permissionPrefix . '-all'); | 334 | + $roleHasPermission = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-all']); |
| 308 | - $roleHasPermissionOwn = $role->hasPermission($permissionPrefix . '-own'); | 335 | + $roleHasPermissionOwn = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-own']); |
| 309 | $explodedAction = explode('-', $action); | 336 | $explodedAction = explode('-', $action); |
| 310 | $restrictionAction = end($explodedAction); | 337 | $restrictionAction = end($explodedAction); |
| 311 | 338 | ||
| ... | @@ -313,54 +340,46 @@ class PermissionService | ... | @@ -313,54 +340,46 @@ class PermissionService |
| 313 | return $this->createJointPermissionDataArray($entity, $role, $action, true, true); | 340 | return $this->createJointPermissionDataArray($entity, $role, $action, true, true); |
| 314 | } | 341 | } |
| 315 | 342 | ||
| 316 | - if ($entity->isA('book')) { | 343 | + if ($entity->restricted) { |
| 317 | - | 344 | + $hasAccess = $this->mapHasActiveRestriction($permissionMap, $entity, $role, $restrictionAction); |
| 318 | - if (!$entity->restricted) { | ||
| 319 | - return $this->createJointPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn); | ||
| 320 | - } else { | ||
| 321 | - $hasAccess = $entity->hasActiveRestriction($role->id, $restrictionAction); | ||
| 322 | return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess); | 345 | return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess); |
| 323 | } | 346 | } |
| 324 | 347 | ||
| 325 | - } elseif ($entity->isA('chapter')) { | 348 | + if ($entity->isA('book')) { |
| 326 | - | 349 | + return $this->createJointPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn); |
| 327 | - if (!$entity->restricted) { | ||
| 328 | - $book = $this->getBook($entity->book_id); | ||
| 329 | - $hasExplicitAccessToBook = $book->hasActiveRestriction($role->id, $restrictionAction); | ||
| 330 | - $hasPermissiveAccessToBook = !$book->restricted; | ||
| 331 | - return $this->createJointPermissionDataArray($entity, $role, $action, | ||
| 332 | - ($hasExplicitAccessToBook || ($roleHasPermission && $hasPermissiveAccessToBook)), | ||
| 333 | - ($hasExplicitAccessToBook || ($roleHasPermissionOwn && $hasPermissiveAccessToBook))); | ||
| 334 | - } else { | ||
| 335 | - $hasAccess = $entity->hasActiveRestriction($role->id, $restrictionAction); | ||
| 336 | - return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess); | ||
| 337 | } | 350 | } |
| 338 | 351 | ||
| 339 | - } elseif ($entity->isA('page')) { | 352 | + // For chapters and pages, Check if explicit permissions are set on the Book. |
| 340 | - | ||
| 341 | - if (!$entity->restricted) { | ||
| 342 | $book = $this->getBook($entity->book_id); | 353 | $book = $this->getBook($entity->book_id); |
| 343 | - $hasExplicitAccessToBook = $book->hasActiveRestriction($role->id, $restrictionAction); | 354 | + $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $book, $role, $restrictionAction); |
| 344 | - $hasPermissiveAccessToBook = !$book->restricted; | 355 | + $hasPermissiveAccessToParents = !$book->restricted; |
| 345 | 356 | ||
| 357 | + // For pages with a chapter, Check if explicit permissions are set on the Chapter | ||
| 358 | + if ($entity->isA('page') && $entity->chapter_id !== 0) { | ||
| 346 | $chapter = $this->getChapter($entity->chapter_id); | 359 | $chapter = $this->getChapter($entity->chapter_id); |
| 347 | - $hasExplicitAccessToChapter = $chapter && $chapter->hasActiveRestriction($role->id, $restrictionAction); | 360 | + $hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapter->restricted; |
| 348 | - $hasPermissiveAccessToChapter = $chapter && !$chapter->restricted; | 361 | + if ($chapter->restricted) { |
| 349 | - $acknowledgeChapter = ($chapter && $chapter->restricted); | 362 | + $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $chapter, $role, $restrictionAction); |
| 350 | - | 363 | + } |
| 351 | - $hasExplicitAccessToParents = $acknowledgeChapter ? $hasExplicitAccessToChapter : $hasExplicitAccessToBook; | 364 | + } |
| 352 | - $hasPermissiveAccessToParents = $acknowledgeChapter ? $hasPermissiveAccessToChapter : $hasPermissiveAccessToBook; | ||
| 353 | 365 | ||
| 354 | return $this->createJointPermissionDataArray($entity, $role, $action, | 366 | return $this->createJointPermissionDataArray($entity, $role, $action, |
| 355 | ($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)), | 367 | ($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)), |
| 356 | ($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents)) | 368 | ($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents)) |
| 357 | ); | 369 | ); |
| 358 | - } else { | ||
| 359 | - $hasAccess = $entity->hasRestriction($role->id, $action); | ||
| 360 | - return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess); | ||
| 361 | } | 370 | } |
| 362 | 371 | ||
| 363 | - } | 372 | + /** |
| 373 | + * Check for an active restriction in an entity map. | ||
| 374 | + * @param $entityMap | ||
| 375 | + * @param Entity $entity | ||
| 376 | + * @param Role $role | ||
| 377 | + * @param $action | ||
| 378 | + * @return bool | ||
| 379 | + */ | ||
| 380 | + protected function mapHasActiveRestriction($entityMap, Entity $entity, Role $role, $action) { | ||
| 381 | + $key = $entity->getMorphClass() . ':' . $entity->getRawAttribute('id') . ':' . $role->getRawAttribute('id') . ':' . $action; | ||
| 382 | + return isset($entityMap[$key]) ? $entityMap[$key] : false; | ||
| 364 | } | 383 | } |
| 365 | 384 | ||
| 366 | /** | 385 | /** |
| ... | @@ -375,11 +394,10 @@ class PermissionService | ... | @@ -375,11 +394,10 @@ class PermissionService |
| 375 | */ | 394 | */ |
| 376 | protected function createJointPermissionDataArray(Entity $entity, Role $role, $action, $permissionAll, $permissionOwn) | 395 | protected function createJointPermissionDataArray(Entity $entity, Role $role, $action, $permissionAll, $permissionOwn) |
| 377 | { | 396 | { |
| 378 | - $entityClass = get_class($entity); | ||
| 379 | return [ | 397 | return [ |
| 380 | 'role_id' => $role->getRawAttribute('id'), | 398 | 'role_id' => $role->getRawAttribute('id'), |
| 381 | 'entity_id' => $entity->getRawAttribute('id'), | 399 | 'entity_id' => $entity->getRawAttribute('id'), |
| 382 | - 'entity_type' => $entityClass, | 400 | + 'entity_type' => $entity->getMorphClass(), |
| 383 | 'action' => $action, | 401 | 'action' => $action, |
| 384 | 'has_permission' => $permissionAll, | 402 | 'has_permission' => $permissionAll, |
| 385 | 'has_permission_own' => $permissionOwn, | 403 | 'has_permission_own' => $permissionOwn, | ... | ... |
| ... | @@ -28,6 +28,12 @@ class DummyContentSeeder extends Seeder | ... | @@ -28,6 +28,12 @@ class DummyContentSeeder extends Seeder |
| 28 | $book->pages()->saveMany($pages); | 28 | $book->pages()->saveMany($pages); |
| 29 | }); | 29 | }); |
| 30 | 30 | ||
| 31 | + $largeBook = factory(\BookStack\Book::class)->create(['name' => 'Large book' . str_random(10), 'created_by' => $user->id, 'updated_by' => $user->id]); | ||
| 32 | + $pages = factory(\BookStack\Page::class, 200)->make(['created_by' => $user->id, 'updated_by' => $user->id]); | ||
| 33 | + $chapters = factory(\BookStack\Chapter::class, 50)->make(['created_by' => $user->id, 'updated_by' => $user->id]); | ||
| 34 | + $largeBook->pages()->saveMany($pages); | ||
| 35 | + $largeBook->chapters()->saveMany($chapters); | ||
| 36 | + | ||
| 31 | app(\BookStack\Services\PermissionService::class)->buildJointPermissions(); | 37 | app(\BookStack\Services\PermissionService::class)->buildJointPermissions(); |
| 32 | app(\BookStack\Services\SearchService::class)->indexAllEntities(); | 38 | app(\BookStack\Services\SearchService::class)->indexAllEntities(); |
| 33 | } | 39 | } | ... | ... |
| ... | @@ -42,7 +42,7 @@ class EntitySearchTest extends TestCase | ... | @@ -42,7 +42,7 @@ class EntitySearchTest extends TestCase |
| 42 | 42 | ||
| 43 | public function test_book_search() | 43 | public function test_book_search() |
| 44 | { | 44 | { |
| 45 | - $book = \BookStack\Book::all()->first(); | 45 | + $book = \BookStack\Book::first(); |
| 46 | $page = $book->pages->last(); | 46 | $page = $book->pages->last(); |
| 47 | $chapter = $book->chapters->last(); | 47 | $chapter = $book->chapters->last(); |
| 48 | 48 | ... | ... |
| ... | @@ -226,6 +226,7 @@ class RestrictionsTest extends BrowserKitTest | ... | @@ -226,6 +226,7 @@ class RestrictionsTest extends BrowserKitTest |
| 226 | ->type('test content', 'html') | 226 | ->type('test content', 'html') |
| 227 | ->press('Save Page') | 227 | ->press('Save Page') |
| 228 | ->seePageIs($chapter->book->getUrl() . '/page/test-page'); | 228 | ->seePageIs($chapter->book->getUrl() . '/page/test-page'); |
| 229 | + | ||
| 229 | $this->visit($chapterUrl)->seeInElement('.action-buttons', 'New Page'); | 230 | $this->visit($chapterUrl)->seeInElement('.action-buttons', 'New Page'); |
| 230 | } | 231 | } |
| 231 | 232 | ... | ... |
-
Please register or sign in to post a comment