]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/PageController.php
Done a refactor pass on PermissionService
[bookstack] / app / Http / Controllers / PageController.php
index da927374332c9ccecfc600a3355f9d5f16d6f47a..7d8e54382961006db647b5f1b1b4fe9982337a2d 100644 (file)
 <?php namespace BookStack\Http\Controllers;
 
-use Activity;
+use BookStack\Entities\Tools\BookContents;
+use BookStack\Entities\Tools\PageContent;
+use BookStack\Entities\Tools\PageEditActivity;
+use BookStack\Entities\Models\Page;
+use BookStack\Entities\Repos\PageRepo;
+use BookStack\Entities\Tools\PermissionsUpdater;
 use BookStack\Exceptions\NotFoundException;
-use BookStack\Repos\UserRepo;
-use BookStack\Services\ExportService;
-use Carbon\Carbon;
+use BookStack\Exceptions\NotifyException;
+use BookStack\Exceptions\PermissionsException;
+use Exception;
 use Illuminate\Http\Request;
-use BookStack\Http\Requests;
-use BookStack\Repos\BookRepo;
-use BookStack\Repos\ChapterRepo;
-use BookStack\Repos\PageRepo;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+use Illuminate\Validation\ValidationException;
+use Throwable;
 use Views;
 
 class PageController extends Controller
 {
 
     protected $pageRepo;
-    protected $bookRepo;
-    protected $chapterRepo;
-    protected $exportService;
-    protected $userRepo;
 
     /**
      * PageController constructor.
-     * @param PageRepo $pageRepo
-     * @param BookRepo $bookRepo
-     * @param ChapterRepo $chapterRepo
-     * @param ExportService $exportService
-     * @param UserRepo $userRepo
      */
-    public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService, UserRepo $userRepo)
+    public function __construct(PageRepo $pageRepo)
     {
         $this->pageRepo = $pageRepo;
-        $this->bookRepo = $bookRepo;
-        $this->chapterRepo = $chapterRepo;
-        $this->exportService = $exportService;
-        $this->userRepo = $userRepo;
-        parent::__construct();
     }
 
     /**
      * Show the form for creating a new page.
-     * @param      $bookSlug
-     * @param bool $chapterSlug
-     * @return Response
-     * @internal param bool $pageSlug
+     * @throws Throwable
      */
-    public function create($bookSlug, $chapterSlug = false)
+    public function create(string $bookSlug, string $chapterSlug = null)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
-        $parent = $chapter ? $chapter : $book;
+        $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
         $this->checkOwnablePermission('page-create', $parent);
-        $this->setPageTitle('Create New Page');
 
-        $draft = $this->pageRepo->getDraftPage($book, $chapter);
-        return redirect($draft->getUrl());
+        // Redirect to draft edit screen if signed in
+        if ($this->isSignedIn()) {
+            $draft = $this->pageRepo->getNewDraftPage($parent);
+            return redirect($draft->getUrl());
+        }
+
+        // Otherwise show the edit view if they're a guest
+        $this->setPageTitle(trans('entities.pages_new'));
+        return view('pages.guest-create', ['parent' => $parent]);
     }
 
     /**
-     * Show form to continue editing a draft page.
-     * @param $bookSlug
-     * @param $pageId
-     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     * Create a new page as a guest user.
+     * @throws ValidationException
      */
-    public function editDraft($bookSlug, $pageId)
+    public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $draft = $this->pageRepo->getById($pageId, true);
-        $this->checkOwnablePermission('page-create', $book);
-        $this->setPageTitle('Edit Page Draft');
+        $this->validate($request, [
+            'name' => 'required|string|max:255'
+        ]);
+
+        $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
+        $this->checkOwnablePermission('page-create', $parent);
+
+        $page = $this->pageRepo->getNewDraftPage($parent);
+        $this->pageRepo->publishDraft($page, [
+            'name' => $request->get('name'),
+            'html' => ''
+        ]);
 
