| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Auth\Permissions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Auth\Role; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\BookChild; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Page; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Collection as EloquentCollection; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Joint permissions provide a pre-query "cached" table of view permissions for all core entity | 
					
						
							|  |  |  |  * types for all roles in the system. This class generates out that table for different scenarios. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  | class JointPermissionBuilder | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |      * @var array<string, array<int, SimpleEntityData>> | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-10-24 19:12:48 +08:00
										 |  |  |     protected array $entityCache; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 03:15:41 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Re-generate all entity permission from scratch. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function rebuildForAll() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         JointPermission::query()->truncate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Get all roles (Should be the most limited dimension)
 | 
					
						
							|  |  |  |         $roles = Role::query()->with('permissions')->get()->all(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Chunk through all books
 | 
					
						
							|  |  |  |         $this->bookFetchQuery()->chunk(5, function (EloquentCollection $books) use ($roles) { | 
					
						
							|  |  |  |             $this->buildJointPermissionsForBooks($books, $roles); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Chunk through all bookshelves
 | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |         Bookshelf::query()->withTrashed()->select(['id', 'owned_by']) | 
					
						
							| 
									
										
										
										
											2022-07-13 03:15:41 +08:00
										 |  |  |             ->chunk(50, function (EloquentCollection $shelves) use ($roles) { | 
					
						
							|  |  |  |                 $this->createManyJointPermissions($shelves->all(), $roles); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Rebuild the entity jointPermissions for a particular entity. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function rebuildForEntity(Entity $entity) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $entities = [$entity]; | 
					
						
							|  |  |  |         if ($entity instanceof Book) { | 
					
						
							|  |  |  |             $books = $this->bookFetchQuery()->where('id', '=', $entity->id)->get(); | 
					
						
							|  |  |  |             $this->buildJointPermissionsForBooks($books, Role::query()->with('permissions')->get()->all(), true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var BookChild $entity */ | 
					
						
							|  |  |  |         if ($entity->book) { | 
					
						
							|  |  |  |             $entities[] = $entity->book; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($entity instanceof Page && $entity->chapter_id) { | 
					
						
							|  |  |  |             $entities[] = $entity->chapter; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($entity instanceof Chapter) { | 
					
						
							|  |  |  |             foreach ($entity->pages as $page) { | 
					
						
							|  |  |  |                 $entities[] = $page; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->buildJointPermissionsForEntities($entities); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Build the entity jointPermissions for a particular role. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function rebuildForRole(Role $role) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $roles = [$role]; | 
					
						
							|  |  |  |         $role->jointPermissions()->delete(); | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |         $role->load('permissions'); | 
					
						
							| 
									
										
										
										
											2022-07-13 03:15:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Chunk through all books
 | 
					
						
							|  |  |  |         $this->bookFetchQuery()->chunk(20, function ($books) use ($roles) { | 
					
						
							|  |  |  |             $this->buildJointPermissionsForBooks($books, $roles); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Chunk through all bookshelves
 | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |         Bookshelf::query()->select(['id', 'owned_by']) | 
					
						
							| 
									
										
										
										
											2022-07-13 03:15:41 +08:00
										 |  |  |             ->chunk(50, function ($shelves) use ($roles) { | 
					
						
							|  |  |  |                 $this->createManyJointPermissions($shelves->all(), $roles); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Prepare the local entity cache and ensure it's empty. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |      * @param SimpleEntityData[] $entities | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |     protected function readyEntityCache(array $entities) | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->entityCache = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($entities as $entity) { | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |             if (!isset($this->entityCache[$entity->type])) { | 
					
						
							|  |  |  |                 $this->entityCache[$entity->type] = []; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |             $this->entityCache[$entity->type][$entity->id] = $entity; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a book via ID, Checks local cache. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |     protected function getBook(int $bookId): SimpleEntityData | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         return $this->entityCache['book'][$bookId]; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a chapter via ID, Checks local cache. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |     protected function getChapter(int $chapterId): SimpleEntityData | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         return $this->entityCache['chapter'][$chapterId]; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |      * Get a query for fetching a book with its children. | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function bookFetchQuery(): Builder | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Book::query()->withTrashed() | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |             ->select(['id', 'owned_by'])->with([ | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |                 'chapters' => function ($query) { | 
					
						
							| 
									
										
										
										
											2022-10-22 04:49:29 +08:00
										 |  |  |                     $query->withTrashed()->select(['id', 'owned_by', 'book_id']); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |                 }, | 
					
						
							|  |  |  |                 'pages' => function ($query) { | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |                     $query->withTrashed()->select(['id', 'owned_by', 'book_id', 'chapter_id']); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |                 }, | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Build joint permissions for the given book and role combinations. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function buildJointPermissionsForBooks(EloquentCollection $books, array $roles, bool $deleteOld = false) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $entities = clone $books; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Book $book */ | 
					
						
							|  |  |  |         foreach ($books->all() as $book) { | 
					
						
							|  |  |  |             foreach ($book->getRelation('chapters') as $chapter) { | 
					
						
							|  |  |  |                 $entities->push($chapter); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             foreach ($book->getRelation('pages') as $page) { | 
					
						
							|  |  |  |                 $entities->push($page); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($deleteOld) { | 
					
						
							|  |  |  |             $this->deleteManyJointPermissionsForEntities($entities->all()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->createManyJointPermissions($entities->all(), $roles); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Rebuild the entity jointPermissions for a collection of entities. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function buildJointPermissionsForEntities(array $entities) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $roles = Role::query()->get()->values()->all(); | 
					
						
							|  |  |  |         $this->deleteManyJointPermissionsForEntities($entities); | 
					
						
							|  |  |  |         $this->createManyJointPermissions($entities, $roles); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Delete all the entity jointPermissions for a list of entities. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Entity[] $entities | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function deleteManyJointPermissionsForEntities(array $entities) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         $simpleEntities = $this->entitiesToSimpleEntities($entities); | 
					
						
							|  |  |  |         $idsByType = $this->entitiesToTypeIdMap($simpleEntities); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         DB::transaction(function () use ($idsByType) { | 
					
						
							|  |  |  |             foreach ($idsByType as $type => $ids) { | 
					
						
							|  |  |  |                 foreach (array_chunk($ids, 1000) as $idChunk) { | 
					
						
							|  |  |  |                     DB::table('joint_permissions') | 
					
						
							|  |  |  |                         ->where('entity_type', '=', $type) | 
					
						
							|  |  |  |                         ->whereIn('entity_id', $idChunk) | 
					
						
							|  |  |  |                         ->delete(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Entity[] $entities | 
					
						
							| 
									
										
										
										
											2022-07-17 17:32:16 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |      * @return SimpleEntityData[] | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function entitiesToSimpleEntities(array $entities): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $simpleEntities = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($entities as $entity) { | 
					
						
							|  |  |  |             $attrs = $entity->getAttributes(); | 
					
						
							|  |  |  |             $simple = new SimpleEntityData(); | 
					
						
							|  |  |  |             $simple->id = $attrs['id']; | 
					
						
							|  |  |  |             $simple->type = $entity->getMorphClass(); | 
					
						
							|  |  |  |             $simple->owned_by = $attrs['owned_by'] ?? 0; | 
					
						
							|  |  |  |             $simple->book_id = $attrs['book_id'] ?? null; | 
					
						
							|  |  |  |             $simple->chapter_id = $attrs['chapter_id'] ?? null; | 
					
						
							|  |  |  |             $simpleEntities[] = $simple; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $simpleEntities; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Create & Save entity jointPermissions for many entities and roles. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-10-24 19:12:48 +08:00
										 |  |  |      * @param Entity[] $originalEntities | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |      * @param Role[]   $roles | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |     protected function createManyJointPermissions(array $originalEntities, array $roles) | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         $entities = $this->entitiesToSimpleEntities($originalEntities); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |         $this->readyEntityCache($entities); | 
					
						
							|  |  |  |         $jointPermissions = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Fetch related entity permissions
 | 
					
						
							|  |  |  |         $permissions = $this->getEntityPermissionsForEntities($entities); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Create a mapping of explicit entity permissions
 | 
					
						
							|  |  |  |         $permissionMap = []; | 
					
						
							|  |  |  |         foreach ($permissions as $permission) { | 
					
						
							| 
									
										
										
										
											2022-10-08 20:52:59 +08:00
										 |  |  |             $key = $permission->entity_type . ':' . $permission->entity_id . ':' . $permission->role_id; | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |             $permissionMap[$key] = $permission->view; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Create a mapping of role permissions
 | 
					
						
							|  |  |  |         $rolePermissionMap = []; | 
					
						
							|  |  |  |         foreach ($roles as $role) { | 
					
						
							|  |  |  |             foreach ($role->permissions as $permission) { | 
					
						
							|  |  |  |                 $rolePermissionMap[$role->getRawAttribute('id') . ':' . $permission->getRawAttribute('name')] = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Create Joint Permission Data
 | 
					
						
							|  |  |  |         foreach ($entities as $entity) { | 
					
						
							|  |  |  |             foreach ($roles as $role) { | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |                 $jointPermissions[] = $this->createJointPermissionData( | 
					
						
							|  |  |  |                     $entity, | 
					
						
							|  |  |  |                     $role->getRawAttribute('id'), | 
					
						
							|  |  |  |                     $permissionMap, | 
					
						
							|  |  |  |                     $rolePermissionMap, | 
					
						
							|  |  |  |                     $role->system_name === 'admin' | 
					
						
							|  |  |  |                 ); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         DB::transaction(function () use ($jointPermissions) { | 
					
						
							|  |  |  |             foreach (array_chunk($jointPermissions, 1000) as $jointPermissionChunk) { | 
					
						
							|  |  |  |                 DB::table('joint_permissions')->insert($jointPermissionChunk); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * From the given entity list, provide back a mapping of entity types to | 
					
						
							|  |  |  |      * the ids of that given type. The type used is the DB morph class. | 
					
						
							| 
									
										
										
										
											2022-07-17 17:32:16 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |      * @param SimpleEntityData[] $entities | 
					
						
							| 
									
										
										
										
											2022-07-17 17:32:16 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |      * @return array<string, int[]> | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function entitiesToTypeIdMap(array $entities): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $idsByType = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($entities as $entity) { | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |             if (!isset($idsByType[$entity->type])) { | 
					
						
							|  |  |  |                 $idsByType[$entity->type] = []; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |             $idsByType[$entity->type][] = $entity->id; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $idsByType; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-07-17 17:32:16 +08:00
										 |  |  |      * Get the entity permissions for all the given entities. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |      * @param SimpleEntityData[] $entities | 
					
						
							| 
									
										
										
										
											2022-07-17 17:32:16 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |      * @return EntityPermission[] | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |     protected function getEntityPermissionsForEntities(array $entities): array | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $idsByType = $this->entitiesToTypeIdMap($entities); | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |         $permissionFetch = EntityPermission::query() | 
					
						
							| 
									
										
										
										
											2022-07-17 17:32:16 +08:00
										 |  |  |             ->where(function (Builder $query) use ($idsByType) { | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |                 foreach ($idsByType as $type => $ids) { | 
					
						
							|  |  |  |                     $query->orWhere(function (Builder $query) use ($type, $ids) { | 
					
						
							| 
									
										
										
										
											2022-10-08 20:52:59 +08:00
										 |  |  |                         $query->where('entity_type', '=', $type)->whereIn('entity_id', $ids); | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |                     }); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         return $permissionFetch->get()->all(); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create entity permission data for an entity and role | 
					
						
							|  |  |  |      * for a particular action. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |     protected function createJointPermissionData(SimpleEntityData $entity, int $roleId, array $permissionMap, array $rolePermissionMap, bool $isAdminRole): array | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |         $permissionPrefix = $entity->type . '-view'; | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         $roleHasPermission = isset($rolePermissionMap[$roleId . ':' . $permissionPrefix . '-all']); | 
					
						
							|  |  |  |         $roleHasPermissionOwn = isset($rolePermissionMap[$roleId . ':' . $permissionPrefix . '-own']); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         if ($isAdminRole) { | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |             return $this->createJointPermissionDataArray($entity, $roleId, true, true); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |         if ($this->entityPermissionsActiveForRole($permissionMap, $entity, $roleId)) { | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |             $hasAccess = $this->mapHasActiveRestriction($permissionMap, $entity, $roleId); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |             return $this->createJointPermissionDataArray($entity, $roleId, $hasAccess, $hasAccess); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         if ($entity->type === 'book' || $entity->type === 'bookshelf') { | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |             return $this->createJointPermissionDataArray($entity, $roleId, $roleHasPermission, $roleHasPermissionOwn); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // For chapters and pages, Check if explicit permissions are set on the Book.
 | 
					
						
							|  |  |  |         $book = $this->getBook($entity->book_id); | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |         $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $book, $roleId); | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |         $hasPermissiveAccessToParents = !$this->entityPermissionsActiveForRole($permissionMap, $book, $roleId); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // For pages with a chapter, Check if explicit permissions are set on the Chapter
 | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |         if ($entity->type === 'page' && $entity->chapter_id !== 0) { | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |             $chapter = $this->getChapter($entity->chapter_id); | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |             $chapterRestricted = $this->entityPermissionsActiveForRole($permissionMap, $chapter, $roleId); | 
					
						
							|  |  |  |             $hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapterRestricted; | 
					
						
							|  |  |  |             if ($chapterRestricted) { | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |                 $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $chapter, $roleId); | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->createJointPermissionDataArray( | 
					
						
							|  |  |  |             $entity, | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |             $roleId, | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |             ($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)), | 
					
						
							|  |  |  |             ($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents)) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check if entity permissions are defined within the given map, for the given entity and role. | 
					
						
							|  |  |  |      * Checks for the default `role_id=0` backup option as a fallback. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function entityPermissionsActiveForRole(array $permissionMap, SimpleEntityData $entity, int $roleId): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $keyPrefix = $entity->type . ':' . $entity->id . ':'; | 
					
						
							|  |  |  |         return isset($permissionMap[$keyPrefix . $roleId]) || isset($permissionMap[$keyPrefix . '0']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check for an active restriction in an entity map. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |     protected function mapHasActiveRestriction(array $entityMap, SimpleEntityData $entity, int $roleId): bool | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |         $roleKey = $entity->type . ':' . $entity->id . ':' . $roleId; | 
					
						
							|  |  |  |         $defaultKey = $entity->type . ':' . $entity->id . ':0'; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 21:28:44 +08:00
										 |  |  |         return $entityMap[$roleKey] ?? $entityMap[$defaultKey] ?? false; | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create an array of data with the information of an entity jointPermissions. | 
					
						
							|  |  |  |      * Used to build data for bulk insertion. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |     protected function createJointPermissionDataArray(SimpleEntityData $entity, int $roleId, bool $permissionAll, bool $permissionOwn): array | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |             'entity_id'          => $entity->id, | 
					
						
							|  |  |  |             'entity_type'        => $entity->type, | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |             'has_permission'     => $permissionAll, | 
					
						
							|  |  |  |             'has_permission_own' => $permissionOwn, | 
					
						
							| 
									
										
										
										
											2022-07-13 04:13:02 +08:00
										 |  |  |             'owned_by'           => $entity->owned_by, | 
					
						
							|  |  |  |             'role_id'            => $roleId, | 
					
						
							| 
									
										
										
										
											2022-07-13 02:38:11 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-17 17:32:16 +08:00
										 |  |  | } |