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;
{
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');
}
/**
*/
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);
}
/**
* @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;
}
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;
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.
*/
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 = '';
Activity::removeEntity($page);
$page->views()->delete();
$page->revisions()->delete();
+ $page->restrictions()->delete();
$page->delete();
}
*/
public function getRecentlyCreatedPaginated($count = 20)
{
- return $this->page->orderBy('created_at', 'desc')->paginate($count);
+ return $this->pageQuery()->orderBy('created_at', 'desc')->paginate($count);
}
/**
*/
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();
}
}