-        return view('pages/edit', ['page' => $draft, 'book' => $book, 'isDraft' => true]);
+        return redirect($page->getUrl('/edit'));
+    }
+
+    /**
+     * Show form to continue editing a draft page.
+     * @throws NotFoundException
+     */
+    public function editDraft(string $bookSlug, int $pageId)
+    {
+        $draft = $this->pageRepo->getById($pageId);
+        $this->checkOwnablePermission('page-create', $draft->getParent());
+        $this->setPageTitle(trans('entities.pages_edit_draft'));
+
+        $draftsEnabled = $this->isSignedIn();
+        $templates = $this->pageRepo->getTemplates(10);
+
+        return view('pages.edit', [
+            'page' => $draft,
+            'book' => $draft->book,
+            'isDraft' => true,
+            'draftsEnabled' => $draftsEnabled,
+            'templates' => $templates,
+        ]);
     }
 
     /**
      * Store a new page by changing a draft into a page.
-     * @param  Request $request
-     * @param  string $bookSlug
-     * @return Response
+     * @throws NotFoundException
+     * @throws ValidationException
      */
-    public function store(Request $request, $bookSlug, $pageId)
+    public function store(Request $request, string $bookSlug, int $pageId)
     {
         $this->validate($request, [
             'name' => 'required|string|max:255'
         ]);
+        $draftPage = $this->pageRepo->getById($pageId);
+        $this->checkOwnablePermission('page-create', $draftPage->getParent());
 
-        $input = $request->all();
-        $book = $this->bookRepo->getBySlug($bookSlug);
-
-        $draftPage = $this->pageRepo->getById($pageId, true);
+        $page = $this->pageRepo->publishDraft($draftPage, $request->all());
 
-        $chapterId = $draftPage->chapter_id;
-        $parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book;
-        $this->checkOwnablePermission('page-create', $parent);
-
-        if ($parent->isA('chapter')) {
-            $input['priority'] = $this->chapterRepo->getNewPriority($parent);
-        } else {
-            $input['priority'] = $this->bookRepo->getNewPriority($parent);
-        }
-
-        $page = $this->pageRepo->publishDraft($draftPage, $input);
-
-        Activity::add($page, 'page_create', $book->id);
         return redirect($page->getUrl());
     }
 
     /**
      * Display the specified page.
-     * If the page is not found via the slug the
-     * revisions are searched for a match.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return Response
+     * If the page is not found via the slug the revisions are searched for a match.
+     * @throws NotFoundException
      */
-    public function show($bookSlug, $pageSlug)
+    public function show(string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-
         try {
-            $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+            $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         } catch (NotFoundException $e) {
-            $page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug);
-            if ($page === null) abort(404);
+            $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
+
+            if ($page === null) {
+                throw $e;
+            }
+
             return redirect($page->getUrl());
         }
 
         $this->checkOwnablePermission('page-view', $page);
 
-        $sidebarTree = $this->bookRepo->getChildren($book);
+        $pageContent = (new PageContent($page));
+        $page->html = $pageContent->render();
+        $sidebarTree = (new BookContents($page->book))->getTree();
+        $pageNav = $pageContent->getNavigation($page->html);
+
+        // Check if page comments are enabled
+        $commentsEnabled = !setting('app-disable-comments');
+        if ($commentsEnabled) {
+            $page->load(['comments.createdBy']);
+        }
+
         Views::add($page);
         $this->setPageTitle($page->getShortName());
-        return view('pages/show', ['page' => $page, 'book' => $book, 'current' => $page, 'sidebarTree' => $sidebarTree]);
+        return view('pages.show', [
+            'page' => $page,
+            'book' => $page->book,
+            'current' => $page,
+            'sidebarTree' => $sidebarTree,
+            'commentsEnabled' => $commentsEnabled,
+            'pageNav' => $pageNav
+        ]);
     }
 
     /**
      * Get page from an ajax request.
-     * @param $pageId
-     * @return \Illuminate\Http\JsonResponse
+     * @throws NotFoundException
      */
-    public function getPageAjax($pageId)
+    public function getPageAjax(int $pageId)
     {
         $page = $this->pageRepo->getById($pageId);
+        $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
+        $page->addHidden(['book']);
         return response()->json($page);
     }
 
     /**
      * Show the form for editing the specified page.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return Response
+     * @throws NotFoundException
      */
