]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/PageRepo.php
Merge branch 'custom_role_system'
[bookstack] / app / Repos / PageRepo.php
index bfaff79ae3ce22fec0bd00f9f63a6e1b2e9b6df6..3d675183e3f1c807d78916893cf4488de3b5155c 100644 (file)
@@ -4,6 +4,8 @@
 use Activity;
 use BookStack\Book;
 use BookStack\Chapter;
+use BookStack\Exceptions\NotFoundException;
+use BookStack\Services\RestrictionService;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Log;
@@ -16,26 +18,28 @@ class PageRepo
 {
     protected $page;
     protected $pageRevision;
+    protected $restrictionService;
 
     /**
      * PageRepo constructor.
-     * @param Page         $page
+     * @param Page $page
      * @param PageRevision $pageRevision
+     * @param RestrictionService $restrictionService
      */
-    public function __construct(Page $page, PageRevision $pageRevision)
+    public function __construct(Page $page, PageRevision $pageRevision, RestrictionService $restrictionService)
     {
         $this->page = $page;
         $this->pageRevision = $pageRevision;
+        $this->restrictionService = $restrictionService;
     }
 
     /**
-     * Check if a page id exists.
-     * @param $id
-     * @return bool
+     * Base query for getting pages, Takes restrictions into account.
+     * @return mixed
      */
-    public function idExists($id)
+    private function pageQuery()
     {
-        return $this->page->where('page_id', '=', $id)->count() > 0;
+        return $this->restrictionService->enforcePageRestrictions($this->page, 'view');
     }
 
     /**
@@ -45,16 +49,7 @@ class PageRepo
      */
     public function getById($id)
     {
-        return $this->page->findOrFail($id);
-    }
-
-    /**
-     * Get all pages.
-     * @return \Illuminate\Database\Eloquent\Collection|static[]
-     */
-    public function getAll()
-    {
-        return $this->page->all();
+        return $this->pageQuery()->findOrFail($id);
     }
 
     /**
@@ -62,11 +57,12 @@ class PageRepo
      * @param $slug
      * @param $bookId
      * @return mixed
+     * @throws NotFoundException
      */
     public function getBySlug($slug, $bookId)
     {
-        $page = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
-        if ($page === null) throw new NotFoundHttpException('Page not found');
+        $page = $this->pageQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+        if ($page === null) throw new NotFoundException('Page not found');
         return $page;
     }
 
@@ -81,6 +77,9 @@ class PageRepo
     public function findPageUsingOldSlug($pageSlug, $bookSlug)
     {
         $revision = $this->pageRevision->where('slug', '=', $pageSlug)
+            ->whereHas('page', function($query) {
+                $this->restrictionService->enforcePageRestrictions($query);
+            })
             ->where('book_slug', '=', $bookSlug)->orderBy('created_at', 'desc')
             ->with('page')->first();
         return $revision !== null ? $revision->page : null;
@@ -211,7 +210,7 @@ class PageRepo
         if (!empty($term)) {
             $terms = array_merge($terms, explode(' ', $term));
         }
-        $pages = $this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms)
+        $pages = $this->restrictionService->enforcePageRestrictions($this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms))
             ->paginate($count)->appends($paginationAppends);
 
         // Add highlights to page text.
@@ -249,7 +248,7 @@ class PageRepo
      */
     public function searchForImage($imageString)
     {
-        $pages = $this->page->where('html', 'like', '%' . $imageString . '%')->get();
+        $pages = $this->pageQuery()->where('html', 'like', '%' . $imageString . '%')->get();
         foreach ($pages as $page) {
             $page->url = $page->getUrl();
             $page->html = '';
@@ -395,6 +394,7 @@ class PageRepo
         Activity::removeEntity($page);
         $page->views()->delete();
         $page->revisions()->delete();
+        $page->restrictions()->delete();
         $page->delete();
     }
 
@@ -404,7 +404,7 @@ class PageRepo
      */
     public function getRecentlyCreatedPaginated($count = 20)
     {
-        return $this->page->orderBy('created_at', 'desc')->paginate($count);
+        return $this->pageQuery()->orderBy('created_at', 'desc')->paginate($count);
     }
 
     /**
@@ -413,7 +413,30 @@ class PageRepo
      */
     public function getRecentlyUpdatedPaginated($count = 20)
     {
-        return $this->page->orderBy('updated_at', 'desc')->paginate($count);
+        return $this->pageQuery()->orderBy('updated_at', 'desc')->paginate($count);
+    }
+
+    /**
+     * Updates pages restrictions from a request
+     * @param $request
+     * @param $page
+     */
+    public function updateRestrictionsFromRequest($request, $page)
+    {
+        // TODO - extract into shared repo
+        $page->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
+        $page->restrictions()->delete();
+        if ($request->has('restrictions')) {
+            foreach($request->get('restrictions') as $roleId => $restrictions) {
+                foreach ($restrictions as $action => $value) {
+                    $page->restrictions()->create([
+                        'role_id' => $roleId,
+                        'action'  => strtolower($action)
+                    ]);
+                }
+            }
+        }
+        $page->save();
     }
 
 }