| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | <?php namespace BookStack\Auth\Permissions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | use BookStack\Auth\Role; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  | use BookStack\Auth\User; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  | use BookStack\Entities\Models\BookChild; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							| 
									
										
										
										
											2021-03-13 23:18:37 +08:00
										 |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							| 
									
										
										
										
											2021-03-13 23:18:37 +08:00
										 |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2020-12-31 02:25:35 +08:00
										 |  |  | use BookStack\Model; | 
					
						
							|  |  |  | use BookStack\Traits\HasCreatorAndUpdater; | 
					
						
							|  |  |  | use BookStack\Traits\HasOwner; | 
					
						
							| 
									
										
										
										
											2017-01-02 05:21:11 +08:00
										 |  |  | use Illuminate\Database\Connection; | 
					
						
							| 
									
										
										
										
											2017-01-02 00:05:44 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Collection as EloquentCollection; | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  | use Illuminate\Database\Query\Builder as QueryBuilder; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  | use Throwable; | 
					
						
							| 
									
										
										
										
											2016-03-01 04:31:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  | class PermissionService | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @var ?array | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected $userRoles = null; | 
					
						
							| 
									
										
										
										
											2017-01-02 05:21:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @var ?User | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected $currentUserModel = null; | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @var Connection | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected $db; | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @var array | 
					
						
							| 
									
										
										
										
											2018-09-26 01:00:40 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     protected $entityCache; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |      * PermissionService constructor. | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     public function __construct(Connection $db) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-01-02 05:21:11 +08:00
										 |  |  |         $this->db = $db; | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Set the database connection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setConnection(Connection $connection) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->db = $connection; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Prepare the local entity cache and ensure it's empty | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @param Entity[] $entities | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function readyEntityCache(array $entities = []) | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-04-15 05:17:47 +08:00
										 |  |  |         $this->entityCache = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($entities as $entity) { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             $class = get_class($entity); | 
					
						
							|  |  |  |             if (!isset($this->entityCache[$class])) { | 
					
						
							|  |  |  |                 $this->entityCache[$class] = collect(); | 
					
						
							| 
									
										
										
										
											2018-04-15 05:17:47 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             $this->entityCache[$class]->put($entity->id, $entity); | 
					
						
							| 
									
										
										
										
											2018-04-15 05:17:47 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a book via ID, Checks local cache | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function getBook(int $bookId): ?Book | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if (isset($this->entityCache[Book::class]) && $this->entityCache[Book::class]->has($bookId)) { | 
					
						
							|  |  |  |             return $this->entityCache[Book::class]->get($bookId); | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         return Book::query()->withTrashed()->find($bookId); | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a chapter via ID, Checks local cache | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-13 23:18:37 +08:00
										 |  |  |     protected function getChapter(int $chapterId): ?Chapter | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if (isset($this->entityCache[Chapter::class]) && $this->entityCache[Chapter::class]->has($chapterId)) { | 
					
						
							|  |  |  |             return $this->entityCache[Chapter::class]->get($chapterId); | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         return Chapter::query() | 
					
						
							| 
									
										
										
										
											2021-03-13 23:18:37 +08:00
										 |  |  |             ->withTrashed() | 
					
						
							|  |  |  |             ->find($chapterId); | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 02:36:53 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * Get the roles for the current logged in user. | 
					
						
							| 
									
										
										
										
											2016-05-02 02:36:53 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function getCurrentUserRoles(): array | 
					
						
							| 
									
										
										
										
											2016-05-02 02:36:53 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if (!is_null($this->userRoles)) { | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |             return $this->userRoles; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-02 02:36:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (auth()->guest()) { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             $this->userRoles = [Role::getSystemRole('public')->id]; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $this->userRoles = $this->currentUser()->roles->pluck('id')->values()->all(); | 
					
						
							| 
									
										
										
										
											2016-05-02 02:36:53 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         return $this->userRoles; | 
					
						
							| 
									
										
										
										
											2016-05-02 02:36:53 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Re-generate all entity permission from scratch. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |     public function buildJointPermissions() | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         JointPermission::query()->truncate(); | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |         $this->readyEntityCache(); | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Get all roles (Should be the most limited dimension)
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $roles = Role::query()->with('permissions')->get()->all(); | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Chunk through all books
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $this->bookFetchQuery()->chunk(5, function (EloquentCollection $books) use ($roles) { | 
					
						
							| 
									
										
										
										
											2017-05-01 02:10:49 +08:00
										 |  |  |             $this->buildJointPermissionsForBooks($books, $roles); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-09-20 22:27:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Chunk through all bookshelves
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         Bookshelf::query()->withTrashed()->select(['id', 'restricted', 'owned_by']) | 
					
						
							|  |  |  |             ->chunk(50, function (EloquentCollection $shelves) use ($roles) { | 
					
						
							| 
									
										
										
										
											2018-09-22 01:48:47 +08:00
										 |  |  |                 $this->buildJointPermissionsForShelves($shelves, $roles); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2017-05-01 02:10:49 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a query for fetching a book with it's children. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function bookFetchQuery(): Builder | 
					
						
							| 
									
										
										
										
											2017-05-01 02:10:49 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         return Book::query()->withTrashed() | 
					
						
							|  |  |  |             ->select(['id', 'restricted', 'owned_by'])->with([ | 
					
						
							|  |  |  |                 'chapters' => function ($query) { | 
					
						
							|  |  |  |                     $query->withTrashed()->select(['id', 'restricted', 'owned_by', 'book_id']); | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |                 'pages' => function ($query) { | 
					
						
							|  |  |  |                     $query->withTrashed()->select(['id', 'restricted', 'owned_by', 'book_id', 'chapter_id']); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 22:27:30 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * Build joint permissions for the given shelf and role combinations. | 
					
						
							|  |  |  |      * @throws Throwable | 
					
						
							| 
									
										
										
										
											2018-09-20 22:27:30 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function buildJointPermissionsForShelves(EloquentCollection $shelves, array $roles, bool $deleteOld = false) | 
					
						
							| 
									
										
										
										
											2018-09-20 22:27:30 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         if ($deleteOld) { | 
					
						
							|  |  |  |             $this->deleteManyJointPermissionsForEntities($shelves->all()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $this->createManyJointPermissions($shelves->all(), $roles); | 
					
						
							| 
									
										
										
										
											2018-09-20 22:27:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * Build joint permissions for the given book and role combinations. | 
					
						
							|  |  |  |      * @throws Throwable | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function buildJointPermissionsForBooks(EloquentCollection $books, array $roles, bool $deleteOld = false) | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |         $entities = clone $books; | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  |         /** @var Book $book */ | 
					
						
							|  |  |  |         foreach ($books->all() as $book) { | 
					
						
							|  |  |  |             foreach ($book->getRelation('chapters') as $chapter) { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |                 $entities->push($chapter); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  |             foreach ($book->getRelation('pages') as $page) { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |                 $entities->push($page); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         if ($deleteOld) { | 
					
						
							|  |  |  |             $this->deleteManyJointPermissionsForEntities($entities->all()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $this->createManyJointPermissions($entities->all(), $roles); | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |      * Rebuild the entity jointPermissions for a particular entity. | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @throws Throwable | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |     public function buildJointPermissionsForEntity(Entity $entity) | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-05-01 02:10:49 +08:00
										 |  |  |         $entities = [$entity]; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if ($entity instanceof Book) { | 
					
						
							| 
									
										
										
										
											2017-05-01 02:10:49 +08:00
										 |  |  |             $books = $this->bookFetchQuery()->where('id', '=', $entity->id)->get(); | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             $this->buildJointPermissionsForBooks($books, Role::query()->get()->all(), true); | 
					
						
							| 
									
										
										
										
											2017-05-01 02:10:49 +08:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         /** @var BookChild $entity */ | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |         if ($entity->book) { | 
					
						
							|  |  |  |             $entities[] = $entity->book; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if ($entity instanceof Page && $entity->chapter_id) { | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  |             $entities[] = $entity->chapter; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if ($entity instanceof Chapter) { | 
					
						
							| 
									
										
										
										
											2017-05-01 02:10:49 +08:00
										 |  |  |             foreach ($entity->pages as $page) { | 
					
						
							|  |  |  |                 $entities[] = $page; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $this->buildJointPermissionsForEntities($entities); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Rebuild the entity jointPermissions for a collection of entities. | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @throws Throwable | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     public function buildJointPermissionsForEntities(array $entities) | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $roles = Role::query()->get()->values()->all(); | 
					
						
							|  |  |  |         $this->deleteManyJointPermissionsForEntities($entities); | 
					
						
							| 
									
										
										
										
											2016-08-27 03:20:58 +08:00
										 |  |  |         $this->createManyJointPermissions($entities, $roles); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |      * Build the entity jointPermissions for a particular role. | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |     public function buildJointPermissionForRole(Role $role) | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  |         $roles = [$role]; | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |         $this->deleteManyJointPermissionsForRoles($roles); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Chunk through all books
 | 
					
						
							| 
									
										
										
										
											2018-04-15 05:17:47 +08:00
										 |  |  |         $this->bookFetchQuery()->chunk(20, function ($books) use ($roles) { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |             $this->buildJointPermissionsForBooks($books, $roles); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-09-20 22:27:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Chunk through all bookshelves
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         Bookshelf::query()->select(['id', 'restricted', 'owned_by']) | 
					
						
							| 
									
										
										
										
											2018-09-20 22:27:30 +08:00
										 |  |  |             ->chunk(50, function ($shelves) use ($roles) { | 
					
						
							|  |  |  |                 $this->buildJointPermissionsForShelves($shelves, $roles); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |      * Delete the entity jointPermissions attached to a particular role. | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |     public function deleteJointPermissionsForRole(Role $role) | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |         $this->deleteManyJointPermissionsForRoles([$role]); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |      * Delete all of the entity jointPermissions for a list of entities. | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |      * @param Role[] $roles | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |     protected function deleteManyJointPermissionsForRoles($roles) | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         $roleIds = array_map(function ($role) { | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  |             return $role->id; | 
					
						
							|  |  |  |         }, $roles); | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         JointPermission::query()->whereIn('role_id', $roleIds)->delete(); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |      * Delete the entity jointPermissions for a particular entity. | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |      * @param Entity $entity | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @throws Throwable | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |     public function deleteJointPermissionsForEntity(Entity $entity) | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |         $this->deleteManyJointPermissionsForEntities([$entity]); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |      * Delete all of the entity jointPermissions for a list of entities. | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * @param Entity[] $entities | 
					
						
							|  |  |  |      * @throws Throwable | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function deleteManyJointPermissionsForEntities(array $entities) | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         if (count($entities) === 0) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         $this->db->transaction(function () use ($entities) { | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             foreach (array_chunk($entities, 1000) as $entityChunk) { | 
					
						
							|  |  |  |                 $query = $this->db->table('joint_permissions'); | 
					
						
							|  |  |  |                 foreach ($entityChunk as $entity) { | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |                     $query->orWhere(function (QueryBuilder $query) use ($entity) { | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  |                         $query->where('entity_id', '=', $entity->id) | 
					
						
							|  |  |  |                             ->where('entity_type', '=', $entity->getMorphClass()); | 
					
						
							|  |  |  |                     }); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 $query->delete(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * Create & Save entity jointPermissions for many entities and roles. | 
					
						
							|  |  |  |      * @param Entity[] $entities | 
					
						
							|  |  |  |      * @param Role[] $roles | 
					
						
							|  |  |  |      * @throws Throwable | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function createManyJointPermissions(array $entities, array $roles) | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-04-15 05:17:47 +08:00
										 |  |  |         $this->readyEntityCache($entities); | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |         $jointPermissions = []; | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Fetch Entity Permissions and create a mapping of entity restricted statuses
 | 
					
						
							|  |  |  |         $entityRestrictedMap = []; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $permissionFetch = EntityPermission::query(); | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |         foreach ($entities as $entity) { | 
					
						
							|  |  |  |             $entityRestrictedMap[$entity->getMorphClass() . ':' . $entity->id] = boolval($entity->getRawAttribute('restricted')); | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |             $permissionFetch->orWhere(function ($query) use ($entity) { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |                 $query->where('restrictable_id', '=', $entity->id)->where('restrictable_type', '=', $entity->getMorphClass()); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $permissions = $permissionFetch->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Create a mapping of explicit entity permissions
 | 
					
						
							|  |  |  |         $permissionMap = []; | 
					
						
							|  |  |  |         foreach ($permissions as $permission) { | 
					
						
							|  |  |  |             $key = $permission->restrictable_type . ':' . $permission->restrictable_id . ':' . $permission->role_id . ':' . $permission->action; | 
					
						
							|  |  |  |             $isRestricted = $entityRestrictedMap[$permission->restrictable_type . ':' . $permission->restrictable_id]; | 
					
						
							|  |  |  |             $permissionMap[$key] = $isRestricted; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Create a mapping of role permissions
 | 
					
						
							|  |  |  |         $rolePermissionMap = []; | 
					
						
							|  |  |  |         foreach ($roles as $role) { | 
					
						
							| 
									
										
										
										
											2018-04-15 05:17:47 +08:00
										 |  |  |             foreach ($role->permissions as $permission) { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |                 $rolePermissionMap[$role->getRawAttribute('id') . ':' . $permission->getRawAttribute('name')] = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Create Joint Permission Data
 | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |         foreach ($entities as $entity) { | 
					
						
							|  |  |  |             foreach ($roles as $role) { | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  |                 foreach ($this->getActions($entity) as $action) { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |                     $jointPermissions[] = $this->createJointPermissionData($entity, $role, $action, $permissionMap, $rolePermissionMap); | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         $this->db->transaction(function () use ($jointPermissions) { | 
					
						
							| 
									
										
										
										
											2017-04-30 18:38:58 +08:00
										 |  |  |             foreach (array_chunk($jointPermissions, 1000) as $jointPermissionChunk) { | 
					
						
							|  |  |  |                 $this->db->table('joint_permissions')->insert($jointPermissionChunk); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the actions related to an entity. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function getActions(Entity $entity): array | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $baseActions = ['view', 'update', 'delete']; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if ($entity instanceof Chapter || $entity instanceof Book) { | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |             $baseActions[] = 'page-create'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if ($entity instanceof Book) { | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |             $baseActions[] = 'chapter-create'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |         return $baseActions; | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Create entity permission data for an entity and role | 
					
						
							|  |  |  |      * for a particular action. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function createJointPermissionData(Entity $entity, Role $role, string $action, array $permissionMap, array $rolePermissionMap): array | 
					
						
							| 
									
										
										
										
											2016-04-21 04:37:57 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  |         $permissionPrefix = (strpos($action, '-') === false ? ($entity->getType() . '-') : '') . $action; | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |         $roleHasPermission = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-all']); | 
					
						
							|  |  |  |         $roleHasPermissionOwn = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-own']); | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  |         $explodedAction = explode('-', $action); | 
					
						
							|  |  |  |         $restrictionAction = end($explodedAction); | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-02 05:21:11 +08:00
										 |  |  |         if ($role->system_name === 'admin') { | 
					
						
							|  |  |  |             return $this->createJointPermissionDataArray($entity, $role, $action, true, true); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |         if ($entity->restricted) { | 
					
						
							|  |  |  |             $hasAccess = $this->mapHasActiveRestriction($permissionMap, $entity, $role, $restrictionAction); | 
					
						
							|  |  |  |             return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if ($entity instanceof Book || $entity instanceof Bookshelf) { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |             return $this->createJointPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |         // For chapters and pages, Check if explicit permissions are set on the Book.
 | 
					
						
							|  |  |  |         $book = $this->getBook($entity->book_id); | 
					
						
							|  |  |  |         $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $book, $role, $restrictionAction); | 
					
						
							|  |  |  |         $hasPermissiveAccessToParents = !$book->restricted; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // For pages with a chapter, Check if explicit permissions are set on the Chapter
 | 
					
						
							| 
									
										
										
										
											2021-03-13 23:18:37 +08:00
										 |  |  |         if ($entity instanceof Page && intval($entity->chapter_id) !== 0) { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |             $chapter = $this->getChapter($entity->chapter_id); | 
					
						
							|  |  |  |             $hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapter->restricted; | 
					
						
							|  |  |  |             if ($chapter->restricted) { | 
					
						
							|  |  |  |                 $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $chapter, $role, $restrictionAction); | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         return $this->createJointPermissionDataArray( | 
					
						
							|  |  |  |             $entity, | 
					
						
							|  |  |  |             $role, | 
					
						
							|  |  |  |             $action, | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |             ($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)), | 
					
						
							|  |  |  |             ($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents)) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check for an active restriction in an entity map. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function mapHasActiveRestriction(array $entityMap, Entity $entity, Role $role, string $action): bool | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  |         $key = $entity->getMorphClass() . ':' . $entity->getRawAttribute('id') . ':' . $role->getRawAttribute('id') . ':' . $action; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         return $entityMap[$key] ?? false; | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |      * Create an array of data with the information of an entity jointPermissions. | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |      * Used to build data for bulk insertion. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function createJointPermissionDataArray(Entity $entity, Role $role, string $action, bool $permissionAll, bool $permissionOwn): array | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             'role_id' => $role->getRawAttribute('id'), | 
					
						
							|  |  |  |             'entity_id' => $entity->getRawAttribute('id'), | 
					
						
							|  |  |  |             'entity_type' => $entity->getMorphClass(), | 
					
						
							|  |  |  |             'action' => $action, | 
					
						
							|  |  |  |             'has_permission' => $permissionAll, | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |             'has_permission_own' => $permissionOwn, | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             'owned_by' => $entity->getRawAttribute('owned_by'), | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2016-03-01 04:31:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Checks if an entity has a restriction set upon it. | 
					
						
							| 
									
										
										
										
											2020-12-31 02:25:35 +08:00
										 |  |  |      * @param HasCreatorAndUpdater|HasOwner $ownable | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-12-31 02:25:35 +08:00
										 |  |  |     public function checkOwnableUserAccess(Model $ownable, string $permission): bool | 
					
						
							| 
									
										
										
										
											2016-03-01 04:31:21 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |         $explodedPermission = explode('-', $permission); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-31 02:25:35 +08:00
										 |  |  |         $baseQuery = $ownable->newQuery()->where('id', '=', $ownable->id); | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  |         $action = end($explodedPermission); | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $user = $this->currentUser(); | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-31 06:18:28 +08:00
										 |  |  |         $nonJointPermissions = ['restrictions', 'image', 'attachment', 'comment']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |         // Handle non entity specific jointPermissions
 | 
					
						
							| 
									
										
										
										
											2020-12-31 06:18:28 +08:00
										 |  |  |         if (in_array($explodedPermission[0], $nonJointPermissions)) { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             $allPermission = $user && $user->can($permission . '-all'); | 
					
						
							|  |  |  |             $ownPermission = $user && $user->can($permission . '-own'); | 
					
						
							| 
									
										
										
										
											2021-01-05 02:07:39 +08:00
										 |  |  |             $ownerField = ($ownable instanceof Entity) ? 'owned_by' : 'created_by'; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             $isOwner = $user && $user->id === $ownable->$ownerField; | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  |             return ($allPermission || ($isOwner && $ownPermission)); | 
					
						
							| 
									
										
										
										
											2016-03-01 04:31:21 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-24 23:54:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |         // Handle abnormal create jointPermissions
 | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  |         if ($action === 'create') { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |             $action = $permission; | 
					
						
							| 
									
										
										
										
											2016-04-27 04:48:17 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $hasAccess = $this->entityRestrictionQuery($baseQuery, $action)->count() > 0; | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |         $this->clean(); | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         return $hasAccess; | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 13:55:28 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-03-10 00:50:22 +08:00
										 |  |  |      * Checks if a user has the given permission for any items in the system. | 
					
						
							| 
									
										
										
										
											2019-03-10 05:15:45 +08:00
										 |  |  |      * Can be passed an entity instance to filter on a specific type. | 
					
						
							| 
									
										
										
										
											2019-01-02 13:55:28 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     public function checkUserHasPermissionOnAnything(string $permission, ?string $entityClass = null): bool | 
					
						
							| 
									
										
										
										
											2019-01-02 13:55:28 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-03-10 00:50:22 +08:00
										 |  |  |         $userRoleIds = $this->currentUser()->roles()->select('id')->pluck('id')->toArray(); | 
					
						
							| 
									
										
										
										
											2019-01-02 13:55:28 +08:00
										 |  |  |         $userId = $this->currentUser()->id; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |         $permissionQuery = JointPermission::query() | 
					
						
							| 
									
										
										
										
											2019-03-10 00:50:22 +08:00
										 |  |  |             ->where('action', '=', $permission) | 
					
						
							| 
									
										
										
										
											2019-01-02 13:55:28 +08:00
										 |  |  |             ->whereIn('role_id', $userRoleIds) | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |             ->where(function (Builder $query) use ($userId) { | 
					
						
							|  |  |  |                 $this->addJointHasPermissionCheck($query, $userId); | 
					
						
							| 
									
										
										
										
											2019-04-03 17:26:24 +08:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2019-03-10 05:15:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!is_null($entityClass)) { | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |             $entityInstance = app($entityClass); | 
					
						
							| 
									
										
										
										
											2019-03-10 05:15:45 +08:00
										 |  |  |             $permissionQuery = $permissionQuery->where('entity_type', '=', $entityInstance->getMorphClass()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-01-02 13:55:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-10 05:15:45 +08:00
										 |  |  |         $hasPermission = $permissionQuery->count() > 0; | 
					
						
							| 
									
										
										
										
											2019-03-10 00:50:22 +08:00
										 |  |  |         $this->clean(); | 
					
						
							| 
									
										
										
										
											2019-03-10 05:15:45 +08:00
										 |  |  |         return $hasPermission; | 
					
						
							| 
									
										
										
										
											2019-01-02 13:55:28 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |      * The general query filter to remove all entities | 
					
						
							|  |  |  |      * that the current user does not have access to. | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     protected function entityRestrictionQuery(Builder $query, string $action): Builder | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $q = $query->where(function ($parentQuery) use ($action) { | 
					
						
							|  |  |  |             $parentQuery->whereHas('jointPermissions', function ($permissionQuery) use ($action) { | 
					
						
							|  |  |  |                 $permissionQuery->whereIn('role_id', $this->getCurrentUserRoles()) | 
					
						
							|  |  |  |                     ->where('action', '=', $action) | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |                     ->where(function (Builder $query) { | 
					
						
							|  |  |  |                         $this->addJointHasPermissionCheck($query, $this->currentUser()->id); | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |         $this->clean(); | 
					
						
							|  |  |  |         return $q; | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 23:00:29 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * Limited the given entity query so that the query will only | 
					
						
							|  |  |  |      * return items that the user has permission for the given ability. | 
					
						
							| 
									
										
										
										
											2017-01-15 23:00:29 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     public function restrictEntityQuery(Builder $query, string $ability = 'view'): Builder | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $this->clean(); | 
					
						
							|  |  |  |         return $query->where(function (Builder $parentQuery) use ($ability) { | 
					
						
							|  |  |  |             $parentQuery->whereHas('jointPermissions', function (Builder $permissionQuery) use ($ability) { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |                 $permissionQuery->whereIn('role_id', $this->getCurrentUserRoles()) | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |                     ->where('action', '=', $ability) | 
					
						
							|  |  |  |                     ->where(function (Builder $query) { | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |                         $this->addJointHasPermissionCheck($query, $this->currentUser()->id); | 
					
						
							| 
									
										
										
										
											2019-01-27 18:29:23 +08:00
										 |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2018-09-21 02:48:08 +08:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-01-22 20:02:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Extend the given page query to ensure draft items are not visible | 
					
						
							|  |  |  |      * unless created by the given user. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     public function enforceDraftVisibilityOnQuery(Builder $query): Builder | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $query->where(function (Builder $query) { | 
					
						
							|  |  |  |             $query->where('draft', '=', false) | 
					
						
							|  |  |  |                 ->orWhere(function (Builder $query) { | 
					
						
							|  |  |  |                     $query->where('draft', '=', true) | 
					
						
							| 
									
										
										
										
											2020-12-31 02:25:35 +08:00
										 |  |  |                         ->where('owned_by', '=', $this->currentUser()->id); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |                 }); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-01-02 05:21:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 03:33:08 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * Add restrictions for a generic entity. | 
					
						
							| 
									
										
										
										
											2016-05-07 03:33:08 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |     public function enforceEntityRestrictions(Entity $entity, Builder $query, string $action = 'view'): Builder | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |         if ($entity instanceof Page) { | 
					
						
							| 
									
										
										
										
											2017-01-02 00:05:44 +08:00
										 |  |  |             // Prevent drafts being visible to others.
 | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |             $this->enforceDraftVisibilityOnQuery($query); | 
					
						
							| 
									
										
										
										
											2017-01-02 00:05:44 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         return $this->entityRestrictionQuery($query, $action); | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-03-19 20:48:44 +08:00
										 |  |  |      * Filter items that have entities set as a polymorphic relation. | 
					
						
							| 
									
										
										
										
											2021-05-20 06:37:23 +08:00
										 |  |  |      * @param Builder|\Illuminate\Database\Query\Builder $query | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-20 06:37:23 +08:00
										 |  |  |     public function filterRestrictedEntityRelations($query, string $tableName, string $entityIdColumn, string $entityTypeColumn, string $action = 'view') | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn, 'entityTypeColumn' => $entityTypeColumn]; | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $q = $query->where(function ($query) use ($tableDetails, $action) { | 
					
						
							|  |  |  |             $query->whereExists(function ($permissionQuery) use (&$tableDetails, $action) { | 
					
						
							| 
									
										
										
										
											2021-05-23 21:34:36 +08:00
										 |  |  |                 $permissionQuery->select(['role_id'])->from('joint_permissions') | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |                     ->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn']) | 
					
						
							|  |  |  |                     ->whereRaw('joint_permissions.entity_type=' . $tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn']) | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |                     ->where('action', '=', $action) | 
					
						
							|  |  |  |                     ->whereIn('role_id', $this->getCurrentUserRoles()) | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |                     ->where(function (QueryBuilder $query) { | 
					
						
							|  |  |  |                         $this->addJointHasPermissionCheck($query, $this->currentUser()->id); | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 00:16:27 +08:00
										 |  |  |         $this->clean(); | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |         return $q; | 
					
						
							| 
									
										
										
										
											2016-02-29 03:03:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-13 21:30:47 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |      * Add conditions to a query to filter the selection to related entities | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |      * where view permissions are granted. | 
					
						
							| 
									
										
										
										
											2016-03-13 21:30:47 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     public function filterRelatedEntity(string $entityClass, Builder $query, string $tableName, string $entityIdColumn): Builder | 
					
						
							| 
									
										
										
										
											2016-03-13 21:30:47 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn]; | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $morphClass = app($entityClass)->getMorphClass(); | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $q = $query->where(function ($query) use ($tableDetails, $morphClass) { | 
					
						
							|  |  |  |             $query->where(function ($query) use (&$tableDetails, $morphClass) { | 
					
						
							|  |  |  |                 $query->whereExists(function ($permissionQuery) use (&$tableDetails, $morphClass) { | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |                     $permissionQuery->select('id')->from('joint_permissions') | 
					
						
							|  |  |  |                         ->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn']) | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |                         ->where('entity_type', '=', $morphClass) | 
					
						
							|  |  |  |                         ->where('action', '=', 'view') | 
					
						
							|  |  |  |                         ->whereIn('role_id', $this->getCurrentUserRoles()) | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |                         ->where(function (QueryBuilder $query) { | 
					
						
							|  |  |  |                             $this->addJointHasPermissionCheck($query, $this->currentUser()->id); | 
					
						
							| 
									
										
										
										
											2016-03-13 21:30:47 +08:00
										 |  |  |                         }); | 
					
						
							| 
									
										
										
										
											2016-04-24 01:14:26 +08:00
										 |  |  |                 }); | 
					
						
							|  |  |  |             })->orWhere($tableDetails['entityIdColumn'], '=', 0); | 
					
						
							| 
									
										
										
										
											2016-03-13 21:30:47 +08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |         $this->clean(); | 
					
						
							|  |  |  |         return $q; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 04:32:33 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 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('has_permission', '=', true)->orWhere(function ($query) use ($userIdToCheck) { | 
					
						
							|  |  |  |             $query->where('has_permission_own', '=', true) | 
					
						
							|  |  |  |                 ->where('owned_by', '=', $userIdToCheck); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the current user | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     private function currentUser(): User | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         if (is_null($this->currentUserModel)) { | 
					
						
							| 
									
										
										
										
											2016-09-29 19:43:46 +08:00
										 |  |  |             $this->currentUserModel = user(); | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->currentUserModel; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Clean the cached user elements. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |     private function clean(): void | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-15 03:52:07 +08:00
										 |  |  |         $this->currentUserModel = null; | 
					
						
							|  |  |  |         $this->userRoles = null; | 
					
						
							| 
									
										
										
										
											2016-03-13 21:30:47 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  | } |