use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\JoinClause;
+use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
class PermissionApplicator
*/
public function restrictEntityQuery(Builder $query, string $morphClass): Builder
{
- // TODO - Leave this as the new admin workaround?
- // Or auto generate collapsed role permissions for admins?
- if (\user()->hasSystemRole('admin')) {
- return $query;
- }
-
$this->applyPermissionsToQuery($query, $query->getModel()->getTable(), $morphClass, 'id', '');
return $query;
/**
* @param Builder|QueryBuilder $query
- * @return void
*/
- protected function applyPermissionsToQuery($query, string $queryTable, string $entityTypeLimiter, string $entityIdColumn, string $entityTypeColumn)
+ protected function applyPermissionsToQuery($query, string $queryTable, string $entityTypeLimiter, string $entityIdColumn, string $entityTypeColumn): void
{
+ if ($this->currentUser()->hasSystemRole('admin')) {
+ return;
+ }
+
$this->applyFallbackJoin($query, $queryTable, $entityTypeLimiter, $entityIdColumn, $entityTypeColumn);
$this->applyRoleJoin($query, $queryTable, $entityTypeLimiter, $entityIdColumn, $entityTypeColumn);
$this->applyUserJoin($query, $queryTable, $entityTypeLimiter, $entityIdColumn, $entityTypeColumn);
* Both should not be applied since that would conflict upon intent.
* @param Builder|QueryBuilder $query
*/
- protected function applyPermissionWhereFilter($query, string $entityTypeLimiter, string $entityTypeColumn)
+ protected function applyPermissionWhereFilter($query, string $queryTable, string $entityTypeLimiter, string $entityTypeColumn)
{
$abilities = ['all' => [], 'own' => []];
$types = $entityTypeLimiter ? [$entityTypeLimiter] : ['page', 'chapter', 'bookshelf', 'book'];
+ $fullEntityTypeColumn = $queryTable . '.' . $entityTypeColumn;
foreach ($types as $type) {
$abilities['all'][$type] = userCan($type . '-view-all');
$abilities['own'][$type] = userCan($type . '-view-own');
$abilities['all'] = array_filter($abilities['all']);
$abilities['own'] = array_filter($abilities['own']);
- $query->where(function (Builder $query) use ($abilities, $entityTypeColumn) {
+ $query->where(function (Builder $query) use ($abilities, $fullEntityTypeColumn, $entityTypeColumn) {
$query->where('perms_user', '=', 1)
->orWhere(function (Builder $query) {
$query->whereNull('perms_user')->where('perms_role', '=', 1);
});
if (count($abilities['all']) > 0) {
- $query->orWhere(function (Builder $query) use ($abilities, $entityTypeColumn) {
+ $query->orWhere(function (Builder $query) use ($abilities, $fullEntityTypeColumn, $entityTypeColumn) {
$query->whereNull(['perms_user', 'perms_role', 'perms_fallback']);
if ($entityTypeColumn) {
- $query->whereIn($entityTypeColumn, array_keys($abilities['all']));
+ $query->whereIn($fullEntityTypeColumn, array_keys($abilities['all']));
}
});
}
if (count($abilities['own']) > 0) {
- $query->orWhere(function (Builder $query) use ($abilities, $entityTypeColumn) {
+ $query->orWhere(function (Builder $query) use ($abilities, $fullEntityTypeColumn, $entityTypeColumn) {
$query->whereNull(['perms_user', 'perms_role', 'perms_fallback'])
->where('owned_by', '=', $this->currentUser()->id);
if ($entityTypeColumn) {
- $query->whereIn($entityTypeColumn, array_keys($abilities['all']));
+ $query->whereIn($fullEntityTypeColumn, array_keys($abilities['all']));
}
});
}
*/
public function restrictEntityRelationQuery($query, string $tableName, string $entityIdColumn, string $entityTypeColumn)
{
+ $query->leftJoinSub(function (QueryBuilder $query) {
+ $query->select(['id as entity_id', DB::raw("'page' as entity_type"), 'owned_by', 'deleted_at', 'draft'])->from('pages');
+ $tablesByType = ['page' => 'pages', 'book' => 'books', 'chapter' => 'chapters', 'bookshelf' => 'bookshelves'];
+ foreach ($tablesByType as $type => $table) {
+ $query->unionAll(function (QueryBuilder $query) use ($type, $table) {
+ $query->select(['id as entity_id', DB::raw("'{$type}' as entity_type"), 'owned_by', 'deleted_at', DB::raw('0 as draft')])->from($table);
+ });
+ }
+ }, 'entities', function (JoinClause $join) use ($tableName, $entityIdColumn, $entityTypeColumn) {
+ $join->on($tableName . '.' . $entityIdColumn, '=', 'entities.entity_id')
+ ->on($tableName . '.' . $entityTypeColumn, '=', 'entities.entity_type');
+ });
+
$this->applyPermissionsToQuery($query, $tableName, '', $entityIdColumn, $entityTypeColumn);
// TODO - Test page draft access (Might allow drafts which should not be seen)
- // TODO - Test each use of this to check column/relation fetching.
- // Original queries might need selects applied to limit field exposure and to get right original table columns.
return $query;
}
*/
public function restrictPageRelationQuery(Builder $query, string $tableName, string $pageIdColumn): Builder
{
- $fullPageIdColumn = $tableName . '.' . $pageIdColumn;
$morphClass = (new Page())->getMorphClass();
- // TODO
+ $this->applyPermissionsToQuery($query, $tableName, $morphClass, $pageIdColumn, '');
+ // TODO - Draft display
+ // TODO - Likely need owned_by entity join workaround as used above
return $query;
-
- $existsQuery = function ($permissionQuery) use ($fullPageIdColumn, $morphClass) {
- /** @var Builder $permissionQuery */
- $permissionQuery->select('joint_permissions.role_id')->from('joint_permissions')
- ->whereColumn('joint_permissions.entity_id', '=', $fullPageIdColumn)
- ->where('joint_permissions.entity_type', '=', $morphClass)
- ->whereIn('joint_permissions.role_id', $this->getCurrentUserRoleIds())
- ->where(function (QueryBuilder $query) {
- $this->addJointHasPermissionCheck($query, $this->currentUser()->id);
- });
- };
-
- $userExistsQuery = function ($hasPermission) use ($fullPageIdColumn, $morphClass) {
- return function ($permissionQuery) use ($fullPageIdColumn, $morphClass) {
- /** @var Builder $permissionQuery */
- $permissionQuery->select('joint_user_permissions.user_id')->from('joint_user_permissions')
- ->whereColumn('joint_user_permissions.entity_id', '=', $fullPageIdColumn)
- ->where('joint_user_permissions.entity_type', '=', $morphClass)
- ->where('joint_user_permissions.user_id', $this->currentUser()->id)
- ->where('has_permission', '=', true);
- };
- };
-
- $q = $query->where(function ($query) use ($existsQuery, $userExistsQuery, $fullPageIdColumn) {
- $query->whereExists($existsQuery)
- ->orWhereExists($userExistsQuery(true))
- ->orWhere($fullPageIdColumn, '=', 0);
- })->whereNotExists($userExistsQuery(false));
-
- // Prevent visibility of non-owned draft pages
- $q->whereExists(function (QueryBuilder $query) use ($fullPageIdColumn) {
- $query->select('id')->from('pages')
- ->whereColumn('pages.id', '=', $fullPageIdColumn)
- ->where(function (QueryBuilder $query) {
- $query->where('pages.draft', '=', false)
- ->orWhere('pages.owned_by', '=', $this->currentUser()->id);
- });
- });
-
- return $q;
- }
-
- /**
- * Add the query for checking the given user id has permission
- * within the join_permissions table.
- *
- * @param QueryBuilder|Builder $query
- */
- protected function addJointHasPermissionCheck($query, int $userIdToCheck)
- {
- $query->where('joint_permissions.has_permission', '=', true)->orWhere(function ($query) use ($userIdToCheck) {
- $query->where('joint_permissions.has_permission_own', '=', true)
- ->where('joint_permissions.owned_by', '=', $userIdToCheck);
- });
}
/**