| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Permissions; | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Page; | 
					
						
							|  |  |  | use BookStack\Entities\Tools\PermissionsUpdater; | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Permissions\Models\EntityPermission; | 
					
						
							|  |  |  | use BookStack\Users\Models\Role; | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PermissionsController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected PermissionsUpdater $permissionsUpdater; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(PermissionsUpdater $permissionsUpdater) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->permissionsUpdater = $permissionsUpdater; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the Permissions view for a page. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showForPage(string $bookSlug, string $pageSlug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page = Page::getBySlugs($bookSlug, $pageSlug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $page); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 22:52:56 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.pages_permissions')); | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  |         return view('pages.permissions', [ | 
					
						
							|  |  |  |             'page' => $page, | 
					
						
							| 
									
										
										
										
											2022-10-10 00:14:11 +08:00
										 |  |  |             'data' => new PermissionFormData($page), | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Set the permissions for a page. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function updateForPage(Request $request, string $bookSlug, string $pageSlug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page = Page::getBySlugs($bookSlug, $pageSlug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $page); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->permissionsUpdater->updateFromPermissionsForm($page, $request); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->showSuccessNotification(trans('entities.pages_permissions_success')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect($page->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the Restrictions view for a chapter. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showForChapter(string $bookSlug, string $chapterSlug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $chapter); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 22:52:56 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.chapters_permissions')); | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  |         return view('chapters.permissions', [ | 
					
						
							|  |  |  |             'chapter' => $chapter, | 
					
						
							| 
									
										
										
										
											2022-10-10 00:14:11 +08:00
										 |  |  |             'data' => new PermissionFormData($chapter), | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Set the restrictions for a chapter. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function updateForChapter(Request $request, string $bookSlug, string $chapterSlug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chapter = Chapter::getBySlugs($bookSlug, $chapterSlug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $chapter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->permissionsUpdater->updateFromPermissionsForm($chapter, $request); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->showSuccessNotification(trans('entities.chapters_permissions_success')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect($chapter->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the permissions view for a book. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showForBook(string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $book = Book::getBySlug($slug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $book); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 22:52:56 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.books_permissions')); | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  |         return view('books.permissions', [ | 
					
						
							|  |  |  |             'book' => $book, | 
					
						
							| 
									
										
										
										
											2022-10-10 00:14:11 +08:00
										 |  |  |             'data' => new PermissionFormData($book), | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Set the restrictions for a book. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function updateForBook(Request $request, string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $book = Book::getBySlug($slug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $book); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->permissionsUpdater->updateFromPermissionsForm($book, $request); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->showSuccessNotification(trans('entities.books_permissions_updated')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect($book->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the permissions view for a shelf. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showForShelf(string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $shelf = Bookshelf::getBySlug($slug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $shelf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 22:52:56 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.shelves_permissions')); | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  |         return view('shelves.permissions', [ | 
					
						
							|  |  |  |             'shelf' => $shelf, | 
					
						
							| 
									
										
										
										
											2022-10-10 00:14:11 +08:00
										 |  |  |             'data' => new PermissionFormData($shelf), | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Set the permissions for a shelf. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function updateForShelf(Request $request, string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $shelf = Bookshelf::getBySlug($slug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $shelf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->permissionsUpdater->updateFromPermissionsForm($shelf, $request); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->showSuccessNotification(trans('entities.shelves_permissions_updated')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect($shelf->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Copy the permissions of a bookshelf to the child books. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function copyShelfPermissionsToBooks(string $slug) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $shelf = Bookshelf::getBySlug($slug); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('restrictions-manage', $shelf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $updateCount = $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf); | 
					
						
							|  |  |  |         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect($shelf->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-10-10 19:24:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get an empty entity permissions form row for the given role. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function formRowForRole(string $entityType, string $roleId) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-14 23:03:06 +08:00
										 |  |  |         $this->checkPermissionOr('restrictions-manage-all', fn() => userCan('restrictions-manage-own')); | 
					
						
							| 
									
										
										
										
											2022-10-10 19:24:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $role = Role::query()->findOrFail($roleId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return view('form.entity-permissions-row', [ | 
					
						
							|  |  |  |             'role' => $role, | 
					
						
							|  |  |  |             'permission' => new EntityPermission(), | 
					
						
							|  |  |  |             'entityType' => $entityType, | 
					
						
							| 
									
										
										
										
											2022-10-11 00:22:38 +08:00
										 |  |  |             'inheriting' => false, | 
					
						
							| 
									
										
										
										
											2022-10-10 19:24:23 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-10-09 23:36:03 +08:00
										 |  |  | } |