PermissionService.php 25.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
<?php namespace BookStack\Services;

use BookStack\Book;
use BookStack\Chapter;
use BookStack\Entity;
use BookStack\EntityPermission;
use BookStack\JointPermission;
use BookStack\Ownable;
use BookStack\Page;
use BookStack\Role;
use BookStack\User;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Collection;

class PermissionService
{

    protected $currentAction;
    protected $isAdminUser;
    protected $userRoles = false;
    protected $currentUserModel = false;

    public $book;
    public $chapter;
    public $page;

    protected $db;

    protected $jointPermission;
    protected $role;
    protected $entityPermission;

    protected $entityCache;

    /**
     * PermissionService constructor.
     * @param JointPermission $jointPermission
     * @param EntityPermission $entityPermission
     * @param Connection $db
     * @param Book $book
     * @param Chapter $chapter
     * @param Page $page
     * @param Role $role
     */
    public function __construct(JointPermission $jointPermission, EntityPermission $entityPermission, Connection $db, Book $book, Chapter $chapter, Page $page, Role $role)
    {
        $this->db = $db;
        $this->jointPermission = $jointPermission;
        $this->entityPermission = $entityPermission;
        $this->role = $role;
        $this->book = $book;
        $this->chapter = $chapter;
        $this->page = $page;
        // TODO - Update so admin still goes through filters
    }

    /**
     * Set the database connection
     * @param Connection $connection
     */
    public function setConnection(Connection $connection)
    {
        $this->db = $connection;
    }

    /**
     * Prepare the local entity cache and ensure it's empty
     */
    protected function readyEntityCache()
    {
        $this->entityCache = [
            'books' => collect(),
            'chapters' => collect()
        ];
    }

    /**
     * Get a book via ID, Checks local cache
     * @param $bookId
     * @return Book
     */
    protected function getBook($bookId)
    {
        if (isset($this->entityCache['books']) && $this->entityCache['books']->has($bookId)) {
            return $this->entityCache['books']->get($bookId);
        }

        $book = $this->book->find($bookId);
        if ($book === null) $book = false;
        if (isset($this->entityCache['books'])) {
            $this->entityCache['books']->put($bookId, $book);
        }

        return $book;
    }

    /**
     * Get a chapter via ID, Checks local cache
     * @param $chapterId
     * @return Book
     */
    protected function getChapter($chapterId)
    {
        if (isset($this->entityCache['chapters']) && $this->entityCache['chapters']->has($chapterId)) {
            return $this->entityCache['chapters']->get($chapterId);
        }

        $chapter = $this->chapter->find($chapterId);
        if ($chapter === null) $chapter = false;
        if (isset($this->entityCache['chapters'])) {
            $this->entityCache['chapters']->put($chapterId, $chapter);
        }

        return $chapter;
    }

    /**
     * Get the roles for the current user;
     * @return array|bool
     */
    protected function getRoles()
    {
        if ($this->userRoles !== false) return $this->userRoles;

        $roles = [];

        if (auth()->guest()) {
            $roles[] = $this->role->getSystemRole('public')->id;
            return $roles;
        }


        foreach ($this->currentUser()->roles as $role) {
            $roles[] = $role->id;
        }
        return $roles;
    }

