+ /**
+ * Get the status of a user-specific permission override for the given entity user combo if existing.
+ * This can return null where no user-specific permission overrides are applicable.
+ */
+ protected function getUserPermissionOverrideStatus(SimpleEntityData $entity, int $userId, array $permissionMap): ?bool
+ {
+ // If direct permissions exists, return those
+ $directKey = $entity->type . ':' . $entity->id . ':user:' . $userId;
+ if (isset($permissionMap[$directKey])) {
+ return $permissionMap[$directKey];
+ }
+
+ // If a book or shelf, exit out since no parents to check
+ if ($entity->type === 'book' || $entity->type === 'bookshelf') {
+ return null;
+ }
+
+ // If a chapter or page, get the parent book permission status.
+ // defaults to null where no permission is set.
+ $bookKey = 'book:' . $entity->book_id . ':user:' . $userId;
+ $bookPermission = $permissionMap[$bookKey] ?? null;
+
+ // If a page within a chapter, return the chapter permission if existing otherwise
+ // default ot the parent book permission.
+ if ($entity->type === 'page' && $entity->chapter_id !== 0) {
+ $chapterKey = 'chapter:' . $entity->chapter_id . ':user:' . $userId;
+ $chapterPermission = $permissionMap[$chapterKey] ?? null;
+ return $chapterPermission ?? $bookPermission;
+ }
+
+ // Return the book permission status
+ return $bookPermission;
+ }
+