Skip to content

Commit 3839bf6

Browse files
committed
Updated joint perms. gen. to use new entity permission format
1 parent aee0e16 commit 3839bf6

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

app/Auth/Permissions/JointPermissionBuilder.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function rebuildForAll()
4040
});
4141

4242
// Chunk through all bookshelves
43-
Bookshelf::query()->withTrashed()->select(['id', 'restricted', 'owned_by'])
43+
Bookshelf::query()->withTrashed()->select(['id', 'owned_by'])
4444
->chunk(50, function (EloquentCollection $shelves) use ($roles) {
4545
$this->createManyJointPermissions($shelves->all(), $roles);
4646
});
@@ -92,7 +92,7 @@ public function rebuildForRole(Role $role)
9292
});
9393

9494
// Chunk through all bookshelves
95-
Bookshelf::query()->select(['id', 'restricted', 'owned_by'])
95+
Bookshelf::query()->select(['id', 'owned_by'])
9696
->chunk(50, function ($shelves) use ($roles) {
9797
$this->createManyJointPermissions($shelves->all(), $roles);
9898
});
@@ -138,12 +138,11 @@ protected function getChapter(int $chapterId): SimpleEntityData
138138
protected function bookFetchQuery(): Builder
139139
{
140140
return Book::query()->withTrashed()
141-
->select(['id', 'restricted', 'owned_by'])->with([
141+
->select(['id', 'owned_by'])->with([
142142
'chapters' => function ($query) {
143-
$query->withTrashed()->select(['id', 'restricted', 'owned_by', 'book_id']);
144143
},
145144
'pages' => function ($query) {
146-
$query->withTrashed()->select(['id', 'restricted', 'owned_by', 'book_id', 'chapter_id']);
145+
$query->withTrashed()->select(['id', 'owned_by', 'book_id', 'chapter_id']);
147146
},
148147
]);
149148
}
@@ -218,7 +217,6 @@ protected function entitiesToSimpleEntities(array $entities): array
218217
$simple = new SimpleEntityData();
219218
$simple->id = $attrs['id'];
220219
$simple->type = $entity->getMorphClass();
221-
$simple->restricted = boolval($attrs['restricted'] ?? 0);
222220
$simple->owned_by = $attrs['owned_by'] ?? 0;
223221
$simple->book_id = $attrs['book_id'] ?? null;
224222
$simple->chapter_id = $attrs['chapter_id'] ?? null;
@@ -240,24 +238,14 @@ protected function createManyJointPermissions(array $originalEntities, array $ro
240238
$this->readyEntityCache($entities);
241239
$jointPermissions = [];
242240

243-
// Create a mapping of entity restricted statuses
244-
$entityRestrictedMap = [];
245-
foreach ($entities as $entity) {
246-
$entityRestrictedMap[$entity->type . ':' . $entity->id] = $entity->restricted;
247-
}
248-
249241
// Fetch related entity permissions
250242
$permissions = $this->getEntityPermissionsForEntities($entities);
251243

252244
// Create a mapping of explicit entity permissions
253-
// TODO - Handle new format, Now getting all defined entity permissions
254-
// from the above call, Need to handle entries with none, and the 'Other Roles' (role_id=0)
255-
// fallback option.
256245
$permissionMap = [];
257246
foreach ($permissions as $permission) {
258247
$key = $permission->entity_type . ':' . $permission->entity_id . ':' . $permission->role_id;
259-
$isRestricted = $entityRestrictedMap[$permission->entity_type . ':' . $permission->entity_id];
260-
$permissionMap[$key] = $isRestricted;
248+
$permissionMap[$key] = $permission->view;
261249
}
262250

263251
// Create a mapping of role permissions
@@ -347,7 +335,7 @@ protected function createJointPermissionData(SimpleEntityData $entity, int $role
347335
return $this->createJointPermissionDataArray($entity, $roleId, true, true);
348336
}
349337

350-
if ($entity->restricted) {
338+
if ($this->entityPermissionsActiveForRole($permissionMap, $entity, $roleId)) {
351339
$hasAccess = $this->mapHasActiveRestriction($permissionMap, $entity, $roleId);
352340

353341
return $this->createJointPermissionDataArray($entity, $roleId, $hasAccess, $hasAccess);
@@ -360,13 +348,14 @@ protected function createJointPermissionData(SimpleEntityData $entity, int $role
360348
// For chapters and pages, Check if explicit permissions are set on the Book.
361349
$book = $this->getBook($entity->book_id);
362350
$hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $book, $roleId);
363-
$hasPermissiveAccessToParents = !$book->restricted;
351+
$hasPermissiveAccessToParents = !$this->entityPermissionsActiveForRole($permissionMap, $book, $roleId);
364352

365353
// For pages with a chapter, Check if explicit permissions are set on the Chapter
366354
if ($entity->type === 'page' && $entity->chapter_id !== 0) {
367355
$chapter = $this->getChapter($entity->chapter_id);
368-
$hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapter->restricted;
369-
if ($chapter->restricted) {
356+
$chapterRestricted = $this->entityPermissionsActiveForRole($permissionMap, $chapter, $roleId);
357+
$hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapterRestricted;
358+
if ($chapterRestricted) {
370359
$hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $chapter, $roleId);
371360
}
372361
}
@@ -379,14 +368,25 @@ protected function createJointPermissionData(SimpleEntityData $entity, int $role
379368
);
380369
}
381370

371+
/**
372+
* Check if entity permissions are defined within the given map, for the given entity and role.
373+
* Checks for the default `role_id=0` backup option as a fallback.
374+
*/
375+
protected function entityPermissionsActiveForRole(array $permissionMap, SimpleEntityData $entity, int $roleId): bool
376+
{
377+
$keyPrefix = $entity->type . ':' . $entity->id . ':';
378+
return isset($permissionMap[$keyPrefix . $roleId]) || isset($permissionMap[$keyPrefix . '0']);
379+
}
380+
382381
/**
383382
* Check for an active restriction in an entity map.
384383
*/
385384
protected function mapHasActiveRestriction(array $entityMap, SimpleEntityData $entity, int $roleId): bool
386385
{
387-
$key = $entity->type . ':' . $entity->id . ':' . $roleId;
386+
$roleKey = $entity->type . ':' . $entity->id . ':' . $roleId;
387+
$defaultKey = $entity->type . ':' . $entity->id . ':0';
388388

389-
return $entityMap[$key] ?? false;
389+
return $entityMap[$roleKey] ?? $entityMap[$defaultKey] ?? false;
390390
}
391391

392392
/**

app/Auth/Permissions/SimpleEntityData.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class SimpleEntityData
66
{
77
public int $id;
88
public string $type;
9-
public bool $restricted;
109
public int $owned_by;
1110
public ?int $book_id;
1211
public ?int $chapter_id;

0 commit comments

Comments
 (0)