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;
|
2017-04-29 17:01:43 -04:00
|
|
|
use BookStack\EntityPermission;
|
2016-05-01 16:20:50 -04:00
|
|
|
use BookStack\JointPermission;
|
2016-07-01 15:11:49 -04:00
|
|
|
use BookStack\Ownable;
|
2016-04-20 16:37:57 -04:00
|
|
|
use BookStack\Page;
|
|
|
|
use BookStack\Role;
|
2016-04-26 16:48:17 -04:00
|
|
|
use BookStack\User;
|
2017-01-01 16:21:11 -05:00
|
|
|
use Illuminate\Database\Connection;
|
2017-01-01 11:05:44 -05:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2017-04-30 06:38:58 -04:00
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder;
|
2016-08-26 15:20:58 -04:00
|
|
|
use Illuminate\Support\Collection;
|
2016-02-29 15:31:21 -05:00
|
|
|
|
2016-05-01 16:20:50 -04:00
|
|
|
class PermissionService
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
protected $currentAction;
|
2016-09-17 13:22:04 -04:00
|
|
|
protected $isAdminUser;
|
|
|
|
protected $userRoles = false;
|
|
|
|
protected $currentUserModel = false;
|
2016-02-28 14:03:04 -05:00
|
|
|
|
2016-04-20 16:37:57 -04:00
|
|
|
public $book;
|
|
|
|
public $chapter;
|
|
|
|
public $page;
|
|
|
|
|
2017-01-01 16:21:11 -05:00
|
|
|
protected $db;
|
|
|
|
|
2016-05-01 16:20:50 -04:00
|
|
|
protected $jointPermission;
|
2016-04-20 16:37:57 -04:00
|
|
|
protected $role;
|
2017-04-29 17:01:43 -04:00
|
|
|
protected $entityPermission;
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2016-08-26 15:20:58 -04:00
|
|
|
protected $entityCache;
|
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* PermissionService constructor.
|
|
|
|
* @param JointPermission $jointPermission
|
2017-04-29 17:01:43 -04:00
|
|
|
* @param EntityPermission $entityPermission
|
2017-01-01 16:21:11 -05:00
|
|
|
* @param Connection $db
|
2016-04-20 16:37:57 -04:00
|
|
|
* @param Book $book
|
|
|
|
* @param Chapter $chapter
|
|
|
|
* @param Page $page
|
|
|
|
* @param Role $role
|
2016-02-28 14:03:04 -05:00
|
|
|
*/
|
2017-04-29 17:01:43 -04:00
|
|
|
public function __construct(JointPermission $jointPermission, EntityPermission $entityPermission, Connection $db, Book $book, Chapter $chapter, Page $page, Role $role)
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
2017-01-01 16:21:11 -05:00
|
|
|
$this->db = $db;
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->jointPermission = $jointPermission;
|
2017-04-29 17:01:43 -04:00
|
|
|
$this->entityPermission = $entityPermission;
|
2016-04-20 16:37:57 -04:00
|
|
|
$this->role = $role;
|
|
|
|
$this->book = $book;
|
|
|
|
$this->chapter = $chapter;
|
|
|
|
$this->page = $page;
|
2017-01-01 16:21:11 -05:00
|
|
|
// TODO - Update so admin still goes through filters
|
2016-04-20 16:37:57 -04:00
|
|
|
}
|
|
|
|
|
2017-04-30 06:38:58 -04:00
|
|
|
/**
|
|
|
|
* Set the database connection
|
|
|
|
* @param Connection $connection
|
|
|
|
*/
|
|
|
|
public function setConnection(Connection $connection)
|
|
|
|
{
|
|
|
|
$this->db = $connection;
|
|
|
|
}
|
|
|
|
|
2016-08-26 15:20:58 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
foreach ($this->currentUser()->roles as $role) {
|
2016-05-01 14:36:53 -04:00
|
|
|
$roles[] = $role->id;
|
|
|
|
}
|
|
|
|
return $roles;
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:37:57 -04:00
|
|
|
/**
|
|
|
|
* Re-generate all entity permission from scratch.
|
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
public function buildJointPermissions()
|
2016-04-20 16:37:57 -04:00
|
|
|
{
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->jointPermission->truncate();
|
2016-08-26 15:20:58 -04:00
|
|
|
$this->readyEntityCache();
|
2016-04-20 16:37:57 -04:00
|
|
|
|
|
|
|
// Get all roles (Should be the most limited dimension)
|
2017-04-30 06:38:58 -04:00
|
|
|
$roles = $this->role->with('permissions')->get()->all();
|
2016-04-20 16:37:57 -04:00
|
|
|
|
|
|
|
// Chunk through all books
|
2017-04-30 14:10:49 -04:00
|
|
|
$this->bookFetchQuery()->chunk(5, function ($books) use ($roles) {
|
|
|
|
$this->buildJointPermissionsForBooks($books, $roles);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a query for fetching a book with it's children.
|
|
|
|
* @return QueryBuilder
|
|
|
|
*/
|
|
|
|
protected function bookFetchQuery()
|
|
|
|
{
|
|
|
|
return $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
|
2017-04-30 06:38:58 -04:00
|
|
|
$query->select(['id', 'restricted', 'created_by', 'book_id']);
|
|
|
|
}, 'pages' => function($query) {
|
|
|
|
$query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
|
2017-04-30 14:10:49 -04:00
|
|
|
}]);
|
2017-04-29 17:01:43 -04:00
|
|
|
}
|
2016-04-20 16:37:57 -04:00
|
|
|
|
2017-04-29 17:01:43 -04:00
|
|
|
/**
|
|
|
|
* Build joint permissions for an array of books
|
|
|
|
* @param Collection $books
|
2017-04-30 06:38:58 -04:00
|
|
|
* @param array $roles
|
2017-04-29 17:01:43 -04:00
|
|
|
* @param bool $deleteOld
|
|
|
|
*/
|
|
|
|
protected function buildJointPermissionsForBooks($books, $roles, $deleteOld = false) {
|
|
|
|
$entities = clone $books;
|
2016-04-20 16:37:57 -04:00
|
|
|
|
2017-04-30 06:38:58 -04:00
|
|
|
/** @var Book $book */
|
|
|
|
foreach ($books->all() as $book) {
|
|
|
|
foreach ($book->getRelation('chapters') as $chapter) {
|
2017-04-29 17:01:43 -04:00
|
|
|
$entities->push($chapter);
|
|
|
|
}
|
2017-04-30 06:38:58 -04:00
|
|
|
foreach ($book->getRelation('pages') as $page) {
|
2017-04-29 17:01:43 -04:00
|
|
|
$entities->push($page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($deleteOld) $this->deleteManyJointPermissionsForEntities($entities->all());
|
|
|
|
$this->createManyJointPermissions($entities, $roles);
|
2016-04-20 16:37:57 -04:00
|
|
|
}
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
/**
|
2016-08-26 15:20:58 -04:00
|
|
|
* Rebuild the entity jointPermissions for a particular entity.
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param Entity $entity
|
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
public function buildJointPermissionsForEntity(Entity $entity)
|
2016-04-24 11:54:20 -04:00
|
|
|
{
|
2017-04-30 14:10:49 -04:00
|
|
|
$entities = [$entity];
|
|
|
|
if ($entity->isA('book')) {
|
|
|
|
$books = $this->bookFetchQuery()->where('id', '=', $entity->id)->get();
|
|
|
|
$this->buildJointPermissionsForBooks($books, $this->role->newQuery()->get(), true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entities[] = $entity->book;
|
2017-11-19 10:56:06 -05:00
|
|
|
|
|
|
|
if ($entity->isA('page') && $entity->chapter_id) {
|
|
|
|
$entities[] = $entity->chapter;
|
|
|
|
}
|
|
|
|
|
2017-04-30 14:10:49 -04:00
|
|
|
if ($entity->isA('chapter')) {
|
|
|
|
foreach ($entity->pages as $page) {
|
|
|
|
$entities[] = $page;
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 10:56:06 -05:00
|
|
|
|
2017-04-30 14:10:49 -04:00
|
|
|
$this->deleteManyJointPermissionsForEntities($entities);
|
|
|
|
$this->buildJointPermissionsForEntities(collect($entities));
|
2016-04-24 11:54:20 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 15:20:58 -04:00
|
|
|
/**
|
|
|
|
* Rebuild the entity jointPermissions for a collection of entities.
|
|
|
|
* @param Collection $entities
|
|
|
|
*/
|
|
|
|
public function buildJointPermissionsForEntities(Collection $entities)
|
|
|
|
{
|
2017-04-30 14:10:49 -04:00
|
|
|
$roles = $this->role->newQuery()->get();
|
2017-04-29 17:01:43 -04:00
|
|
|
$this->deleteManyJointPermissionsForEntities($entities->all());
|
2016-08-26 15:20:58 -04:00
|
|
|
$this->createManyJointPermissions($entities, $roles);
|
|
|
|
}
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* Build the entity jointPermissions for a particular role.
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param Role $role
|
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
public function buildJointPermissionForRole(Role $role)
|
2016-04-24 11:54:20 -04:00
|
|
|
{
|
2017-04-30 06:38:58 -04:00
|
|
|
$roles = [$role];
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->deleteManyJointPermissionsForRoles($roles);
|
2016-04-24 11:54:20 -04:00
|
|
|
|
|
|
|
// Chunk through all books
|
2017-04-30 14:10:49 -04:00
|
|
|
$this->bookFetchQuery()->chunk(5, function ($books) use ($roles) {
|
2017-04-29 17:01:43 -04:00
|
|
|
$this->buildJointPermissionsForBooks($books, $roles);
|
2016-04-24 11:54:20 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* Delete the entity jointPermissions attached to a particular role.
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param Role $role
|
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
public function deleteJointPermissionsForRole(Role $role)
|
2016-04-24 11:54:20 -04:00
|
|
|
{
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->deleteManyJointPermissionsForRoles([$role]);
|
2016-04-24 11:54:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* Delete all of the entity jointPermissions for a list of entities.
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param Role[] $roles
|
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
protected function deleteManyJointPermissionsForRoles($roles)
|
2016-04-24 11:54:20 -04:00
|
|
|
{
|
2017-04-30 06:38:58 -04:00
|
|
|
$roleIds = array_map(function($role) {
|
|
|
|
return $role->id;
|
|
|
|
}, $roles);
|
2017-06-04 10:37:10 -04:00
|
|
|
$this->jointPermission->newQuery()->whereIn('role_id', $roleIds)->delete();
|
2016-04-24 11:54:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* Delete the entity jointPermissions for a particular entity.
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param Entity $entity
|
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
public function deleteJointPermissionsForEntity(Entity $entity)
|
2016-04-24 11:54:20 -04:00
|
|
|
{
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->deleteManyJointPermissionsForEntities([$entity]);
|
2016-04-24 11:54:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* Delete all of the entity jointPermissions for a list of entities.
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param Entity[] $entities
|
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
protected function deleteManyJointPermissionsForEntities($entities)
|
2016-04-24 11:54:20 -04:00
|
|
|
{
|
2017-01-23 17:27:11 -05:00
|
|
|
if (count($entities) === 0) return;
|
2017-04-29 17:01:43 -04:00
|
|
|
|
2017-04-30 06:38:58 -04:00
|
|
|
$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();
|
|
|
|
}
|
2017-04-29 17:01:43 -04:00
|
|
|
|
2017-04-30 06:38:58 -04:00
|
|
|
});
|
2016-04-24 11:54:20 -04:00
|
|
|
}
|
|
|
|
|
2016-04-20 16:37:57 -04:00
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* Create & Save entity jointPermissions for many entities and jointPermissions.
|
2016-04-20 16:37:57 -04:00
|
|
|
* @param Collection $entities
|
2017-04-30 06:38:58 -04:00
|
|
|
* @param array $roles
|
2016-04-20 16:37:57 -04:00
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
protected function createManyJointPermissions($entities, $roles)
|
2016-04-20 16:37:57 -04:00
|
|
|
{
|
2016-08-26 15:20:58 -04:00
|
|
|
$this->readyEntityCache();
|
2016-05-01 16:20:50 -04:00
|
|
|
$jointPermissions = [];
|
2017-04-29 17:01:43 -04:00
|
|
|
|
|
|
|
// 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
|
2016-04-20 16:37:57 -04:00
|
|
|
foreach ($entities as $entity) {
|
|
|
|
foreach ($roles as $role) {
|
2016-04-26 16:48:17 -04:00
|
|
|
foreach ($this->getActions($entity) as $action) {
|
2017-04-29 17:01:43 -04:00
|
|
|
$jointPermissions[] = $this->createJointPermissionData($entity, $role, $action, $permissionMap, $rolePermissionMap);
|
2016-04-23 13:14:26 -04:00
|
|
|
}
|
2016-04-20 16:37:57 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 06:38:58 -04:00
|
|
|
|
|
|
|
$this->db->transaction(function() use ($jointPermissions) {
|
|
|
|
foreach (array_chunk($jointPermissions, 1000) as $jointPermissionChunk) {
|
|
|
|
$this->db->table('joint_permissions')->insert($jointPermissionChunk);
|
|
|
|
}
|
|
|
|
});
|
2016-04-20 16:37:57 -04:00
|
|
|
}
|
|
|
|
|
2016-04-26 16:48:17 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the actions related to an entity.
|
2017-04-29 17:01:43 -04:00
|
|
|
* @param Entity $entity
|
2016-04-26 16:48:17 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-04-29 17:01:43 -04:00
|
|
|
protected function getActions(Entity $entity)
|
2016-04-26 16:48:17 -04:00
|
|
|
{
|
|
|
|
$baseActions = ['view', 'update', 'delete'];
|
2017-04-29 17:01:43 -04:00
|
|
|
if ($entity->isA('chapter') || $entity->isA('book')) $baseActions[] = 'page-create';
|
|
|
|
if ($entity->isA('book')) $baseActions[] = 'chapter-create';
|
|
|
|
return $baseActions;
|
2016-04-26 16:48:17 -04:00
|
|
|
}
|
|
|
|
|
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
|
2017-04-29 17:01:43 -04:00
|
|
|
* @param string $action
|
|
|
|
* @param array $permissionMap
|
|
|
|
* @param array $rolePermissionMap
|
2016-04-24 11:54:20 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-04-29 17:01:43 -04:00
|
|
|
protected function createJointPermissionData(Entity $entity, Role $role, $action, $permissionMap, $rolePermissionMap)
|
2016-04-20 16:37:57 -04:00
|
|
|
{
|
2016-04-26 16:48:17 -04:00
|
|
|
$permissionPrefix = (strpos($action, '-') === false ? ($entity->getType() . '-') : '') . $action;
|
2017-04-29 17:01:43 -04:00
|
|
|
$roleHasPermission = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-all']);
|
|
|
|
$roleHasPermissionOwn = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-own']);
|
2016-04-26 16:48:17 -04:00
|
|
|
$explodedAction = explode('-', $action);
|
|
|
|
$restrictionAction = end($explodedAction);
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2017-01-01 16:21:11 -05:00
|
|
|
if ($role->system_name === 'admin') {
|
|
|
|
return $this->createJointPermissionDataArray($entity, $role, $action, true, true);
|
|
|
|
}
|
|
|
|
|
2017-04-29 17:01:43 -04:00
|
|
|
if ($entity->restricted) {
|
|
|
|
$hasAccess = $this->mapHasActiveRestriction($permissionMap, $entity, $role, $restrictionAction);
|
|
|
|
return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
|
|
|
|
}
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2017-04-29 17:01:43 -04:00
|
|
|
if ($entity->isA('book')) {
|
|
|
|
return $this->createJointPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn);
|
|
|
|
}
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2017-04-29 17:01:43 -04:00
|
|
|
// 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
|
2017-05-07 14:34:11 -04:00
|
|
|
if ($entity->isA('page') && $entity->chapter_id !== 0 && $entity->chapter_id !== '0') {
|
2017-04-29 17:01:43 -04:00
|
|
|
$chapter = $this->getChapter($entity->chapter_id);
|
|
|
|
$hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapter->restricted;
|
|
|
|
if ($chapter->restricted) {
|
|
|
|
$hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $chapter, $role, $restrictionAction);
|
2016-04-23 13:14:26 -04:00
|
|
|
}
|
2017-04-29 17:01:43 -04:00
|
|
|
}
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2017-04-29 17:01:43 -04:00
|
|
|
return $this->createJointPermissionDataArray($entity, $role, $action,
|
|
|
|
($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)),
|
|
|
|
($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents))
|
|
|
|
);
|
|
|
|
}
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2017-04-29 17:01:43 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
2016-04-23 13:14:26 -04:00
|
|
|
}
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* Create an array of data with the information of an entity jointPermissions.
|
2016-04-24 11:54:20 -04:00
|
|
|
* Used to build data for bulk insertion.
|
|
|
|
* @param Entity $entity
|
|
|
|
* @param Role $role
|
|
|
|
* @param $action
|
|
|
|
* @param $permissionAll
|
|
|
|
* @param $permissionOwn
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
protected function createJointPermissionDataArray(Entity $entity, Role $role, $action, $permissionAll, $permissionOwn)
|
2016-04-23 13:14:26 -04:00
|
|
|
{
|
|
|
|
return [
|
2016-04-30 12:16:06 -04:00
|
|
|
'role_id' => $role->getRawAttribute('id'),
|
|
|
|
'entity_id' => $entity->getRawAttribute('id'),
|
2017-04-29 17:01:43 -04:00
|
|
|
'entity_type' => $entity->getMorphClass(),
|
2016-04-23 13:14:26 -04:00
|
|
|
'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.
|
2016-07-01 15:11:49 -04:00
|
|
|
* @param Ownable $ownable
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param $permission
|
2016-03-05 13:09:21 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-07-01 15:11:49 -04:00
|
|
|
public function checkOwnableUserAccess(Ownable $ownable, $permission)
|
2016-02-29 15:31:21 -05:00
|
|
|
{
|
2016-09-17 13:22:04 -04:00
|
|
|
if ($this->isAdmin()) {
|
|
|
|
$this->clean();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-24 11:54:20 -04:00
|
|
|
$explodedPermission = explode('-', $permission);
|
|
|
|
|
2016-07-01 15:11:49 -04:00
|
|
|
$baseQuery = $ownable->where('id', '=', $ownable->id);
|
2016-04-26 16:48:17 -04:00
|
|
|
$action = end($explodedPermission);
|
|
|
|
$this->currentAction = $action;
|
2016-04-24 11:54:20 -04:00
|
|
|
|
2017-01-29 03:55:20 -05:00
|
|
|
$nonJointPermissions = ['restrictions', 'image', 'attachment', 'comment'];
|
2016-04-24 11:54:20 -04:00
|
|
|
|
2016-05-01 16:20:50 -04:00
|
|
|
// Handle non entity specific jointPermissions
|
|
|
|
if (in_array($explodedPermission[0], $nonJointPermissions)) {
|
2016-09-17 13:22:04 -04:00
|
|
|
$allPermission = $this->currentUser() && $this->currentUser()->can($permission . '-all');
|
|
|
|
$ownPermission = $this->currentUser() && $this->currentUser()->can($permission . '-own');
|
2016-04-24 11:54:20 -04:00
|
|
|
$this->currentAction = 'view';
|
2016-09-17 13:22:04 -04:00
|
|
|
$isOwner = $this->currentUser() && $this->currentUser()->id === $ownable->created_by;
|
2016-04-24 11:54:20 -04:00
|
|
|
return ($allPermission || ($isOwner && $ownPermission));
|
2016-02-29 15:31:21 -05:00
|
|
|
}
|
2016-04-24 11:54:20 -04:00
|
|
|
|
2016-05-01 16:20:50 -04:00
|
|
|
// Handle abnormal create jointPermissions
|
2016-04-26 16:48:17 -04:00
|
|
|
if ($action === 'create') {
|
|
|
|
$this->currentAction = $permission;
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
$q = $this->entityRestrictionQuery($baseQuery)->count() > 0;
|
|
|
|
$this->clean();
|
|
|
|
return $q;
|
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-09-17 13:22:04 -04:00
|
|
|
$q = $query->where(function ($parentQuery) {
|
2016-05-01 16:20:50 -04:00
|
|
|
$parentQuery->whereHas('jointPermissions', 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)
|
2016-09-17 13:22:04 -04:00
|
|
|
->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-09-17 13:22:04 -04:00
|
|
|
$this->clean();
|
|
|
|
return $q;
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|
|
|
|
|
2017-01-15 10:00:29 -05:00
|
|
|
/**
|
|
|
|
* Get the children of a book in an efficient single query, Filtered by the permission system.
|
|
|
|
* @param integer $book_id
|
2017-02-26 08:26:51 -05:00
|
|
|
* @param bool $filterDrafts
|
|
|
|
* @param bool $fetchPageContent
|
2017-04-30 06:38:58 -04:00
|
|
|
* @return QueryBuilder
|
2017-01-15 10:00:29 -05:00
|
|
|
*/
|
2017-02-26 08:26:51 -05:00
|
|
|
public function bookChildrenQuery($book_id, $filterDrafts = false, $fetchPageContent = false) {
|
2017-03-19 08:48:44 -04:00
|
|
|
$pageSelect = $this->db->table('pages')->selectRaw($this->page->entityRawQuery($fetchPageContent))->where('book_id', '=', $book_id)->where(function($query) use ($filterDrafts) {
|
2017-01-15 10:00:29 -05:00
|
|
|
$query->where('draft', '=', 0);
|
|
|
|
if (!$filterDrafts) {
|
|
|
|
$query->orWhere(function($query) {
|
|
|
|
$query->where('draft', '=', 1)->where('created_by', '=', $this->currentUser()->id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-03-19 08:48:44 -04:00
|
|
|
$chapterSelect = $this->db->table('chapters')->selectRaw($this->chapter->entityRawQuery())->where('book_id', '=', $book_id);
|
2017-01-15 10:00:29 -05:00
|
|
|
$query = $this->db->query()->select('*')->from($this->db->raw("({$pageSelect->toSql()} UNION {$chapterSelect->toSql()}) AS U"))
|
2017-01-22 07:02:30 -05:00
|
|
|
->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');
|
2017-01-01 16:21:11 -05:00
|
|
|
$this->clean();
|
2017-01-15 10:00:29 -05:00
|
|
|
return $query;
|
2017-01-01 16:21:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-06 15:33:08 -04:00
|
|
|
/**
|
|
|
|
* Add restrictions for a generic entity
|
2017-01-01 11:05:44 -05:00
|
|
|
* @param string $entityType
|
|
|
|
* @param Builder|Entity $query
|
2016-05-06 15:33:08 -04:00
|
|
|
* @param string $action
|
2017-03-26 14:24:57 -04:00
|
|
|
* @return Builder
|
2016-05-06 15:33:08 -04:00
|
|
|
*/
|
2017-01-01 11:05:44 -05:00
|
|
|
public function enforceEntityRestrictions($entityType, $query, $action = 'view')
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
2017-01-01 11:05:44 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
if ($this->isAdmin()) {
|
|
|
|
$this->clean();
|
|
|
|
return $query;
|
|
|
|
}
|
2017-01-01 11:05:44 -05:00
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
$this->currentAction = $action;
|
2016-04-23 13:14:26 -04:00
|
|
|
return $this->entityRestrictionQuery($query);
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-19 08:48:44 -04:00
|
|
|
* Filter items that have entities set as a polymorphic relation.
|
2016-02-28 14:03:04 -05:00
|
|
|
* @param $query
|
|
|
|
* @param string $tableName
|
|
|
|
* @param string $entityIdColumn
|
|
|
|
* @param string $entityTypeColumn
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function filterRestrictedEntityRelations($query, $tableName, $entityIdColumn, $entityTypeColumn)
|
|
|
|
{
|
2016-09-17 13:22:04 -04:00
|
|
|
if ($this->isAdmin()) {
|
|
|
|
$this->clean();
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
$this->currentAction = 'view';
|
|
|
|
$tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn, 'entityTypeColumn' => $entityTypeColumn];
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
$q = $query->where(function ($query) use ($tableDetails) {
|
2016-04-23 13:14:26 -04:00
|
|
|
$query->whereExists(function ($permissionQuery) use (&$tableDetails) {
|
2016-05-01 16:20:50 -04:00
|
|
|
$permissionQuery->select('id')->from('joint_permissions')
|
|
|
|
->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
|
|
|
|
->whereRaw('joint_permissions.entity_type=' . $tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'])
|
2016-04-23 13:14:26 -04:00
|
|
|
->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)
|
2016-09-17 13:22:04 -04:00
|
|
|
->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
|
|
|
});
|
|
|
|
});
|
2017-01-21 11:16:27 -05:00
|
|
|
$this->clean();
|
2016-09-17 13:22:04 -04:00
|
|
|
return $q;
|
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)
|
|
|
|
{
|
2016-09-17 13:22:04 -04:00
|
|
|
if ($this->isAdmin()) {
|
|
|
|
$this->clean();
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-03-13 09:30:47 -04:00
|
|
|
$this->currentAction = 'view';
|
|
|
|
$tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn];
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
$q = $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) {
|
2016-05-01 16:20:50 -04:00
|
|
|
$permissionQuery->select('id')->from('joint_permissions')
|
|
|
|
->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
|
2016-04-23 13:14:26 -04:00
|
|
|
->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-09-17 13:22:04 -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-09-17 13:22:04 -04:00
|
|
|
$this->clean();
|
|
|
|
return $q;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the current user is an admin.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isAdmin()
|
|
|
|
{
|
|
|
|
if ($this->isAdminUser === null) {
|
2017-01-01 16:21:11 -05:00
|
|
|
$this->isAdminUser = ($this->currentUser()->id !== null) ? $this->currentUser()->hasSystemRole('admin') : false;
|
2016-09-17 13:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isAdminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current user
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
private function currentUser()
|
|
|
|
{
|
|
|
|
if ($this->currentUserModel === false) {
|
2016-09-29 07:43:46 -04:00
|
|
|
$this->currentUserModel = user();
|
2016-09-17 13:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->currentUserModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean the cached user elements.
|
|
|
|
*/
|
|
|
|
private function clean()
|
|
|
|
{
|
|
|
|
$this->currentUserModel = false;
|
|
|
|
$this->userRoles = false;
|
|
|
|
$this->isAdminUser = null;
|
2016-03-13 09:30:47 -04:00
|
|
|
}
|
|
|
|
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|