-    public function edit($bookSlug, $pageSlug)
+    public function edit(string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         $this->checkOwnablePermission('page-update', $page);
-        $this->setPageTitle('Editing Page ' . $page->getShortName());
+
         $page->isDraft = false;
+        $editActivity = new PageEditActivity($page);
 
         // Check for active editing
         $warnings = [];
-        if ($this->pageRepo->isPageEditingActive($page, 60)) {
-            $warnings[] = $this->pageRepo->getPageEditingActiveMessage($page, 60);
+        if ($editActivity->hasActiveEditing()) {
+            $warnings[] = $editActivity->activeEditingMessage();
         }
 
         // Check for a current draft version for this user
-        if ($this->pageRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
-            $draft = $this->pageRepo->getUserPageDraft($page, $this->currentUser->id);
-            $page->name = $draft->name;
-            $page->html = $draft->html;
-            $page->markdown = $draft->markdown;
+        $userDraft = $this->pageRepo->getUserDraft($page);
+        if ($userDraft !== null) {
+            $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
             $page->isDraft = true;
-            $warnings [] = $this->pageRepo->getUserPageDraftMessage($draft);
+            $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
         }
 
-        if (count($warnings) > 0) session()->flash('warning', implode("\n", $warnings));
+        if (count($warnings) > 0) {
+            $this->showWarningNotification(implode("\n", $warnings));
+        }
 
-        return view('pages/edit', ['page' => $page, 'book' => $book, 'current' => $page]);
+        $templates = $this->pageRepo->getTemplates(10);
+        $draftsEnabled = $this->isSignedIn();
+        $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
+        return view('pages.edit', [
+            'page' => $page,
+            'book' => $page->book,
+            'current' => $page,
+            'draftsEnabled' => $draftsEnabled,
+            'templates' => $templates,
+        ]);
     }
 
     /**
      * Update the specified page in storage.
-     * @param  Request $request
-     * @param          $bookSlug
-     * @param          $pageSlug
-     * @return Response
+     * @throws ValidationException
+     * @throws NotFoundException
      */
-    public function update(Request $request, $bookSlug, $pageSlug)
+    public function update(Request $request, string $bookSlug, string $pageSlug)
     {
         $this->validate($request, [
             'name' => 'required|string|max:255'
         ]);
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         $this->checkOwnablePermission('page-update', $page);
-        $this->pageRepo->updatePage($page, $book->id, $request->all());
-        Activity::add($page, 'page_update', $book->id);
+
+        $this->pageRepo->update($page, $request->all());
+
         return redirect($page->getUrl());
     }
 
     /**
      * Save a draft update as a revision.
-     * @param Request $request
-     * @param $pageId
-     * @return \Illuminate\Http\JsonResponse
+     * @throws NotFoundException
      */
-    public function saveDraft(Request $request, $pageId)
+    public function saveDraft(Request $request, int $pageId)
     {
-        $page = $this->pageRepo->getById($pageId, true);
+        $page = $this->pageRepo->getById($pageId);
         $this->checkOwnablePermission('page-update', $page);
-        if ($page->draft) {
-            $draft = $this->pageRepo->updateDraftPage($page, $request->only(['name', 'html', 'markdown']));
-        } else {
-            $draft = $this->pageRepo->saveUpdateDraft($page, $request->only(['name', 'html', 'markdown']));
+
+        if (!$this->isSignedIn()) {
+            return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
         }
 
+        $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
+
         $updateTime = $draft->updated_at->timestamp;
-        $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
         return response()->json([
-            'status' => 'success',
-            'message' => 'Draft saved at ',
-            'timestamp' => $utcUpdateTimestamp
+            'status'    => 'success',
+            'message'   => trans('entities.pages_edit_draft_save_at'),
+            'timestamp' => $updateTime
         ]);
     }
 
     /**
-     * Redirect from a special link url which
-     * uses the page id rather than the name.
-     * @param $pageId
-     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+     * Redirect from a special link url which uses the page id rather than the name.
+     * @throws NotFoundException
      */
-    public function redirectFromLink($pageId)
+    public function redirectFromLink(int $pageId)
     {
         $page = $this->pageRepo->getById($pageId);
         return redirect($page->getUrl());
@@ -241,230 +261,207 @@ class PageController extends Controller
 
     /**
      * Show the deletion page for the specified page.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return \Illuminate\View\View
+     * @throws NotFoundException
      */
-    public function showDelete($bookSlug, $pageSlug)
+    public function showDelete(string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         $this->checkOwnablePermission('page-delete', $page);
-        $this->setPageTitle('Delete Page ' . $page->getShortName());
-        return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
+        $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
+        return view('pages.delete', [
+            'book' => $page->book,
+            'page' => $page,
+            'current' => $page
+        ]);
     }
 
-
     /**
      * Show the deletion page for the specified page.
-     * @param $bookSlug
-     * @param $pageId
-     * @return \Illuminate\View\View
      * @throws NotFoundException
      */
-    public function showDeleteDraft($bookSlug, $pageId)
+    public function showDeleteDraft(string $bookSlug, int $pageId)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getById($pageId, true);
+        $page = $this->pageRepo->getById($pageId);
         $this->checkOwnablePermission('page-update', $page);
-        $this->setPageTitle('Delete Draft Page ' . $page->getShortName());
-        return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
+        $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
+        return view('pages.delete', [
+            'book' => $page->book,
+            'page' => $page,
+            'current' => $page
+        ]);
     }
 
     /**
      * Remove the specified page from storage.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return Response
-     * @internal param int $id
+     * @throws NotFoundException
+     * @throws Throwable
+     * @throws NotifyException
      */
-    public function destroy($bookSlug, $pageSlug)
+    public function destroy(string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         $this->checkOwnablePermission('page-delete', $page);
-        Activity::addMessage('page_delete', $book->id, $page->name);
-        session()->flash('success', 'Page deleted');
+        $parent = $page->getParent();
+
         $this->pageRepo->destroy($page);
-        return redirect($book->getUrl());
+
+        return redirect($parent->getUrl());
     }
 
     /**
      * Remove the specified draft page from storage.
-     * @param $bookSlug
-     * @param $pageId
-     * @return Response
      * @throws NotFoundException
+     * @throws NotifyException
+     * @throws Throwable
      */
-    public function destroyDraft($bookSlug, $pageId)
+    public function destroyDraft(string $bookSlug, int $pageId)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getById($pageId, true);
+        $page = $this->pageRepo->getById($pageId);
+        $book = $page->book;
+        $chapter = $page->chapter;
         $this->checkOwnablePermission('page-update', $page);
-        session()->flash('success', 'Draft deleted');
+
         $this->pageRepo->destroy($page);
+
+        $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
+
+        if ($chapter && userCan('view', $chapter)) {
+            return redirect($chapter->getUrl());
+        }
         return redirect($book->getUrl());
     }
 
     /**
-     * Shows the last revisions for this page.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return \Illuminate\View\View
+     * Show a listing of recently created pages.
      */
-    public function showRevisions($bookSlug, $pageSlug)
+    public function showRecentlyUpdated()
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
-        $this->setPageTitle('Revisions For ' . $page->getShortName());
-        return view('pages/revisions', ['page' => $page, 'book' => $book, 'current' => $page]);
+        $pages = Page::visible()->orderBy('updated_at', 'desc')
+            ->paginate(20)
+            ->setPath(url('/pages/recently-updated'));
+
+        return view('pages.detailed-listing', [
+            'title' => trans('entities.recently_updated_pages'),
+            'pages' => $pages
+        ]);
     }
 
     /**
-     * Shows a preview of a single revision
-     * @param $bookSlug
-     * @param $pageSlug
-     * @param $revisionId
-     * @return \Illuminate\View\View
+     * Show the view to choose a new parent to move a page into.
+     * @throws NotFoundException
      */
-    public function showRevision($bookSlug, $pageSlug, $revisionId)
+    public function showMove(string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
-        $revision = $this->pageRepo->getRevisionById($revisionId);
-        $page->fill($revision->toArray());
-        $this->setPageTitle('Page Revision For ' . $page->getShortName());
-        return view('pages/revision', ['page' => $page, 'book' => $book]);
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+        $this->checkOwnablePermission('page-update', $page);
+        $this->checkOwnablePermission('page-delete', $page);
+        return view('pages.move', [
+            'book' => $page->book,
+            'page' => $page
+        ]);
     }
 
     /**
-     * Restores a page using the content of the specified revision.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @param $revisionId
-     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+     * Does the action of moving the location of a page.
+     * @throws NotFoundException
+     * @throws Throwable
      */
-    public function restoreRevision($bookSlug, $pageSlug, $revisionId)
+    public function move(Request $request, string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         $this->checkOwnablePermission('page-update', $page);
-        $page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
-        Activity::add($page, 'page_restore', $book->id);
+        $this->checkOwnablePermission('page-delete', $page);
+
+        $entitySelection = $request->get('entity_selection', null);
+        if ($entitySelection === null || $entitySelection === '') {
+            return redirect($page->getUrl());
+        }
+
+        try {
+            $parent = $this->pageRepo->move($page, $entitySelection);
+        } catch (Exception $exception) {
+            if ($exception instanceof  PermissionsException) {
+                $this->showPermissionError();
+            }
+
+            $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
+            return redirect()->back();
+        }
+
+        $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
         return redirect($page->getUrl());
     }
 
     /**
-     * Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
-     * https://github.com/barryvdh/laravel-dompdf
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return \Illuminate\Http\Response
+     * Show the view to copy a page.
+     * @throws NotFoundException
      */
-    public function exportPdf($bookSlug, $pageSlug)
+    public function showCopy(string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
-        $pdfContent = $this->exportService->pageToPdf($page);
-        return response()->make($pdfContent, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+        $this->checkOwnablePermission('page-view', $page);
+        session()->flashInput(['name' => $page->name]);
+        return view('pages.copy', [
+            'book' => $page->book,
+            'page' => $page
         ]);
     }
 
-    /**
-     * Export a page to a self-contained HTML file.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return \Illuminate\Http\Response
-     */
-    public function exportHtml($bookSlug, $pageSlug)
-    {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
-        $containedHtml = $this->exportService->pageToContainedHtml($page);
-        return response()->make($containedHtml, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
-        ]);
-    }
 
     /**
-     * Export a page to a simple plaintext .txt file.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return \Illuminate\Http\Response
+     * Create a copy of a page within the requested target destination.
+     * @throws NotFoundException
+     * @throws Throwable
      */
-    public function exportPlainText($bookSlug, $pageSlug)
+    public function copy(Request $request, string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
-        $containedHtml = $this->exportService->pageToPlainText($page);
-        return response()->make($containedHtml, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
-        ]);
-    }
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+        $this->checkOwnablePermission('page-view', $page);
 
