@@ -40,7 +40,7 @@ public function rebuildForAll()
40
40
});
41
41
42
42
// Chunk through all bookshelves
43
- Bookshelf::query ()->withTrashed ()->select (['id ' , 'restricted ' , ' owned_by ' ])
43
+ Bookshelf::query ()->withTrashed ()->select (['id ' , 'owned_by ' ])
44
44
->chunk (50 , function (EloquentCollection $ shelves ) use ($ roles ) {
45
45
$ this ->createManyJointPermissions ($ shelves ->all (), $ roles );
46
46
});
@@ -92,7 +92,7 @@ public function rebuildForRole(Role $role)
92
92
});
93
93
94
94
// Chunk through all bookshelves
95
- Bookshelf::query ()->select (['id ' , 'restricted ' , ' owned_by ' ])
95
+ Bookshelf::query ()->select (['id ' , 'owned_by ' ])
96
96
->chunk (50 , function ($ shelves ) use ($ roles ) {
97
97
$ this ->createManyJointPermissions ($ shelves ->all (), $ roles );
98
98
});
@@ -138,12 +138,11 @@ protected function getChapter(int $chapterId): SimpleEntityData
138
138
protected function bookFetchQuery (): Builder
139
139
{
140
140
return Book::query ()->withTrashed ()
141
- ->select (['id ' , 'restricted ' , ' owned_by ' ])->with ([
141
+ ->select (['id ' , 'owned_by ' ])->with ([
142
142
'chapters ' => function ($ query ) {
143
- $ query ->withTrashed ()->select (['id ' , 'restricted ' , 'owned_by ' , 'book_id ' ]);
144
143
},
145
144
'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 ' ]);
147
146
},
148
147
]);
149
148
}
@@ -218,7 +217,6 @@ protected function entitiesToSimpleEntities(array $entities): array
218
217
$ simple = new SimpleEntityData ();
219
218
$ simple ->id = $ attrs ['id ' ];
220
219
$ simple ->type = $ entity ->getMorphClass ();
221
- $ simple ->restricted = boolval ($ attrs ['restricted ' ] ?? 0 );
222
220
$ simple ->owned_by = $ attrs ['owned_by ' ] ?? 0 ;
223
221
$ simple ->book_id = $ attrs ['book_id ' ] ?? null ;
224
222
$ simple ->chapter_id = $ attrs ['chapter_id ' ] ?? null ;
@@ -240,24 +238,14 @@ protected function createManyJointPermissions(array $originalEntities, array $ro
240
238
$ this ->readyEntityCache ($ entities );
241
239
$ jointPermissions = [];
242
240
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
-
249
241
// Fetch related entity permissions
250
242
$ permissions = $ this ->getEntityPermissionsForEntities ($ entities );
251
243
252
244
// 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.
256
245
$ permissionMap = [];
257
246
foreach ($ permissions as $ permission ) {
258
247
$ 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 ;
261
249
}
262
250
263
251
// Create a mapping of role permissions
@@ -347,7 +335,7 @@ protected function createJointPermissionData(SimpleEntityData $entity, int $role
347
335
return $ this ->createJointPermissionDataArray ($ entity , $ roleId , true , true );
348
336
}
349
337
350
- if ($ entity -> restricted ) {
338
+ if ($ this -> entityPermissionsActiveForRole ( $ permissionMap , $ entity , $ roleId ) ) {
351
339
$ hasAccess = $ this ->mapHasActiveRestriction ($ permissionMap , $ entity , $ roleId );
352
340
353
341
return $ this ->createJointPermissionDataArray ($ entity , $ roleId , $ hasAccess , $ hasAccess );
@@ -360,13 +348,14 @@ protected function createJointPermissionData(SimpleEntityData $entity, int $role
360
348
// For chapters and pages, Check if explicit permissions are set on the Book.
361
349
$ book = $ this ->getBook ($ entity ->book_id );
362
350
$ hasExplicitAccessToParents = $ this ->mapHasActiveRestriction ($ permissionMap , $ book , $ roleId );
363
- $ hasPermissiveAccessToParents = !$ book -> restricted ;
351
+ $ hasPermissiveAccessToParents = !$ this -> entityPermissionsActiveForRole ( $ permissionMap , $ book , $ roleId ) ;
364
352
365
353
// For pages with a chapter, Check if explicit permissions are set on the Chapter
366
354
if ($ entity ->type === 'page ' && $ entity ->chapter_id !== 0 ) {
367
355
$ 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 ) {
370
359
$ hasExplicitAccessToParents = $ this ->mapHasActiveRestriction ($ permissionMap , $ chapter , $ roleId );
371
360
}
372
361
}
@@ -379,14 +368,25 @@ protected function createJointPermissionData(SimpleEntityData $entity, int $role
379
368
);
380
369
}
381
370
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
+
382
381
/**
383
382
* Check for an active restriction in an entity map.
384
383
*/
385
384
protected function mapHasActiveRestriction (array $ entityMap , SimpleEntityData $ entity , int $ roleId ): bool
386
385
{
387
- $ key = $ entity ->type . ': ' . $ entity ->id . ': ' . $ roleId ;
386
+ $ roleKey = $ entity ->type . ': ' . $ entity ->id . ': ' . $ roleId ;
387
+ $ defaultKey = $ entity ->type . ': ' . $ entity ->id . ':0 ' ;
388
388
389
- return $ entityMap [$ key ] ?? false ;
389
+ return $ entityMap [$ roleKey ] ?? $ entityMap [ $ defaultKey ] ?? false ;
390
390
}
391
391
392
392
/**
0 commit comments