2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Auth\Permissions;
|
2018-09-25 07:30:50 -04:00
|
|
|
|
2018-09-25 11:58:03 -04:00
|
|
|
use BookStack\Auth\Role;
|
2021-03-14 15:52:07 -04:00
|
|
|
use BookStack\Auth\User;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
2021-03-14 15:52:07 -04:00
|
|
|
use BookStack\Entities\Models\BookChild;
|
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2021-03-13 10:18:37 -05:00
|
|
|
use BookStack\Entities\Models\Chapter;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2021-03-13 10:18:37 -05:00
|
|
|
use BookStack\Entities\Models\Page;
|
2020-12-30 13:25:35 -05:00
|
|
|
use BookStack\Model;
|
|
|
|
use BookStack\Traits\HasCreatorAndUpdater;
|
|
|
|
use BookStack\Traits\HasOwner;
|
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;
|
2021-03-14 15:52:07 -04:00
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
2017-04-30 06:38:58 -04:00
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder;
|
2021-03-14 15:52:07 -04:00
|
|
|
use Throwable;
|
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
|
|
|
{
|
2018-09-25 13:00:40 -04:00
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* @var ?array
|
2018-09-25 13:00:40 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected $userRoles = null;
|
2017-01-01 16:21:11 -05:00
|
|
|
|
2018-09-25 13:00:40 -04:00
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* @var ?User
|
2018-09-25 13:00:40 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected $currentUserModel = null;
|
2018-09-25 13:00:40 -04:00
|
|
|
|
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* @var Connection
|
2018-09-25 13:00:40 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected $db;
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2018-09-25 13:00:40 -04:00
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* @var array
|
2018-09-25 13:00:40 -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.
|
2016-02-28 14:03:04 -05:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
public function __construct(Connection $db)
|
|
|
|
{
|
2017-01-01 16:21:11 -05:00
|
|
|
$this->db = $db;
|
2016-04-20 16:37:57 -04:00
|
|
|
}
|
|
|
|
|
2017-04-30 06:38:58 -04:00
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Set the database connection.
|
2017-04-30 06:38:58 -04:00
|
|
|
*/
|
|
|
|
public function setConnection(Connection $connection)
|
|
|
|
{
|
|
|
|
$this->db = $connection;
|
|
|
|
}
|
|
|
|
|
2016-08-26 15:20:58 -04:00
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Prepare the local entity cache and ensure it's empty.
|
|
|
|
*
|
2021-03-14 15:52:07 -04:00
|
|
|
* @param Entity[] $entities
|
2016-08-26 15:20:58 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function readyEntityCache(array $entities = [])
|
2016-08-26 15:20:58 -04:00
|
|
|
{
|
2018-04-14 17:17:47 -04:00
|
|
|
$this->entityCache = [];
|
|
|
|
|
|
|
|
foreach ($entities as $entity) {
|
2021-03-14 15:52:07 -04:00
|
|
|
$class = get_class($entity);
|
|
|
|
if (!isset($this->entityCache[$class])) {
|
|
|
|
$this->entityCache[$class] = collect();
|
2018-04-14 17:17:47 -04:00
|
|
|
}
|
2021-03-14 15:52:07 -04:00
|
|
|
$this->entityCache[$class]->put($entity->id, $entity);
|
2018-04-14 17:17:47 -04:00
|
|
|
}
|
2016-08-26 15:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Get a book via ID, Checks local cache.
|
2016-08-26 15:20:58 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function getBook(int $bookId): ?Book
|
2016-08-26 15:20:58 -04:00
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
if (isset($this->entityCache[Book::class]) && $this->entityCache[Book::class]->has($bookId)) {
|
|
|
|
return $this->entityCache[Book::class]->get($bookId);
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|
2016-08-26 15:20:58 -04:00
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
return Book::query()->withTrashed()->find($bookId);
|
2016-08-26 15:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Get a chapter via ID, Checks local cache.
|
2016-08-26 15:20:58 -04:00
|
|
|
*/
|
2021-03-13 10:18:37 -05:00
|
|
|
protected function getChapter(int $chapterId): ?Chapter
|
2016-08-26 15:20:58 -04:00
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
if (isset($this->entityCache[Chapter::class]) && $this->entityCache[Chapter::class]->has($chapterId)) {
|
|
|
|
return $this->entityCache[Chapter::class]->get($chapterId);
|
2016-08-26 15:20:58 -04:00
|
|
|
}
|
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
return Chapter::query()
|
2021-03-13 10:18:37 -05:00
|
|
|
->withTrashed()
|
|
|
|
->find($chapterId);
|
2016-08-26 15:20:58 -04:00
|
|
|
}
|
|
|
|
|
2016-05-01 14:36:53 -04:00
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* Get the roles for the current logged in user.
|
2016-05-01 14:36:53 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function getCurrentUserRoles(): array
|
2016-05-01 14:36:53 -04:00
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
if (!is_null($this->userRoles)) {
|
2018-01-28 11:58:52 -05:00
|
|
|
return $this->userRoles;
|
|
|
|
}
|
2016-05-01 14:36:53 -04:00
|
|
|
|
|
|
|
if (auth()->guest()) {
|
2021-03-14 15:52:07 -04:00
|
|
|
$this->userRoles = [Role::getSystemRole('public')->id];
|
|
|
|
} else {
|
|
|
|
$this->userRoles = $this->currentUser()->roles->pluck('id')->values()->all();
|
2016-05-01 14:36:53 -04:00
|
|
|
}
|
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
return $this->userRoles;
|
2016-05-01 14:36:53 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
JointPermission::query()->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)
|
2021-03-14 15:52:07 -04:00
|
|
|
$roles = Role::query()->with('permissions')->get()->all();
|
2016-04-20 16:37:57 -04:00
|
|
|
|
|
|
|
// Chunk through all books
|
2021-03-14 15:52:07 -04:00
|
|
|
$this->bookFetchQuery()->chunk(5, function (EloquentCollection $books) use ($roles) {
|
2017-04-30 14:10:49 -04:00
|
|
|
$this->buildJointPermissionsForBooks($books, $roles);
|
|
|
|
});
|
2018-09-20 10:27:30 -04:00
|
|
|
|
|
|
|
// Chunk through all bookshelves
|
2021-03-14 15:52:07 -04:00
|
|
|
Bookshelf::query()->withTrashed()->select(['id', 'restricted', 'owned_by'])
|
|
|
|
->chunk(50, function (EloquentCollection $shelves) use ($roles) {
|
2018-09-21 13:48:47 -04:00
|
|
|
$this->buildJointPermissionsForShelves($shelves, $roles);
|
|
|
|
});
|
2017-04-30 14:10:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a query for fetching a book with it's children.
|
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function bookFetchQuery(): Builder
|
2017-04-30 14:10:49 -04:00
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
return Book::query()->withTrashed()
|
|
|
|
->select(['id', 'restricted', 'owned_by'])->with([
|
|
|
|
'chapters' => function ($query) {
|
|
|
|
$query->withTrashed()->select(['id', 'restricted', 'owned_by', 'book_id']);
|
|
|
|
},
|
|
|
|
'pages' => function ($query) {
|
|
|
|
$query->withTrashed()->select(['id', 'restricted', 'owned_by', 'book_id', 'chapter_id']);
|
2021-06-26 11:23:15 -04:00
|
|
|
},
|
2021-03-14 15:52:07 -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
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* Build joint permissions for the given shelf and role combinations.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 15:52:07 -04:00
|
|
|
* @throws Throwable
|
2018-09-20 10:27:30 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function buildJointPermissionsForShelves(EloquentCollection $shelves, array $roles, bool $deleteOld = false)
|
2018-09-20 10:27:30 -04:00
|
|
|
{
|
|
|
|
if ($deleteOld) {
|
|
|
|
$this->deleteManyJointPermissionsForEntities($shelves->all());
|
|
|
|
}
|
2021-03-14 15:52:07 -04:00
|
|
|
$this->createManyJointPermissions($shelves->all(), $roles);
|
2018-09-20 10:27:30 -04:00
|
|
|
}
|
|
|
|
|
2017-04-29 17:01:43 -04:00
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* Build joint permissions for the given book and role combinations.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 15:52:07 -04:00
|
|
|
* @throws Throwable
|
2017-04-29 17:01:43 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function buildJointPermissionsForBooks(EloquentCollection $books, array $roles, bool $deleteOld = false)
|
2018-01-28 11:58:52 -05:00
|
|
|
{
|
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());
|
|
|
|
}
|
2021-03-14 15:52:07 -04:00
|
|
|
$this->createManyJointPermissions($entities->all(), $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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 15:52:07 -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];
|
2021-03-14 15:52:07 -04:00
|
|
|
if ($entity instanceof Book) {
|
2017-04-30 14:10:49 -04:00
|
|
|
$books = $this->bookFetchQuery()->where('id', '=', $entity->id)->get();
|
2021-03-14 15:52:07 -04:00
|
|
|
$this->buildJointPermissionsForBooks($books, Role::query()->get()->all(), true);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2017-04-30 14:10:49 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
/** @var BookChild $entity */
|
2018-08-27 09:18:09 -04:00
|
|
|
if ($entity->book) {
|
|
|
|
$entities[] = $entity->book;
|
|
|
|
}
|
2017-11-19 10:56:06 -05:00
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
if ($entity instanceof Page && $entity->chapter_id) {
|
2017-11-19 10:56:06 -05:00
|
|
|
$entities[] = $entity->chapter;
|
|
|
|
}
|
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
if ($entity instanceof Chapter) {
|
2017-04-30 14:10:49 -04:00
|
|
|
foreach ($entity->pages as $page) {
|
|
|
|
$entities[] = $page;
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 10:56:06 -05:00
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
$this->buildJointPermissionsForEntities($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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 15:52:07 -04:00
|
|
|
* @throws Throwable
|
2016-08-26 15:20:58 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
public function buildJointPermissionsForEntities(array $entities)
|
2016-08-26 15:20:58 -04:00
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
$roles = Role::query()->get()->values()->all();
|
|
|
|
$this->deleteManyJointPermissionsForEntities($entities);
|
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
|
|
|
*/
|
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
|
2021-03-14 15:52:07 -04:00
|
|
|
Bookshelf::query()->select(['id', 'restricted', 'owned_by'])
|
2018-09-20 10:27:30 -04:00
|
|
|
->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
|
|
|
*/
|
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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
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);
|
2021-03-14 15:52:07 -04:00
|
|
|
JointPermission::query()->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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param Entity $entity
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 15:52:07 -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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 15:52:07 -04:00
|
|
|
* @param Entity[] $entities
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 15:52:07 -04:00
|
|
|
* @throws Throwable
|
2016-04-24 11:54:20 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function deleteManyJointPermissionsForEntities(array $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
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* Create & Save entity jointPermissions for many entities and roles.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 15:52:07 -04:00
|
|
|
* @param Entity[] $entities
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param Role[] $roles
|
|
|
|
*
|
2021-03-14 15:52:07 -04:00
|
|
|
* @throws Throwable
|
2016-04-20 16:37:57 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function createManyJointPermissions(array $entities, array $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 = [];
|
2021-03-14 15:52:07 -04:00
|
|
|
$permissionFetch = EntityPermission::query();
|
2017-04-29 17:01:43 -04:00
|
|
|
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.
|
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function getActions(Entity $entity): array
|
2016-04-26 16:48:17 -04:00
|
|
|
{
|
|
|
|
$baseActions = ['view', 'update', 'delete'];
|
2021-03-14 15:52:07 -04:00
|
|
|
if ($entity instanceof Chapter || $entity instanceof Book) {
|
2018-01-28 11:58:52 -05:00
|
|
|
$baseActions[] = 'page-create';
|
|
|
|
}
|
2021-03-14 15:52:07 -04:00
|
|
|
if ($entity instanceof Book) {
|
2018-01-28 11:58:52 -05:00
|
|
|
$baseActions[] = 'chapter-create';
|
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
|
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.
|
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function createJointPermissionData(Entity $entity, Role $role, string $action, array $permissionMap, array $rolePermissionMap): array
|
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);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2017-04-29 17:01:43 -04:00
|
|
|
return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
|
|
|
|
}
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
if ($entity instanceof Book || $entity instanceof 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
|
2021-03-13 10:18:37 -05:00
|
|
|
if ($entity instanceof Page && intval($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.
|
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function mapHasActiveRestriction(array $entityMap, Entity $entity, Role $role, string $action): bool
|
2018-01-28 11:58:52 -05:00
|
|
|
{
|
2017-04-29 17:01:43 -04:00
|
|
|
$key = $entity->getMorphClass() . ':' . $entity->getRawAttribute('id') . ':' . $role->getRawAttribute('id') . ':' . $action;
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
return $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.
|
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function createJointPermissionDataArray(Entity $entity, Role $role, string $action, bool $permissionAll, bool $permissionOwn): array
|
2016-04-23 13:14:26 -04:00
|
|
|
{
|
|
|
|
return [
|
2021-06-26 11:23:15 -04:00
|
|
|
'role_id' => $role->getRawAttribute('id'),
|
|
|
|
'entity_id' => $entity->getRawAttribute('id'),
|
|
|
|
'entity_type' => $entity->getMorphClass(),
|
|
|
|
'action' => $action,
|
|
|
|
'has_permission' => $permissionAll,
|
2016-04-23 13:14:26 -04:00
|
|
|
'has_permission_own' => $permissionOwn,
|
2021-06-26 11:23:15 -04:00
|
|
|
'owned_by' => $entity->getRawAttribute('owned_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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2020-12-30 13:25:35 -05:00
|
|
|
* @param HasCreatorAndUpdater|HasOwner $ownable
|
2016-03-05 13:09:21 -05:00
|
|
|
*/
|
2020-12-30 13:25:35 -05:00
|
|
|
public function checkOwnableUserAccess(Model $ownable, string $permission): bool
|
2016-02-29 15:31:21 -05:00
|
|
|
{
|
2016-04-24 11:54:20 -04:00
|
|
|
$explodedPermission = explode('-', $permission);
|
|
|
|
|
2020-12-30 13:25:35 -05:00
|
|
|
$baseQuery = $ownable->newQuery()->where('id', '=', $ownable->id);
|
2016-04-26 16:48:17 -04:00
|
|
|
$action = end($explodedPermission);
|
2021-03-14 15:52:07 -04:00
|
|
|
$user = $this->currentUser();
|
2016-04-24 11:54:20 -04:00
|
|
|
|
2020-12-30 17:18:28 -05:00
|
|
|
$nonJointPermissions = ['restrictions', 'image', 'attachment', 'comment'];
|
|
|
|
|
2016-05-01 16:20:50 -04:00
|
|
|
// Handle non entity specific jointPermissions
|
2020-12-30 17:18:28 -05:00
|
|
|
if (in_array($explodedPermission[0], $nonJointPermissions)) {
|
2021-03-14 15:52:07 -04:00
|
|
|
$allPermission = $user && $user->can($permission . '-all');
|
|
|
|
$ownPermission = $user && $user->can($permission . '-own');
|
2021-01-04 13:07:39 -05:00
|
|
|
$ownerField = ($ownable instanceof Entity) ? 'owned_by' : 'created_by';
|
2021-03-14 15:52:07 -04:00
|
|
|
$isOwner = $user && $user->id === $ownable->$ownerField;
|
2021-06-26 11:23:15 -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') {
|
2021-03-14 15:52:07 -04:00
|
|
|
$action = $permission;
|
2016-04-26 16:48:17 -04:00
|
|
|
}
|
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
$hasAccess = $this->entityRestrictionQuery($baseQuery, $action)->count() > 0;
|
2016-09-17 13:22:04 -04:00
|
|
|
$this->clean();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
return $hasAccess;
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 00:55:28 -05:00
|
|
|
/**
|
2019-03-09 11:50:22 -05:00
|
|
|
* Checks if a user has the given permission for any items in the system.
|
2019-03-09 16:15:45 -05:00
|
|
|
* Can be passed an entity instance to filter on a specific type.
|
2019-01-02 00:55:28 -05:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
public function checkUserHasPermissionOnAnything(string $permission, ?string $entityClass = null): bool
|
2019-01-02 00:55:28 -05:00
|
|
|
{
|
2019-03-09 11:50:22 -05:00
|
|
|
$userRoleIds = $this->currentUser()->roles()->select('id')->pluck('id')->toArray();
|
2019-01-02 00:55:28 -05:00
|
|
|
$userId = $this->currentUser()->id;
|
|
|
|
|
2021-03-14 16:32:33 -04:00
|
|
|
$permissionQuery = JointPermission::query()
|
2019-03-09 11:50:22 -05:00
|
|
|
->where('action', '=', $permission)
|
2019-01-02 00:55:28 -05:00
|
|
|
->whereIn('role_id', $userRoleIds)
|
2021-03-14 16:32:33 -04:00
|
|
|
->where(function (Builder $query) use ($userId) {
|
|
|
|
$this->addJointHasPermissionCheck($query, $userId);
|
2019-04-03 05:26:24 -04:00
|
|
|
});
|
2019-03-09 16:15:45 -05:00
|
|
|
|
|
|
|
if (!is_null($entityClass)) {
|
2021-03-14 16:32:33 -04:00
|
|
|
$entityInstance = app($entityClass);
|
2019-03-09 16:15:45 -05:00
|
|
|
$permissionQuery = $permissionQuery->where('entity_type', '=', $entityInstance->getMorphClass());
|
|
|
|
}
|
2019-01-02 00:55:28 -05:00
|
|
|
|
2019-03-09 16:15:45 -05:00
|
|
|
$hasPermission = $permissionQuery->count() > 0;
|
2019-03-09 11:50:22 -05:00
|
|
|
$this->clean();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-03-09 16:15:45 -05:00
|
|
|
return $hasPermission;
|
2019-01-02 00:55:28 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
protected function entityRestrictionQuery(Builder $query, string $action): Builder
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
$q = $query->where(function ($parentQuery) use ($action) {
|
|
|
|
$parentQuery->whereHas('jointPermissions', function ($permissionQuery) use ($action) {
|
|
|
|
$permissionQuery->whereIn('role_id', $this->getCurrentUserRoles())
|
|
|
|
->where('action', '=', $action)
|
2021-03-14 16:32:33 -04:00
|
|
|
->where(function (Builder $query) {
|
|
|
|
$this->addJointHasPermissionCheck($query, $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
|
|
|
});
|
2021-03-14 15:52:07 -04:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
$this->clean();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
return $q;
|
2016-02-28 14:03:04 -05:00
|
|
|
}
|
|
|
|
|
2017-01-15 10:00:29 -05:00
|
|
|
/**
|
2019-10-05 07:55:01 -04:00
|
|
|
* Limited the given entity query so that the query will only
|
|
|
|
* return items that the user has permission for the given ability.
|
2017-01-15 10:00:29 -05:00
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function restrictEntityQuery(Builder $query, string $ability = 'view'): Builder
|
2018-01-28 11:58:52 -05:00
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->clean();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return $query->where(function (Builder $parentQuery) use ($ability) {
|
|
|
|
$parentQuery->whereHas('jointPermissions', function (Builder $permissionQuery) use ($ability) {
|
2021-03-14 15:52:07 -04:00
|
|
|
$permissionQuery->whereIn('role_id', $this->getCurrentUserRoles())
|
2019-10-05 07:55:01 -04:00
|
|
|
->where('action', '=', $ability)
|
|
|
|
->where(function (Builder $query) {
|
2021-03-14 16:32:33 -04:00
|
|
|
$this->addJointHasPermissionCheck($query, $this->currentUser()->id);
|
2019-01-27 05:29:23 -05:00
|
|
|
});
|
2018-09-20 14:48:08 -04:00
|
|
|
});
|
2019-10-05 07:55:01 -04:00
|
|
|
});
|
|
|
|
}
|
2017-01-22 07:02:30 -05:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
/**
|
|
|
|
* Extend the given page query to ensure draft items are not visible
|
|
|
|
* unless created by the given user.
|
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
public function enforceDraftVisibilityOnQuery(Builder $query): Builder
|
2019-10-05 07:55:01 -04:00
|
|
|
{
|
|
|
|
return $query->where(function (Builder $query) {
|
|
|
|
$query->where('draft', '=', false)
|
|
|
|
->orWhere(function (Builder $query) {
|
|
|
|
$query->where('draft', '=', true)
|
2020-12-30 13:25:35 -05:00
|
|
|
->where('owned_by', '=', $this->currentUser()->id);
|
2019-10-05 07:55:01 -04:00
|
|
|
});
|
|
|
|
});
|
2017-01-01 16:21:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-06 15:33:08 -04:00
|
|
|
/**
|
2021-03-14 15:52:07 -04:00
|
|
|
* Add restrictions for a generic entity.
|
2016-05-06 15:33:08 -04:00
|
|
|
*/
|
2021-03-14 16:32:33 -04:00
|
|
|
public function enforceEntityRestrictions(Entity $entity, Builder $query, string $action = 'view'): Builder
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
2021-03-14 16:32:33 -04:00
|
|
|
if ($entity instanceof Page) {
|
2017-01-01 11:05:44 -05:00
|
|
|
// Prevent drafts being visible to others.
|
2021-03-14 16:32:33 -04:00
|
|
|
$this->enforceDraftVisibilityOnQuery($query);
|
2017-01-01 11:05:44 -05:00
|
|
|
}
|
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
return $this->entityRestrictionQuery($query, $action);
|
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.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-05-19 18:37:23 -04:00
|
|
|
* @param Builder|\Illuminate\Database\Query\Builder $query
|
2016-02-28 14:03:04 -05:00
|
|
|
*/
|
2021-05-19 18:37:23 -04:00
|
|
|
public function filterRestrictedEntityRelations($query, string $tableName, string $entityIdColumn, string $entityTypeColumn, string $action = 'view')
|
2016-02-28 14:03:04 -05:00
|
|
|
{
|
2021-09-19 08:28:57 -04:00
|
|
|
$tableDetails = ['tableName' => $this->db->getTablePrefix() . $tableName, 'entityIdColumn' => $entityIdColumn, 'entityTypeColumn' => $entityTypeColumn];
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
$q = $query->where(function ($query) use ($tableDetails, $action) {
|
|
|
|
$query->whereExists(function ($permissionQuery) use (&$tableDetails, $action) {
|
2021-05-23 09:34:36 -04:00
|
|
|
$permissionQuery->select(['role_id'])->from('joint_permissions')
|
2021-09-19 08:28:57 -04:00
|
|
|
->whereRaw($this->db->getTablePrefix() . 'joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
|
|
|
|
->whereRaw($this->db->getTablePrefix() . 'joint_permissions.entity_type=' . $tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'])
|
2021-03-14 15:52:07 -04:00
|
|
|
->where('action', '=', $action)
|
|
|
|
->whereIn('role_id', $this->getCurrentUserRoles())
|
2021-03-14 16:32:33 -04:00
|
|
|
->where(function (QueryBuilder $query) {
|
|
|
|
$this->addJointHasPermissionCheck($query, $this->currentUser()->id);
|
2016-04-23 13:14:26 -04:00
|
|
|
});
|
2016-02-28 14:03:04 -05:00
|
|
|
});
|
|
|
|
});
|
2021-03-14 15:52:07 -04:00
|
|
|
|
2017-01-21 11:16:27 -05:00
|
|
|
$this->clean();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
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
|
|
|
/**
|
2019-04-27 09:18:00 -04:00
|
|
|
* Add conditions to a query to filter the selection to related entities
|
2021-03-14 15:52:07 -04:00
|
|
|
* where view permissions are granted.
|
2016-03-13 09:30:47 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
public function filterRelatedEntity(string $entityClass, Builder $query, string $tableName, string $entityIdColumn): Builder
|
2016-03-13 09:30:47 -04:00
|
|
|
{
|
|
|
|
$tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn];
|
2021-03-14 15:52:07 -04:00
|
|
|
$morphClass = app($entityClass)->getMorphClass();
|
2016-04-23 13:14:26 -04:00
|
|
|
|
2021-03-14 15:52:07 -04:00
|
|
|
$q = $query->where(function ($query) use ($tableDetails, $morphClass) {
|
|
|
|
$query->where(function ($query) use (&$tableDetails, $morphClass) {
|
|
|
|
$query->whereExists(function ($permissionQuery) use (&$tableDetails, $morphClass) {
|
2016-05-01 16:20:50 -04:00
|
|
|
$permissionQuery->select('id')->from('joint_permissions')
|
2021-09-19 08:28:57 -04:00
|
|
|
->whereRaw($this->db->getTablePrefix() . 'joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
|
2021-03-14 15:52:07 -04:00
|
|
|
->where('entity_type', '=', $morphClass)
|
|
|
|
->where('action', '=', 'view')
|
|
|
|
->whereIn('role_id', $this->getCurrentUserRoles())
|
2021-03-14 16:32:33 -04:00
|
|
|
->where(function (QueryBuilder $query) {
|
|
|
|
$this->addJointHasPermissionCheck($query, $this->currentUser()->id);
|
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
|
|
|
});
|
2019-04-27 09:18:00 -04:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
$this->clean();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
return $q;
|
|
|
|
}
|
|
|
|
|
2021-03-14 16:32:33 -04:00
|
|
|
/**
|
|
|
|
* Add the query for checking the given user id has permission
|
|
|
|
* within the join_permissions table.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-14 16:32:33 -04:00
|
|
|
* @param QueryBuilder|Builder $query
|
|
|
|
*/
|
|
|
|
protected function addJointHasPermissionCheck($query, int $userIdToCheck)
|
|
|
|
{
|
|
|
|
$query->where('has_permission', '=', true)->orWhere(function ($query) use ($userIdToCheck) {
|
|
|
|
$query->where('has_permission_own', '=', true)
|
|
|
|
->where('owned_by', '=', $userIdToCheck);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Get the current user.
|
2016-09-17 13:22:04 -04:00
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
private function currentUser(): User
|
2016-09-17 13:22:04 -04:00
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
if (is_null($this->currentUserModel)) {
|
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.
|
|
|
|
*/
|
2021-03-14 15:52:07 -04:00
|
|
|
private function clean(): void
|
2016-09-17 13:22:04 -04:00
|
|
|
{
|
2021-03-14 15:52:07 -04:00
|
|
|
$this->currentUserModel = null;
|
|
|
|
$this->userRoles = null;
|
2016-03-13 09:30:47 -04:00
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|