-    /**
-     * Show a listing of recently created pages
-     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
-     */
-    public function showRecentlyCreated()
-    {
-        $pages = $this->pageRepo->getRecentlyCreatedPaginated(20);
-        return view('pages/detailed-listing', [
-            'title' => 'Recently Created Pages',
-            'pages' => $pages
-        ]);
-    }
+        $entitySelection = $request->get('entity_selection', null) ?? null;
+        $newName = $request->get('name', null);
 
-    /**
-     * Show a listing of recently created pages
-     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
-     */
-    public function showRecentlyUpdated()
-    {
-        $pages = $this->pageRepo->getRecentlyUpdatedPaginated(20);
-        return view('pages/detailed-listing', [
-            'title' => 'Recently Updated Pages',
-            'pages' => $pages
-        ]);
+        try {
+            $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
+        } catch (Exception $exception) {
+            if ($exception instanceof  PermissionsException) {
+                $this->showPermissionError();
+            }
+
+            $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
+            return redirect()->back();
+        }
+
+        $this->showSuccessNotification(trans('entities.pages_copy_success'));
+        return redirect($pageCopy->getUrl());
     }
 
     /**
-     * Show the Restrictions view.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     * Show the Permissions view.
+     * @throws NotFoundException
      */
-    public function showRestrict($bookSlug, $pageSlug)
+    public function showPermissions(string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         $this->checkOwnablePermission('restrictions-manage', $page);
-        $roles = $this->userRepo->getRestrictableRoles();
-        return view('pages/restrictions', [
+        return view('pages.permissions', [
             'page'  => $page,
-            'roles' => $roles
         ]);
     }
 
     /**
      * Set the permissions for this page.
-     * @param $bookSlug
-     * @param $pageSlug
-     * @param Request $request
-     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+     * @throws NotFoundException
+     * @throws Throwable
      */
-    public function restrict($bookSlug, $pageSlug, Request $request)
+    public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
     {
-        $book = $this->bookRepo->getBySlug($bookSlug);
-        $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+        $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         $this->checkOwnablePermission('restrictions-manage', $page);
-        $this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
-        session()->flash('success', 'Page Permissions Updated');
+
+        $permissionsUpdater->updateFromPermissionsForm($page, $request);
+
+        $this->showSuccessNotification(trans('entities.pages_permissions_success'));
         return redirect($page->getUrl());
     }
-
 }