2016-02-28 14:03:04 -05:00
|
|
|
<?php namespace BookStack\Services;
|
|
|
|
|
2016-04-20 16:37:57 -04:00
|
|
|
use BookStack\Book;
|
2018-09-20 10:27:30 -04:00
|
|
|
use BookStack\Bookshelf;
|
2016-04-20 16:37:57 -04:00
|
|
|
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;
|
2018-09-20 10:27:30 -04:00
|
|
|
public $bookshelf;
|
2016-04-20 16:37:57 -04:00
|
|
|
|
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
|
2018-09-20 10:27:30 -04:00
|
|
|
* @param Role $role
|
2017-01-01 16:21:11 -05:00
|
|
|
* @param Connection $db
|
2018-09-20 10:27:30 -04:00
|
|
|
* @param Bookshelf $bookshelf
|
2016-04-20 16:37:57 -04:00
|
|
|
* @param Book $book
|
|
|
|
* @param Chapter $chapter
|
|
|
|
* @param Page $page
|
2016-02-28 14:03:04 -05:00
|
|
|
*/
|
2018-09-20 10:27:30 -04:00
|
|
|
public function __construct(
|
|
|
|
JointPermission $jointPermission, EntityPermission $entityPermission, Role $role, Connection $db,
|
|
|
|
Bookshelf $bookshelf, Book $book, Chapter $chapter, Page $page
|
|
|
|
)
|
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;
|
2018-09-20 10:27:30 -04:00
|
|
|
$this->bookshelf = $bookshelf;
|
2016-04-20 16:37:57 -04:00
|
|
|
$this->book = $book;
|
|
|
|
$this->chapter = $chapter;
|
|
|
|
$this->page = $page;
|
|
|
|
}
|
|
|
|
|
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
|
2018-04-14 17:17:47 -04:00
|
|
|
* @param Entity[] $entities
|
2016-08-26 15:20:58 -04:00
|
|
|
*/
|
2018-04-14 17:17:47 -04:00
|
|
|
protected function readyEntityCache($entities = [])
|
2016-08-26 15:20:58 -04:00
|
|
|
{
|
2018-04-14 17:17:47 -04:00
|
|
|
$this->entityCache = [];
|
|
|
|
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
$type = $entity->getType();
|
|
|
|
if (!isset($this->entityCache[$type])) {
|
|
|
|
$this->entityCache[$type] = collect();
|
|
|
|
}
|
|
|
|
$this->entityCache[$type]->put($entity->id, $entity);
|
|
|
|
}
|
2016-08-26 15:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a book via ID, Checks local cache
|
|
|
|
* @param $bookId
|
|
|
|
* @return Book
|
|
|
|
*/
|
|
|
|
protected function getBook($bookId)
|
|
|
|
{
|
2018-04-14 17:17:47 -04:00
|
|
|
if (isset($this->entityCache['book']) && $this->entityCache['book']->has($bookId)) {
|
|
|
|
return $this->entityCache['book']->get($bookId);
|
2016-08-26 15:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$book = $this->book->find($bookId);
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($book === null) {
|
|
|
|
$book = false;
|
|
|
|
}
|
2016-08-26 15:20:58 -04:00
|
|
|
|
|
|
|
return $book;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a chapter via ID, Checks local cache
|
|
|
|
* @param $chapterId
|
|
|
|
* @return Book
|
|
|
|
*/
|
|
|
|
protected function getChapter($chapterId)
|
|
|
|
{
|
2018-04-14 17:17:47 -04:00
|
|
|
if (isset($this->entityCache['chapter']) && $this->entityCache['chapter']->has($chapterId)) {
|
|
|
|
return $this->entityCache['chapter']->get($chapterId);
|
2016-08-26 15:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$chapter = $this->chapter->find($chapterId);
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($chapter === null) {
|
|
|
|
$chapter = false;
|
|
|
|
}
|
2016-08-26 15:20:58 -04:00
|
|
|
|
|
|
|
return $chapter;
|
|
|
|
}
|
|
|
|
|
2016-05-01 14:36:53 -04:00
|
|
|
/**
|
|
|
|
* Get the roles for the current user;
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
protected function getRoles()
|
|
|
|
{
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($this->userRoles !== false) {
|
|
|
|
return $this->userRoles;
|
|
|
|
}
|
2016-05-01 14:36:53 -04:00
|
|
|
|
|
|
|
$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);
|
|
|
|
});
|
2018-09-20 10:27:30 -04:00
|
|
|
|
|
|
|
// Chunk through all bookshelves
|
|
|
|
$this->bookshelf->newQuery()->select(['id', 'restricted', 'created_by'])
|
|
|
|
->chunk(50, function ($shelves) use ($roles) {
|
|
|
|
$this->buildJointPermissionsForShelves($shelves, $roles);
|
|
|
|
});
|
2017-04-30 14:10:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a query for fetching a book with it's children.
|
|
|
|
* @return QueryBuilder
|
|
|
|
*/
|
|
|
|
protected function bookFetchQuery()
|
|
|
|
{
|
2018-01-28 11:58:52 -05:00
|
|
|
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']);
|
2018-01-28 11:58:52 -05:00
|
|
|
}, 'pages' => function ($query) {
|
2017-04-30 06:38:58 -04:00
|
|
|
$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
|
|
|
|
2018-09-20 10:27:30 -04:00
|
|
|
/**
|
|
|
|
* @param Collection $shelves
|
|
|
|
* @param array $roles
|
|
|
|
* @param bool $deleteOld
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
protected function buildJointPermissionsForShelves($shelves, $roles, $deleteOld = false)
|
|
|
|
{
|
|
|
|
if ($deleteOld) {
|
|
|
|
$this->deleteManyJointPermissionsForEntities($shelves->all());
|
|
|
|
}
|
|
|
|
$this->createManyJointPermissions($shelves, $roles);
|
|
|
|
}
|
|
|
|
|
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
|
2018-04-14 17:17:47 -04:00
|
|
|
* @throws \Throwable
|
2017-04-29 17:01:43 -04:00
|
|
|
*/
|
2018-01-28 11:58:52 -05:00
|
|
|
protected function buildJointPermissionsForBooks($books, $roles, $deleteOld = false)
|
|
|
|
{
|
2017-04-29 17:01:43 -04:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($deleteOld) {
|
|
|
|
$this->deleteManyJointPermissionsForEntities($entities->all());
|
|
|
|
}
|
2017-04-29 17:01:43 -04:00
|
|
|
$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
|
2018-08-27 09:18:09 -04:00
|
|
|
* @throws \Throwable
|
2016-04-24 11:54:20 -04:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-27 09:18:09 -04:00
|
|
|
if ($entity->book) {
|
|
|
|
$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->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
|
2018-08-27 09:18:09 -04:00
|
|
|
* @throws \Throwable
|
2016-08-26 15:20:58 -04:00
|
|
|
*/
|
|
|
|
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
|
2018-04-14 17:17:47 -04:00
|
|
|
$this->bookFetchQuery()->chunk(20, function ($books) use ($roles) {
|
2017-04-29 17:01:43 -04:00
|
|
|
$this->buildJointPermissionsForBooks($books, $roles);
|
2016-04-24 11:54:20 -04:00
|
|
|
});
|
2018-09-20 10:27:30 -04:00
|
|
|
|
|
|
|
// Chunk through all bookshelves
|
|
|
|
$this->bookshelf->newQuery()->select(['id', 'restricted', 'created_by'])
|
|
|
|
->chunk(50, function ($shelves) use ($roles) {
|
|
|
|
$this->buildJointPermissionsForShelves($shelves, $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
|
|
|
{
|
2018-01-28 11:58:52 -05:00
|
|
|
$roleIds = array_map(function ($role) {
|
2017-04-30 06:38:58 -04:00
|
|
|
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
|
2018-04-14 17:17:47 -04:00
|
|
|
* @throws \Throwable
|
2016-04-24 11:54:20 -04:00
|
|
|
*/
|
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
|
2018-04-14 17:17:47 -04:00
|
|
|
* @throws \Throwable
|
2016-04-24 11:54:20 -04:00
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
protected function deleteManyJointPermissionsForEntities($entities)
|
2016-04-24 11:54:20 -04:00
|
|
|
{
|
2018-01-28 11:58:52 -05:00
|
|
|
if (count($entities) === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-29 17:01:43 -04:00
|
|
|
|
2018-01-28 11:58:52 -05:00
|
|
|
$this->db->transaction(function () use ($entities) {
|
2017-04-30 06:38:58 -04:00
|
|
|
|
|
|
|
foreach (array_chunk($entities, 1000) as $entityChunk) {
|
|
|
|
$query = $this->db->table('joint_permissions');
|
|
|
|
foreach ($entityChunk as $entity) {
|
2018-01-28 11:58:52 -05:00
|
|
|
$query->orWhere(function (QueryBuilder $query) use ($entity) {
|
2017-04-30 06:38:58 -04:00
|
|
|
$query->where('entity_id', '=', $entity->id)
|
|
|
|
->where('entity_type', '=', $entity->getMorphClass());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$query->delete();
|
|
|
|
}
|
|
|
|
});
|
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
|
2018-04-14 17:17:47 -04:00
|
|
|
* @throws \Throwable
|
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
|
|
|
{
|
2018-04-14 17:17:47 -04:00
|
|
|
$this->readyEntityCache($entities);
|
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'));
|
2018-01-28 11:58:52 -05:00
|
|
|
$permissionFetch->orWhere(function ($query) use ($entity) {
|
2017-04-29 17:01:43 -04:00
|
|
|
$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) {
|
2018-04-14 17:17:47 -04:00
|
|
|
foreach ($role->permissions as $permission) {
|
2017-04-29 17:01:43 -04:00
|
|
|
$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
|
|
|
|
2018-01-28 11:58:52 -05:00
|
|
|
$this->db->transaction(function () use ($jointPermissions) {
|
2017-04-30 06:38:58 -04:00
|
|
|
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'];
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($entity->isA('chapter') || $entity->isA('book')) {
|
|
|
|
$baseActions[] = 'page-create';
|
|
|
|
}
|
|
|
|
if ($entity->isA('book')) {
|
|
|
|
$baseActions[] = 'chapter-create';
|
|
|
|
}
|
2017-04-29 17:01:43 -04:00
|
|
|
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
|
|
|
|
2018-08-27 09:18:09 -04:00
|
|
|
if ($entity->isA('book') || $entity->isA('bookshelf')) {
|
2017-04-29 17:01:43 -04:00
|
|
|
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
|
|
|
|
2018-01-28 11:58:52 -05:00
|
|
|
return $this->createJointPermissionDataArray(
|
|
|
|
$entity,
|
|
|
|
$role,
|
|
|
|
$action,
|
2017-04-29 17:01:43 -04:00
|
|
|
($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
|
|
|
|
*/
|
2018-01-28 11:58:52 -05:00
|
|
|
protected function mapHasActiveRestriction($entityMap, Entity $entity, Role $role, $action)
|
|
|
|
{
|
2017-04-29 17:01:43 -04:00
|
|
|
$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-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
|
|
|
*/
|
2018-01-28 11:58:52 -05:00
|
|
|
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) {
|
2017-01-15 10:00:29 -05:00
|
|
|
$query->where('draft', '=', 0);
|
|
|
|
if (!$filterDrafts) {
|
2018-01-28 11:58:52 -05:00
|
|
|
$query->orWhere(function ($query) {
|
2017-01-15 10:00:29 -05:00
|
|
|
$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);
|
|
|
|
|
2018-09-20 14:48:08 -04:00
|
|
|
// Add joint permission filter
|
|
|
|
$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);
|
2017-01-22 07:02:30 -05:00
|
|
|
});
|
2018-09-20 14:48:08 -04:00
|
|
|
});
|
|
|
|
$query->whereRaw("({$whereQuery->toSql()}) > 0")->mergeBindings($whereQuery);
|
2017-01-22 07:02:30 -05:00
|
|
|
|
|
|
|
$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-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
|
2018-04-14 13:00:16 -04:00
|
|
|
* @param string $action
|
2016-02-28 14:03:04 -05:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-04-14 13:00:16 -04:00
|
|
|
public function filterRestrictedEntityRelations($query, $tableName, $entityIdColumn, $entityTypeColumn, $action = 'view')
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
2016-09-17 13:22:04 -04:00
|
|
|
|
2018-04-14 13:00:16 -04:00
|
|
|
$this->currentAction = $action;
|
2016-02-28 14:03:04 -05:00
|
|
|
$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)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|