    /**
     * Re-generate all entity permission from scratch.
     */
    public function buildJointPermissions()
    {
        $this->jointPermission->truncate();
        $this->readyEntityCache();

        // Get all roles (Should be the most limited dimension)
        $roles = $this->role->with('permissions')->get()->all();

        // Chunk through all books
        $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
            $query->select(['id', 'restricted', 'created_by', 'book_id']);
        }, 'pages'  => function($query) {
            $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
        }])->chunk(5, function ($books) use ($roles) {
            $this->buildJointPermissionsForBooks($books, $roles);
        });
    }

    /**
     * Build joint permissions for an array of books
     * @param Collection $books
     * @param array $roles
     * @param bool $deleteOld
     */
    protected function buildJointPermissionsForBooks($books, $roles, $deleteOld = false) {
        $entities = clone $books;

        /** @var Book $book */
        foreach ($books->all() as $book) {
            foreach ($book->getRelation('chapters') as $chapter) {
                $entities->push($chapter);
            }
            foreach ($book->getRelation('pages') as $page) {
                $entities->push($page);
            }
        }

        if ($deleteOld) $this->deleteManyJointPermissionsForEntities($entities->all());
        $this->createManyJointPermissions($entities, $roles);
    }

    /**
     * Rebuild the entity jointPermissions for a particular entity.
     * @param Entity $entity
     */
    public function buildJointPermissionsForEntity(Entity $entity)
    {
        $roles = $this->role->newQuery()->get();
        $book = ($entity->isA('book')) ? $entity : $entity->book;
        $book = $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
            $query->select(['id', 'restricted', 'created_by', 'book_id']);
        }, 'pages'  => function($query) {
            $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
        }])->where('id', '=', $book->id)->get();
        $this->buildJointPermissionsForBooks($book, $roles, true);
    }

    /**
     * Rebuild the entity jointPermissions for a collection of entities.
     * @param Collection $entities
     */
    public function buildJointPermissionsForEntities(Collection $entities)
    {
        $roles = $this->role->get();
        $this->deleteManyJointPermissionsForEntities($entities->all());
        $this->createManyJointPermissions($entities, $roles);
    }

    /**
     * Build the entity jointPermissions for a particular role.
     * @param Role $role
     */
    public function buildJointPermissionForRole(Role $role)
    {
        $roles = [$role];
        $this->deleteManyJointPermissionsForRoles($roles);

        // Chunk through all books
        $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
            $query->select(['id', 'restricted', 'created_by', 'book_id']);
        }, 'pages'  => function($query) {
            $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
        }])->chunk(5, function ($books) use ($roles) {
            $this->buildJointPermissionsForBooks($books, $roles);
        });
    }

    /**
     * Delete the entity jointPermissions attached to a particular role.
     * @param Role $role
     */
    public function deleteJointPermissionsForRole(Role $role)
    {
        $this->deleteManyJointPermissionsForRoles([$role]);
    }

    /**
     * Delete all of the entity jointPermissions for a list of entities.
     * @param Role[] $roles
     */
    protected function deleteManyJointPermissionsForRoles($roles)
    {
        $roleIds = array_map(function($role) {
            return $role->id;
        }, $roles);
        $this->jointPermission->newQuery()->whereIn('id', $roleIds)->delete();
    }

    /**
     * Delete the entity jointPermissions for a particular entity.
     * @param Entity $entity
     */
    public function deleteJointPermissionsForEntity(Entity $entity)
    {
        $this->deleteManyJointPermissionsForEntities([$entity]);
    }

    /**
     * Delete all of the entity jointPermissions for a list of entities.
     * @param Entity[] $entities
     */
    protected function deleteManyJointPermissionsForEntities($entities)
    {
        if (count($entities) === 0) return;

        $this->db->transaction(function() use ($entities) {

            foreach (array_chunk($entities, 1000) as $entityChunk) {
                $query = $this->db->table('joint_permissions');
                foreach ($entityChunk as $entity) {
                    $query->orWhere(function(QueryBuilder $query) use ($entity) {
                        $query->where('entity_id', '=', $entity->id)
                            ->where('entity_type', '=', $entity->getMorphClass());
                    });
                }
                $query->delete();
            }

        });
    }

    /**
     * Create & Save entity jointPermissions for many entities and jointPermissions.
     * @param Collection $entities
     * @param array $roles
     */
    protected function createManyJointPermissions($entities, $roles)
    {
        $this->readyEntityCache();
        $jointPermissions = [];

        // Fetch Entity Permissions and create a mapping of entity restricted statuses
        $entityRestrictedMap = [];
        $permissionFetch = $this->entityPermission->newQuery();
        foreach ($entities as $entity) {
            $entityRestrictedMap[$entity->getMorphClass() . ':' . $entity->id] = boolval($entity->getRawAttribute('restricted'));
            $permissionFetch->orWhere(function($query) use ($entity) {
                $query->where('restrictable_id', '=', $entity->id)->where('restrictable_type', '=', $entity->getMorphClass());
            });
        }
        $permissions = $permissionFetch->get();

        // Create a mapping of explicit entity permissions
        $permissionMap = [];
        foreach ($permissions as $permission) {
            $key = $permission->restrictable_type . ':' . $permission->restrictable_id . ':' . $permission->role_id . ':' . $permission->action;
            $isRestricted = $entityRestrictedMap[$permission->restrictable_type . ':' . $permission->restrictable_id];
            $permissionMap[$key] = $isRestricted;
        }

        // Create a mapping of role permissions
        $rolePermissionMap = [];
        foreach ($roles as $role) {
            foreach ($role->getRelationValue('permissions') as $permission) {
                $rolePermissionMap[$role->getRawAttribute('id') . ':' . $permission->getRawAttribute('name')] = true;
            }
        }

        // Create Joint Permission Data
        foreach ($entities as $entity) {
            foreach ($roles as $role) {
                foreach ($this->getActions($entity) as $action) {
                    $jointPermissions[] = $this->createJointPermissionData($entity, $role, $action, $permissionMap, $rolePermissionMap);
                }
            }
        }

        $this->db->transaction(function() use ($jointPermissions) {
            foreach (array_chunk($jointPermissions, 1000) as $jointPermissionChunk) {
                $this->db->table('joint_permissions')->insert($jointPermissionChunk);
            }
        });
    }


    /**
     * Get the actions related to an entity.
     * @param Entity $entity
     * @return array
     */
    protected function getActions(Entity $entity)
    {
        $baseActions = ['view', 'update', 'delete'];
        if ($entity->isA('chapter') || $entity->isA('book')) $baseActions[] = 'page-create';
        if ($entity->isA('book')) $baseActions[] = 'chapter-create';
        return $baseActions;
    }

    /**
     * Create entity permission data for an entity and role
     * for a particular action.
     * @param Entity $entity
     * @param Role $role
     * @param string $action
     * @param array $permissionMap
     * @param array $rolePermissionMap
     * @return array
     */
    protected function createJointPermissionData(Entity $entity, Role $role, $action, $permissionMap, $rolePermissionMap)
    {
        $permissionPrefix = (strpos($action, '-') === false ? ($entity->getType() . '-') : '') . $action;
        $roleHasPermission = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-all']);
        $roleHasPermissionOwn = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-own']);
        $explodedAction = explode('-', $action);
        $restrictionAction = end($explodedAction);

        if ($role->system_name === 'admin') {
            return $this->createJointPermissionDataArray($entity, $role, $action, true, true);
        }

        if ($entity->restricted) {
            $hasAccess = $this->mapHasActiveRestriction($permissionMap, $entity, $role, $restrictionAction);
            return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
        }

        if ($entity->isA('book')) {
            return $this->createJointPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn);
        }

        // For chapters and pages, Check if explicit permissions are set on the Book.
        $book = $this->getBook($entity->book_id);
        $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $book, $role, $restrictionAction);
        $hasPermissiveAccessToParents = !$book->restricted;

        // For pages with a chapter, Check if explicit permissions are set on the Chapter
        if ($entity->isA('page') && $entity->chapter_id !== 0) {
            $chapter = $this->getChapter($entity->chapter_id);
            $hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapter->restricted;
            if ($chapter->restricted) {
                $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $chapter, $role, $restrictionAction);
            }
        }

        return $this->createJointPermissionDataArray($entity, $role, $action,
            ($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)),
            ($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents))
        );
    }

    /**
     * Check for an active restriction in an entity map.
     * @param $entityMap
     * @param Entity $entity
     * @param Role $role
     * @param $action
     * @return bool
     */
    protected function mapHasActiveRestriction($entityMap, Entity $entity, Role $role, $action) {
        $key = $entity->getMorphClass() . ':' . $entity->getRawAttribute('id') . ':' . $role->getRawAttribute('id') . ':' . $action;
        return isset($entityMap[$key]) ? $entityMap[$key] : false;
    }

    /**
     * Create an array of data with the information of an entity jointPermissions.
     * Used to build data for bulk insertion.
     * @param Entity $entity
     * @param Role $role
     * @param $action
     * @param $permissionAll
     * @param $permissionOwn
     * @return array
     */
    protected function createJointPermissionDataArray(Entity $entity, Role $role, $action, $permissionAll, $permissionOwn)
    {
        return [
            'role_id'            => $role->getRawAttribute('id'),
            'entity_id'          => $entity->getRawAttribute('id'),
            'entity_type'        => $entity->getMorphClass(),
            'action'             => $action,
            'has_permission'     => $permissionAll,
            'has_permission_own' => $permissionOwn,
            'created_by'         => $entity->getRawAttribute('created_by')
        ];
    }

    /**
     * Checks if an entity has a restriction set upon it.
     * @param Ownable $ownable
     * @param $permission
     * @return bool
     */
    public function checkOwnableUserAccess(Ownable $ownable, $permission)
    {
        if ($this->isAdmin()) {
            $this->clean();
            return true;
        }

        $explodedPermission = explode('-', $permission);

        $baseQuery = $ownable->where('id', '=', $ownable->id);
        $action = end($explodedPermission);
        $this->currentAction = $action;

        $nonJointPermissions = ['restrictions', 'image', 'attachment'];

        // Handle non entity specific jointPermissions
        if (in_array($explodedPermission[0], $nonJointPermissions)) {
            $allPermission = $this->currentUser() && $this->currentUser()->can($permission . '-all');
            $ownPermission = $this->currentUser() && $this->currentUser()->can($permission . '-own');
            $this->currentAction = 'view';
            $isOwner = $this->currentUser() && $this->currentUser()->id === $ownable->created_by;
            return ($allPermission || ($isOwner && $ownPermission));
        }

        // Handle abnormal create jointPermissions
        if ($action === 'create') {
            $this->currentAction = $permission;
        }

        $q = $this->entityRestrictionQuery($baseQuery)->count() > 0;
        $this->clean();
        return $q;
    }

    /**
     * Check if an entity has restrictions set on itself or its
     * parent tree.
     * @param Entity $entity
     * @param $action
     * @return bool|mixed
     */
    public function checkIfRestrictionsSet(Entity $entity, $action)
    {
        $this->currentAction = $action;
        if ($entity->isA('page')) {
            return $entity->restricted || ($entity->chapter && $entity->chapter->restricted) || $entity->book->restricted;
        } elseif ($entity->isA('chapter')) {
            return $entity->restricted || $entity->book->restricted;
        } elseif ($entity->isA('book')) {
            return $entity->restricted;
        }
    }

    /**
     * The general query filter to remove all entities
     * that the current user does not have access to.
     * @param $query
     * @return mixed
     */
    protected function entityRestrictionQuery($query)
    {
        $q = $query->where(function ($parentQuery) {
            $parentQuery->whereHas('jointPermissions', function ($permissionQuery) {
                $permissionQuery->whereIn('role_id', $this->getRoles())
                    ->where('action', '=', $this->currentAction)
                    ->where(function ($query) {
                        $query->where('has_permission', '=', true)
                            ->orWhere(function ($query) {
                                $query->where('has_permission_own', '=', true)
                                    ->where('created_by', '=', $this->currentUser()->id);
                            });
                    });
            });
        });
        $this->clean();
        return $q;
    }

    /**
     * Get the children of a book in an efficient single query, Filtered by the permission system.
     * @param integer $book_id
     * @param bool $filterDrafts
     * @param bool $fetchPageContent
     * @return QueryBuilder
     */
    public function bookChildrenQuery($book_id, $filterDrafts = false, $fetchPageContent = false) {
        $pageSelect = $this->db->table('pages')->selectRaw($this->page->entityRawQuery($fetchPageContent))->where('book_id', '=', $book_id)->where(function($query) use ($filterDrafts) {
            $query->where('draft', '=', 0);
            if (!$filterDrafts) {
                $query->orWhere(function($query) {
                    $query->where('draft', '=', 1)->where('created_by', '=', $this->currentUser()->id);
                });
            }
        });
        $chapterSelect = $this->db->table('chapters')->selectRaw($this->chapter->entityRawQuery())->where('book_id', '=', $book_id);
        $query = $this->db->query()->select('*')->from($this->db->raw("({$pageSelect->toSql()} UNION {$chapterSelect->toSql()}) AS U"))
            ->mergeBindings($pageSelect)->mergeBindings($chapterSelect);

        if (!$this->isAdmin()) {
            $whereQuery = $this->db->table('joint_permissions as jp')->selectRaw('COUNT(*)')
                ->whereRaw('jp.entity_id=U.id')->whereRaw('jp.entity_type=U.entity_type')
                ->where('jp.action', '=', 'view')->whereIn('jp.role_id', $this->getRoles())
                ->where(function($query) {
                    $query->where('jp.has_permission', '=', 1)->orWhere(function($query) {
                        $query->where('jp.has_permission_own', '=', 1)->where('jp.created_by', '=', $this->currentUser()->id);
                    });
                });
            $query->whereRaw("({$whereQuery->toSql()}) > 0")->mergeBindings($whereQuery);
        }

        $query->orderBy('draft', 'desc')->orderBy('priority', 'asc');
        $this->clean();
        return  $query;
    }

    /**
     * Add restrictions for a generic entity
     * @param string $entityType
     * @param Builder|Entity $query
     * @param string $action
     * @return Builder
     */
    public function enforceEntityRestrictions($entityType, $query, $action = 'view')
    {
        if (strtolower($entityType) === 'page') {
            // Prevent drafts being visible to others.
            $query = $query->where(function ($query) {
                $query->where('draft', '=', false);
                if ($this->currentUser()) {
                    $query->orWhere(function ($query) {
                        $query->where('draft', '=', true)->where('created_by', '=', $this->currentUser()->id);
                    });
                }
            });
        }

        if ($this->isAdmin()) {
            $this->clean();
            return $query;
        }

        $this->currentAction = $action;
        return $this->entityRestrictionQuery($query);
    }

    /**
     * Filter items that have entities set as a polymorphic relation.
     * @param $query
     * @param string $tableName
     * @param string $entityIdColumn
     * @param string $entityTypeColumn
     * @return mixed
     */
    public function filterRestrictedEntityRelations($query, $tableName, $entityIdColumn, $entityTypeColumn)
    {
        if ($this->isAdmin()) {
            $this->clean();
            return $query;
        }

        $this->currentAction = 'view';
        $tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn, 'entityTypeColumn' => $entityTypeColumn];

        $q = $query->where(function ($query) use ($tableDetails) {
            $query->whereExists(function ($permissionQuery) use (&$tableDetails) {
                $permissionQuery->select('id')->from('joint_permissions')
                    ->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
                    ->whereRaw('joint_permissions.entity_type=' . $tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'])
                    ->where('action', '=', $this->currentAction)
                    ->whereIn('role_id', $this->getRoles())
                    ->where(function ($query) {
                        $query->where('has_permission', '=', true)->orWhere(function ($query) {
                            $query->where('has_permission_own', '=', true)
                                ->where('created_by', '=', $this->currentUser()->id);
                        });
                    });
            });
        });
        $this->clean();
        return $q;
    }

    /**
     * Filters pages that are a direct relation to another item.
     * @param $query
     * @param $tableName
     * @param $entityIdColumn
     * @return mixed
     */
    public function filterRelatedPages($query, $tableName, $entityIdColumn)
    {
        if ($this->isAdmin()) {
            $this->clean();
            return $query;
        }

        $this->currentAction = 'view';
        $tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn];

        $q = $query->where(function ($query) use ($tableDetails) {
            $query->where(function ($query) use (&$tableDetails) {
                $query->whereExists(function ($permissionQuery) use (&$tableDetails) {
                    $permissionQuery->select('id')->from('joint_permissions')
                        ->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
                        ->where('entity_type', '=', 'Bookstack\\Page')
                        ->where('action', '=', $this->currentAction)
                        ->whereIn('role_id', $this->getRoles())
                        ->where(function ($query) {
                            $query->where('has_permission', '=', true)->orWhere(function ($query) {
                                $query->where('has_permission_own', '=', true)
                                    ->where('created_by', '=', $this->currentUser()->id);
                            });
                        });
                });
            })->orWhere($tableDetails['entityIdColumn'], '=', 0);
        });
        $this->clean();
        return $q;
    }

    /**
     * Check if the current user is an admin.
     * @return bool
     */
    private function isAdmin()
    {
        if ($this->isAdminUser === null) {
            $this->isAdminUser = ($this->currentUser()->id !== null) ? $this->currentUser()->hasSystemRole('admin') : false;
        }

        return $this->isAdminUser;
    }

    /**
     * Get the current user
     * @return User
     */
    private function currentUser()
    {
        if ($this->currentUserModel === false) {
            $this->currentUserModel = user();
        }

        return $this->currentUserModel;
    }

    /**
     * Clean the cached user elements.
     */
    private function clean()
    {
        $this->currentUserModel = false;
        $this->userRoles = false;
        $this->isAdminUser = null;
    }

}