2016-02-28 14:03:04 -05:00
|
|
|
<?php namespace BookStack\Services;
|
|
|
|
|
2016-04-20 16:37:57 -04:00
|
|
|
use BookStack\Book;
|
|
|
|
use BookStack\Chapter;
|
2016-02-29 15:31:21 -05:00
|
|
|
use BookStack\Entity;
|
2016-04-20 16:37:57 -04:00
|
|
|
use BookStack\EntityPermission;
|
|
|
|
use BookStack\Page;
|
|
|
|
use BookStack\Role;
|
2016-04-26 16:48:17 -04:00
|
|
|
use BookStack\User;
|
2016-04-20 16:37:57 -04:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2016-02-29 15:31:21 -05:00
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
class RestrictionService
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $userRoles;
|
|
|
|
protected $isAdmin;
|
|
|
|
protected $currentAction;
|
2016-03-13 08:04:08 -04:00
|
|
|
protected $currentUser;
|
2016-02-28 14:03:04 -05:00
|
|
|
|
2016-04-20 16:37:57 -04:00
|
|
|
public $book;
|
|
|
|
public $chapter;
|
|
|
|
public $page;
|
|
|
|
|
|
|
|
protected $entityPermission;
|
|
|
|
protected $role;
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
/**
|
|
|
|
* RestrictionService constructor.
|
2016-04-20 16:37:57 -04:00
|
|
|
* @param EntityPermission $entityPermission
|
|
|
|
* @param Book $book
|
|
|
|
* @param Chapter $chapter
|
|
|
|
* @param Page $page
|
|
|
|
* @param Role $role
|
2016-02-28 14:03:04 -05:00
|
|
|
*/
|
2016-04-23 13:14:26 -04:00
|
|
|
public function __construct(EntityPermission $entityPermission, Book $book, Chapter $chapter, Page $page, Role $role)
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
2016-03-13 08:04:08 -04:00
|
|
|
$this->currentUser = auth()->user();
|
2016-05-01 14:36:53 -04:00
|
|
|
$userSet = $this->currentUser !== null;
|
|
|
|
$this->userRoles = false;
|
|
|
|
$this->isAdmin = $userSet ? $this->currentUser->hasRole('admin') : false;
|
|
|
|
if (!$userSet) $this->currentUser = new User();
|
2016-04-20 16:37:57 -04:00
|
|
|
|
|
|
|
$this->entityPermission = $entityPermission;
|
|
|
|
$this->role = $role;
|
|
|
|
$this->book = $book;
|
|
|
|
$this->chapter = $chapter;
|
|
|
|
$this->page = $page;
|
|
|
|
}
|
|
|
|
|
2016-05-01 14:36:53 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:37:57 -04:00
|
|
|
/**
|
|
|
|
* Re-generate all entity permission from scratch.
|
|
|
|
*/
|
|
|
|
public function buildEntityPermissions()
|
|
|
|
{
|
|
|
|
$this->entityPermission->truncate();
|
|
|
|
|
|
|
|
// Get all roles (Should be the most limited dimension)
|
2016-04-30 12:16:06 -04:00
|
|
|
$roles = $this->role->with('permissions')->get();
|
2016-04-20 16:37:57 -04:00
|
|
|
|
|
|
|
// Chunk through all books
|
2016-04-30 12:16:06 -04:00
|
|
|
$this->book->with('restrictions')->chunk(500, function ($books) use ($roles) {
|
2016-04-20 16:37:57 -04:00
|
|
|
$this->createManyEntityPermissions($books, $roles);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Chunk through all chapters
|
2016-04-30 12:16:06 -04:00
|
|
|
$this->chapter->with('book', 'restrictions')->chunk(500, function ($chapters) use ($roles) {
|
|
|
|
$this->createManyEntityPermissions($chapters, $roles);
|
2016-04-20 16:37:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Chunk through all pages
|
2016-04-30 12:16:06 -04:00
|
|
|
$this->page->with('book', 'chapter', 'restrictions')->chunk(500, function ($pages) use ($roles) {
|
|
|
|
$this->createManyEntityPermissions($pages, $roles);
|
2016-04-20 16:37:57 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
/**
|
|
|
|
* Create the entity permissions for a particular entity.
|
|
|
|
* @param Entity $entity
|
|
|
|
*/
|
|
|
|
public function buildEntityPermissionsForEntity(Entity $entity)
|
|
|
|
{
|
2016-04-30 12:16:06 -04:00
|
|
|
$roles = $this->role->with('permissions')->get();
|
2016-04-24 11:54:20 -04:00
|
|
|
$entities = collect([$entity]);
|
|
|
|
|
|
|
|
if ($entity->isA('book')) {
|
|
|
|
$entities = $entities->merge($entity->chapters);
|
|
|
|
$entities = $entities->merge($entity->pages);
|
|
|
|
} elseif ($entity->isA('chapter')) {
|
|
|
|
$entities = $entities->merge($entity->pages);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->deleteManyEntityPermissionsForEntities($entities);
|
|
|
|
$this->createManyEntityPermissions($entities, $roles);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the entity permissions for a particular role.
|
|
|
|
* @param Role $role
|
|
|
|
*/
|
|
|
|
public function buildEntityPermissionForRole(Role $role)
|
|
|
|
{
|
|
|
|
$roles = collect([$role]);
|
|
|
|
|
|
|
|
$this->deleteManyEntityPermissionsForRoles($roles);
|
|
|
|
|
|
|
|
// Chunk through all books
|
2016-04-30 12:16:06 -04:00
|
|
|
$this->book->with('restrictions')->chunk(500, function ($books) use ($roles) {
|
2016-04-24 11:54:20 -04:00
|
|
|
$this->createManyEntityPermissions($books, $roles);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Chunk through all chapters
|
2016-04-30 12:16:06 -04:00
|
|
|
$this->chapter->with('book', 'restrictions')->chunk(500, function ($books) use ($roles) {
|
2016-04-24 11:54:20 -04:00
|
|
|
$this->createManyEntityPermissions($books, $roles);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Chunk through all pages
|
2016-04-30 12:16:06 -04:00
|
|
|
$this->page->with('book', 'chapter', 'restrictions')->chunk(500, function ($books) use ($roles) {
|
2016-04-24 11:54:20 -04:00
|
|
|
$this->createManyEntityPermissions($books, $roles);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the entity permissions attached to a particular role.
|
|
|
|
* @param Role $role
|
|
|
|
*/
|
|
|
|
public function deleteEntityPermissionsForRole(Role $role)
|
|
|
|
{
|
|
|
|
$this->deleteManyEntityPermissionsForRoles([$role]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all of the entity permissions for a list of entities.
|
|
|
|
* @param Role[] $roles
|
|
|
|
*/
|
|
|
|
protected function deleteManyEntityPermissionsForRoles($roles)
|
|
|
|
{
|
|
|
|
foreach ($roles as $role) {
|
|
|
|
$role->entityPermissions()->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the entity permissions for a particular entity.
|
|
|
|
* @param Entity $entity
|
|
|
|
*/
|
|
|
|
public function deleteEntityPermissionsForEntity(Entity $entity)
|
|
|
|
{
|
|
|
|
$this->deleteManyEntityPermissionsForEntities([$entity]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all of the entity permissions for a list of entities.
|
|
|
|
* @param Entity[] $entities
|
|
|
|
*/
|
|
|
|
protected function deleteManyEntityPermissionsForEntities($entities)
|
|
|
|
{
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
$entity->permissions()->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:37:57 -04:00
|
|
|
/**
|
|
|
|
* Create & Save entity permissions for many entities and permissions.
|
|
|
|
* @param Collection $entities
|
|
|
|
* @param Collection $roles
|
|
|
|
*/
|
|
|
|
protected function createManyEntityPermissions($entities, $roles)
|
|
|
|
{
|
|
|
|
$entityPermissions = [];
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
foreach ($roles as $role) {
|
2016-04-26 16:48:17 -04:00
|
|
|
foreach ($this->getActions($entity) as $action) {
|
2016-04-23 13:14:26 -04:00
|
|
|
$entityPermissions[] = $this->createEntityPermissionData($entity, $role, $action);
|
|
|
|
}
|
2016-04-20 16:37:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->entityPermission->insert($entityPermissions);
|
|
|
|
}
|
|
|
|
|
2016-04-26 16:48:17 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the actions related to an entity.
|
|
|
|
* @param $entity
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getActions($entity)
|
|
|
|
{
|
|
|
|
$baseActions = ['view', 'update', 'delete'];
|
|
|
|
|
|
|
|
if ($entity->isA('chapter')) {
|
|
|
|
$baseActions[] = 'page-create';
|
|
|
|
} else if ($entity->isA('book')) {
|
|
|
|
$baseActions[] = 'page-create';
|
|
|
|
$baseActions[] = 'chapter-create';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $baseActions;
|
|
|
|
}
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
/**
|
|
|
|
* Create entity permission data for an entity and role
|
|
|
|
* for a particular action.
|
|
|
|
* @param Entity $entity
|
|
|
|
* @param Role $role
|
|
|
|
* @param $action
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-23 13:14:26 -04:00
|
|
|
protected function createEntityPermissionData(Entity $entity, Role $role, $action)
|
2016-04-20 16:37:57 -04:00
|
|
|
{
|
2016-04-26 16:48:17 -04:00
|
|
|
$permissionPrefix = (strpos($action, '-') === false ? ($entity->getType() . '-') : '') . $action;
|
2016-04-23 13:14:26 -04:00
|
|
|
$roleHasPermission = $role->hasPermission($permissionPrefix . '-all');
|
|
|
|
$roleHasPermissionOwn = $role->hasPermission($permissionPrefix . '-own');
|
2016-04-26 16:48:17 -04:00
|
|
|
$explodedAction = explode('-', $action);
|
|
|
|
$restrictionAction = end($explodedAction);
|
2016-04-23 13:14:26 -04:00
|
|
|
|
|
|
|
if ($entity->isA('book')) {
|
|
|
|
|
|
|
|
if (!$entity->restricted) {
|
|
|
|
return $this->createEntityPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn);
|
|
|
|
} else {
|
2016-04-26 16:48:17 -04:00
|
|
|
$hasAccess = $entity->hasActiveRestriction($role->id, $restrictionAction);
|
2016-04-23 13:14:26 -04:00
|
|
|
return $this->createEntityPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($entity->isA('chapter')) {
|
|
|
|
|
|
|
|
if (!$entity->restricted) {
|
2016-04-26 16:48:17 -04:00
|
|
|
$hasExplicitAccessToBook = $entity->book->hasActiveRestriction($role->id, $restrictionAction);
|
2016-04-24 11:54:20 -04:00
|
|
|
$hasPermissiveAccessToBook = !$entity->book->restricted;
|
2016-04-23 13:14:26 -04:00
|
|
|
return $this->createEntityPermissionDataArray($entity, $role, $action,
|
2016-04-24 11:54:20 -04:00
|
|
|
($hasExplicitAccessToBook || ($roleHasPermission && $hasPermissiveAccessToBook)),
|
|
|
|
($hasExplicitAccessToBook || ($roleHasPermissionOwn && $hasPermissiveAccessToBook)));
|
2016-04-23 13:14:26 -04:00
|
|
|
} else {
|
2016-04-26 16:48:17 -04:00
|
|
|
$hasAccess = $entity->hasActiveRestriction($role->id, $restrictionAction);
|
2016-04-23 13:14:26 -04:00
|
|
|
return $this->createEntityPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($entity->isA('page')) {
|
|
|
|
|
|
|
|
if (!$entity->restricted) {
|
2016-04-26 16:48:17 -04:00
|
|
|
$hasExplicitAccessToBook = $entity->book->hasActiveRestriction($role->id, $restrictionAction);
|
2016-04-24 11:54:20 -04:00
|
|
|
$hasPermissiveAccessToBook = !$entity->book->restricted;
|
2016-04-26 16:48:17 -04:00
|
|
|
$hasExplicitAccessToChapter = $entity->chapter && $entity->chapter->hasActiveRestriction($role->id, $restrictionAction);
|
2016-04-24 11:54:20 -04:00
|
|
|
$hasPermissiveAccessToChapter = $entity->chapter && !$entity->chapter->restricted;
|
|
|
|
$acknowledgeChapter = ($entity->chapter && $entity->chapter->restricted);
|
|
|
|
|
|
|
|
$hasExplicitAccessToParents = $acknowledgeChapter ? $hasExplicitAccessToChapter : $hasExplicitAccessToBook;
|
|
|
|
$hasPermissiveAccessToParents = $acknowledgeChapter ? $hasPermissiveAccessToChapter : $hasPermissiveAccessToBook;
|
|
|
|
|
2016-04-23 13:14:26 -04:00
|
|
|
return $this->createEntityPermissionDataArray($entity, $role, $action,
|
2016-04-24 11:54:20 -04:00
|
|
|
($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)),
|
|
|
|
($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents))
|
|
|
|
);
|
2016-04-23 13:14:26 -04:00
|
|
|
} else {
|
|
|
|
$hasAccess = $entity->hasRestriction($role->id, $action);
|
|
|
|
return $this->createEntityPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
/**
|
|
|
|
* Create an array of data with the information of an entity permissions.
|
|
|
|
* Used to build data for bulk insertion.
|
|
|
|
* @param Entity $entity
|
|
|
|
* @param Role $role
|
|
|
|
* @param $action
|
|
|
|
* @param $permissionAll
|
|
|
|
* @param $permissionOwn
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-23 13:14:26 -04:00
|
|
|
protected function createEntityPermissionDataArray(Entity $entity, Role $role, $action, $permissionAll, $permissionOwn)
|
|
|
|
{
|
|
|
|
$entityClass = get_class($entity);
|
|
|
|
return [
|
2016-04-30 12:16:06 -04:00
|
|
|
'role_id' => $role->getRawAttribute('id'),
|
|
|
|
'entity_id' => $entity->getRawAttribute('id'),
|
2016-04-23 13:14:26 -04:00
|
|
|
'entity_type' => $entityClass,
|
|
|
|
'action' => $action,
|
|
|
|
'has_permission' => $permissionAll,
|
|
|
|
'has_permission_own' => $permissionOwn,
|
2016-04-30 12:16:06 -04:00
|
|
|
'created_by' => $entity->getRawAttribute('created_by')
|
2016-04-23 13:14:26 -04:00
|
|
|
];
|
2016-02-29 15:31:21 -05:00
|
|
|
}
|
|
|
|
|
2016-03-05 13:09:21 -05:00
|
|
|
/**
|
|
|
|
* Checks if an entity has a restriction set upon it.
|
|
|
|
* @param Entity $entity
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param $permission
|
2016-03-05 13:09:21 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-24 11:54:20 -04:00
|
|
|
public function checkEntityUserAccess(Entity $entity, $permission)
|
2016-02-29 15:31:21 -05:00
|
|
|
{
|
|
|
|
if ($this->isAdmin) return true;
|
2016-04-24 11:54:20 -04:00
|
|
|
$explodedPermission = explode('-', $permission);
|
|
|
|
|
2016-02-29 15:31:21 -05:00
|
|
|
$baseQuery = $entity->where('id', '=', $entity->id);
|
2016-04-26 16:48:17 -04:00
|
|
|
$action = end($explodedPermission);
|
|
|
|
$this->currentAction = $action;
|
2016-04-24 11:54:20 -04:00
|
|
|
|
|
|
|
$nonEntityPermissions = ['restrictions'];
|
|
|
|
|
|
|
|
// Handle non entity specific permissions
|
|
|
|
if (in_array($explodedPermission[0], $nonEntityPermissions)) {
|
|
|
|
$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 === $entity->created_by;
|
|
|
|
return ($allPermission || ($isOwner && $ownPermission));
|
2016-02-29 15:31:21 -05:00
|
|
|
}
|
2016-04-24 11:54:20 -04:00
|
|
|
|
2016-04-26 16:48:17 -04:00
|
|
|
// Handle abnormal create permissions
|
|
|
|
if ($action === 'create') {
|
|
|
|
$this->currentAction = $permission;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
return $this->entityRestrictionQuery($baseQuery)->count() > 0;
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|
|
|
|
|
2016-03-30 15:15:44 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
/**
|
2016-04-23 13:14:26 -04:00
|
|
|
* The general query filter to remove all entities
|
|
|
|
* that the current user does not have access to.
|
2016-02-28 14:03:04 -05:00
|
|
|
* @param $query
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-04-23 13:14:26 -04:00
|
|
|
protected function entityRestrictionQuery($query)
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
2016-04-23 13:14:26 -04:00
|
|
|
return $query->where(function ($parentQuery) {
|
|
|
|
$parentQuery->whereHas('permissions', function ($permissionQuery) {
|
2016-05-01 14:36:53 -04:00
|
|
|
$permissionQuery->whereIn('role_id', $this->getRoles())
|
2016-04-23 13:14:26 -04:00
|
|
|
->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);
|
2016-02-28 14:03:04 -05:00
|
|
|
});
|
|
|
|
});
|
2016-04-23 13:14:26 -04:00
|
|
|
});
|
2016-02-28 14:03:04 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
/**
|
|
|
|
* Add restrictions for a page query
|
|
|
|
* @param $query
|
|
|
|
* @param string $action
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function enforcePageRestrictions($query, $action = 'view')
|
|
|
|
{
|
|
|
|
// 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) return $query;
|
|
|
|
$this->currentAction = $action;
|
|
|
|
return $this->entityRestrictionQuery($query);
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
/**
|
|
|
|
* Add on permission restrictions to a chapter query.
|
|
|
|
* @param $query
|
|
|
|
* @param string $action
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function enforceChapterRestrictions($query, $action = 'view')
|
|
|
|
{
|
|
|
|
if ($this->isAdmin) return $query;
|
|
|
|
$this->currentAction = $action;
|
2016-04-23 13:14:26 -04:00
|
|
|
return $this->entityRestrictionQuery($query);
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add restrictions to a book query.
|
|
|
|
* @param $query
|
|
|
|
* @param string $action
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function enforceBookRestrictions($query, $action = 'view')
|
|
|
|
{
|
|
|
|
if ($this->isAdmin) return $query;
|
|
|
|
$this->currentAction = $action;
|
2016-04-23 13:14:26 -04:00
|
|
|
return $this->entityRestrictionQuery($query);
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter items that have entities set a 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) return $query;
|
|
|
|
$this->currentAction = 'view';
|
|
|
|
$tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn, 'entityTypeColumn' => $entityTypeColumn];
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2016-02-29 15:31:21 -05:00
|
|
|
return $query->where(function ($query) use ($tableDetails) {
|
2016-04-23 13:14:26 -04:00
|
|
|
$query->whereExists(function ($permissionQuery) use (&$tableDetails) {
|
|
|
|
$permissionQuery->select('id')->from('entity_permissions')
|
|
|
|
->whereRaw('entity_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
|
|
|
|
->whereRaw('entity_permissions.entity_type=' . $tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'])
|
|
|
|
->where('action', '=', $this->currentAction)
|
2016-05-01 14:36:53 -04:00
|
|
|
->whereIn('role_id', $this->getRoles())
|
2016-04-23 13:14:26 -04:00
|
|
|
->where(function ($query) {
|
|
|
|
$query->where('has_permission', '=', true)->orWhere(function ($query) {
|
|
|
|
$query->where('has_permission_own', '=', true)
|
|
|
|
->where('created_by', '=', $this->currentUser->id);
|
2016-02-28 14:03:04 -05:00
|
|
|
});
|
2016-04-23 13:14:26 -04:00
|
|
|
});
|
2016-02-28 14:03:04 -05:00
|
|
|
});
|
|
|
|
});
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|
|
|
|
|
2016-03-13 09:30:47 -04:00
|
|
|
/**
|
|
|
|
* 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) return $query;
|
|
|
|
$this->currentAction = 'view';
|
|
|
|
$tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn];
|
2016-04-23 13:14:26 -04:00
|
|
|
|
|
|
|
return $query->where(function ($query) use ($tableDetails) {
|
2016-03-13 09:30:47 -04:00
|
|
|
$query->where(function ($query) use (&$tableDetails) {
|
2016-04-23 13:14:26 -04:00
|
|
|
$query->whereExists(function ($permissionQuery) use (&$tableDetails) {
|
|
|
|
$permissionQuery->select('id')->from('entity_permissions')
|
|
|
|
->whereRaw('entity_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
|
|
|
|
->where('entity_type', '=', 'Bookstack\\Page')
|
|
|
|
->where('action', '=', $this->currentAction)
|
2016-05-01 14:36:53 -04:00
|
|
|
->whereIn('role_id', $this->getRoles())
|
2016-03-13 09:30:47 -04:00
|
|
|
->where(function ($query) {
|
2016-04-23 13:14:26 -04:00
|
|
|
$query->where('has_permission', '=', true)->orWhere(function ($query) {
|
|
|
|
$query->where('has_permission_own', '=', true)
|
2016-04-26 16:48:17 -04:00
|
|
|
->where('created_by', '=', $this->currentUser->id);
|
2016-04-23 13:14:26 -04:00
|
|
|
});
|
2016-03-13 09:30:47 -04:00
|
|
|
});
|
2016-04-23 13:14:26 -04:00
|
|
|
});
|
|
|
|
})->orWhere($tableDetails['entityIdColumn'], '=', 0);
|
2016-03-13 09:30:47 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|