{
protected $fillable = ['name', 'description', 'priority', 'book_id'];
+ protected $with = ['book'];
+
/**
* Get the book this chapter is within.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
use BookStack\Exceptions\FileUploadException;
use BookStack\Attachment;
+use BookStack\Repos\EntityRepo;
use BookStack\Repos\PageRepo;
use BookStack\Services\AttachmentService;
use Illuminate\Http\Request;
protected $attachmentService;
protected $attachment;
protected $pageRepo;
+ protected $entityRepo;
/**
* AttachmentController constructor.
* @param Attachment $attachment
* @param PageRepo $pageRepo
*/
- public function __construct(AttachmentService $attachmentService, Attachment $attachment, PageRepo $pageRepo)
+ public function __construct(AttachmentService $attachmentService, Attachment $attachment, EntityRepo $entityRepo, PageRepo $pageRepo)
{
$this->attachmentService = $attachmentService;
$this->attachment = $attachment;
+ // TODO - Remove this
$this->pageRepo = $pageRepo;
+ $this->entityRepo = $entityRepo;
parent::__construct();
}
]);
$pageId = $request->get('uploaded_to');
- $page = $this->pageRepo->getById($pageId, true);
+ $page = $this->entityRepo->getById('page', $pageId, true);
$this->checkPermission('attachment-create-all');
$this->checkOwnablePermission('page-update', $page);
]);
$pageId = $request->get('uploaded_to');
- $page = $this->pageRepo->getById($pageId, true);
+ $page = $this->entityRepo->getById('page', $pageId, true);
$attachment = $this->attachment->findOrFail($attachmentId);
$this->checkOwnablePermission('page-update', $page);
]);
$pageId = $request->get('uploaded_to');
- $page = $this->pageRepo->getById($pageId, true);
+ $page = $this->entityRepo->getById('page', $pageId, true);
$attachment = $this->attachment->findOrFail($attachmentId);
$this->checkOwnablePermission('page-update', $page);
]);
$pageId = $request->get('uploaded_to');
- $page = $this->pageRepo->getById($pageId, true);
+ $page = $this->entityRepo->getById('page', $pageId, true);
$this->checkPermission('attachment-create-all');
$this->checkOwnablePermission('page-update', $page);
*/
public function listForPage($pageId)
{
- $page = $this->pageRepo->getById($pageId, true);
+ $page = $this->entityRepo->getById('page', $pageId, true);
$this->checkOwnablePermission('page-view', $page);
return response()->json($page->attachments);
}
'files' => 'required|array',
'files.*.id' => 'required|integer',
]);
- $page = $this->pageRepo->getById($pageId);
+ $page = $this->entityRepo->getById('page', $pageId);
$this->checkOwnablePermission('page-update', $page);
$attachments = $request->get('files');
public function get($attachmentId)
{
$attachment = $this->attachment->findOrFail($attachmentId);
- $page = $this->pageRepo->getById($attachment->uploaded_to);
+ $page = $this->entityRepo->getById('page', $attachment->uploaded_to);
$this->checkOwnablePermission('page-view', $page);
if ($attachment->external) {
<?php namespace BookStack\Http\Controllers;
use Activity;
+use BookStack\Repos\EntityRepo;
use BookStack\Repos\UserRepo;
use Illuminate\Http\Request;
use BookStack\Http\Requests;
class BookController extends Controller
{
+ protected $entityRepo;
protected $bookRepo;
protected $pageRepo;
protected $chapterRepo;
* @param ChapterRepo $chapterRepo
* @param UserRepo $userRepo
*/
- public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
+ public function __construct(EntityRepo $entityRepo, BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
{
+ $this->entityRepo = $entityRepo;
+ // TODO - Remove below
$this->bookRepo = $bookRepo;
$this->pageRepo = $pageRepo;
$this->chapterRepo = $chapterRepo;
*/
public function index()
{
- $books = $this->bookRepo->getAllPaginated(10);
- $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
- $popular = $this->bookRepo->getPopular(4, 0);
+ $books = $this->entityRepo->getAllPaginated('book', 10);
+ $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
+ $popular = $this->entityRepo->getPopular('book', 4, 0);
$this->setPageTitle('Books');
return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
}
*/
public function show($slug)
{
- $book = $this->bookRepo->getBySlug($slug);
+ $book = $this->entityRepo->getBySlug('book', $slug);
$this->checkOwnablePermission('book-view', $book);
$bookChildren = $this->bookRepo->getChildren($book);
Views::add($book);
*/
public function edit($slug)
{
- $book = $this->bookRepo->getBySlug($slug);
+ $book = $this->entityRepo->getBySlug('book', $slug);
$this->checkOwnablePermission('book-update', $book);
$this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
return view('books/edit', ['book' => $book, 'current' => $book]);
*/
public function update(Request $request, $slug)
{
- $book = $this->bookRepo->getBySlug($slug);
+ $book = $this->entityRepo->getBySlug('book', $slug);
$this->checkOwnablePermission('book-update', $book);
$this->validate($request, [
'name' => 'required|string|max:255',
*/
public function showDelete($bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('book-delete', $book);
$this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
return view('books/delete', ['book' => $book, 'current' => $book]);
*/
public function sort($bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('book-update', $book);
$bookChildren = $this->bookRepo->getChildren($book, true);
- $books = $this->bookRepo->getAll(false);
+ $books = $this->entityRepo->getAll('book', false);
$this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
}
*/
public function getSortItem($bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$bookChildren = $this->bookRepo->getChildren($book);
return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
}
*/
public function saveSort($bookSlug, Request $request)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('book-update', $book);
// Return if no map sent
$priority = $bookChild->sort;
$id = intval($bookChild->id);
$isPage = $bookChild->type == 'page';
- $bookId = $this->bookRepo->exists($bookChild->book) ? intval($bookChild->book) : $defaultBookId;
+ $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
$chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
- $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
+ $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
// Update models only if there's a change in parent chain or ordering.
if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
// Add activity for books
foreach ($sortedBooks as $bookId) {
- $updatedBook = $this->bookRepo->getById($bookId);
+ $updatedBook = $this->entityRepo->getById('book', $bookId);
Activity::add($updatedBook, 'book_sort', $updatedBook->id);
}
*/
public function destroy($bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('book-delete', $book);
Activity::addMessage('book_delete', 0, $book->name);
Activity::removeEntity($book);
*/
public function showRestrict($bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $book);
$roles = $this->userRepo->getRestrictableRoles();
return view('books/restrictions', [
*/
public function restrict($bookSlug, Request $request)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $book);
$this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
session()->flash('success', trans('entities.books_permissions_updated'));
<?php namespace BookStack\Http\Controllers;
use Activity;
+use BookStack\Repos\EntityRepo;
use BookStack\Repos\UserRepo;
use Illuminate\Http\Request;
use BookStack\Repos\BookRepo;
protected $bookRepo;
protected $chapterRepo;
protected $userRepo;
+ protected $entityRepo;
/**
* ChapterController constructor.
+ * @param EntityRepo $entityRepo
* @param BookRepo $bookRepo
* @param ChapterRepo $chapterRepo
* @param UserRepo $userRepo
*/
- public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
+ public function __construct(EntityRepo $entityRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
{
+ $this->entityRepo = $entityRepo;
+ // TODO - Remove below
$this->bookRepo = $bookRepo;
$this->chapterRepo = $chapterRepo;
$this->userRepo = $userRepo;
*/
public function create($bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('chapter-create', $book);
$this->setPageTitle(trans('entities.chapters_create'));
return view('chapters/create', ['book' => $book, 'current' => $book]);
'name' => 'required|string|max:255'
]);
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('chapter-create', $book);
$input = $request->all();
*/
public function show($bookSlug, $chapterSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$this->checkOwnablePermission('chapter-view', $chapter);
- $sidebarTree = $this->bookRepo->getChildren($book);
+ $sidebarTree = $this->bookRepo->getChildren($chapter->book);
Views::add($chapter);
$this->setPageTitle($chapter->getShortName());
$pages = $this->chapterRepo->getChildren($chapter);
return view('chapters/show', [
- 'book' => $book,
+ 'book' => $chapter->book,
'chapter' => $chapter,
'current' => $chapter,
'sidebarTree' => $sidebarTree,
*/
public function edit($bookSlug, $chapterSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$this->checkOwnablePermission('chapter-update', $chapter);
$this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
- return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
+ return view('chapters/edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
}
/**
*/
public function update(Request $request, $bookSlug, $chapterSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$this->checkOwnablePermission('chapter-update', $chapter);
if ($chapter->name !== $request->get('name')) {
- $chapter->slug = $this->chapterRepo->findSuitableSlug($request->get('name'), $book->id, $chapter->id);
+ $chapter->slug = $this->chapterRepo->findSuitableSlug($request->get('name'), $chapter->book->id, $chapter->id);
}
$chapter->fill($request->all());
$chapter->updated_by = user()->id;
$chapter->save();
- Activity::add($chapter, 'chapter_update', $book->id);
+ Activity::add($chapter, 'chapter_update', $chapter->book->id);
return redirect($chapter->getUrl());
}
*/
public function showDelete($bookSlug, $chapterSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$this->checkOwnablePermission('chapter-delete', $chapter);
$this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
- return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
+ return view('chapters/delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
}
/**
*/
public function destroy($bookSlug, $chapterSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
+ $book = $chapter->book;
$this->checkOwnablePermission('chapter-delete', $chapter);
Activity::addMessage('chapter_delete', $book->id, $chapter->name);
$this->chapterRepo->destroy($chapter);
* @throws \BookStack\Exceptions\NotFoundException
*/
public function showMove($bookSlug, $chapterSlug) {
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
$this->checkOwnablePermission('chapter-update', $chapter);
return view('chapters/move', [
'chapter' => $chapter,
- 'book' => $book
+ 'book' => $chapter->book
]);
}
* @throws \BookStack\Exceptions\NotFoundException
*/
public function move($bookSlug, $chapterSlug, Request $request) {
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$this->checkOwnablePermission('chapter-update', $chapter);
$entitySelection = $request->get('entity_selection', null);
$parent = false;
if ($entityType == 'book') {
- $parent = $this->bookRepo->getById($entityId);
+ $parent = $this->entityRepo->getById('book', $entityId);
}
if ($parent === false || $parent === null) {
*/
public function showRestrict($bookSlug, $chapterSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $chapter);
$roles = $this->userRepo->getRestrictableRoles();
return view('chapters/restrictions', [
*/
public function restrict($bookSlug, $chapterSlug, Request $request)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $chapter);
$this->chapterRepo->updateEntityPermissionsFromRequest($request, $chapter);
session()->flash('success', trans('entities.chapters_permissions_success'));
use Activity;
use BookStack\Repos\EntityRepo;
use BookStack\Http\Requests;
+use Illuminate\Http\Response;
use Views;
class HomeController extends Controller
$activity = Activity::latest(10);
$draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
$recentFactor = count($draftPages) > 0 ? 0.5 : 1;
- $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreatedBooks(10*$recentFactor);
- $recentlyCreatedPages = $this->entityRepo->getRecentlyCreatedPages(5);
- $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdatedPages(5);
+ $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 10*$recentFactor);
+ $recentlyCreatedPages = $this->entityRepo->getRecentlyCreated('page', 5);
+ $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 5);
return view('home', [
'activity' => $activity,
'recents' => $recents,
use Activity;
use BookStack\Exceptions\NotFoundException;
+use BookStack\Repos\EntityRepo;
use BookStack\Repos\UserRepo;
use BookStack\Services\ExportService;
use Carbon\Carbon;
use Illuminate\Http\Request;
-use BookStack\Http\Requests;
use BookStack\Repos\BookRepo;
use BookStack\Repos\ChapterRepo;
use BookStack\Repos\PageRepo;
use Illuminate\Http\Response;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Views;
use GatherContent\Htmldiff\Htmldiff;
class PageController extends Controller
{
+ protected $entityRepo;
protected $pageRepo;
protected $bookRepo;
protected $chapterRepo;
* @param ExportService $exportService
* @param UserRepo $userRepo
*/
- public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService, UserRepo $userRepo)
+ public function __construct(EntityRepo $entityRepo, PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService, UserRepo $userRepo)
{
+ $this->entityRepo = $entityRepo;
+ // TODO - remove below;
$this->pageRepo = $pageRepo;
$this->bookRepo = $bookRepo;
$this->chapterRepo = $chapterRepo;
*/
public function create($bookSlug, $chapterSlug = null)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
+ $chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
$parent = $chapter ? $chapter : $book;
$this->checkOwnablePermission('page-create', $parent);
'name' => 'required|string|max:255'
]);
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
+ $chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
$parent = $chapter ? $chapter : $book;
$this->checkOwnablePermission('page-create', $parent);
*/
public function editDraft($bookSlug, $pageId)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $draft = $this->pageRepo->getById($pageId, true);
- $this->checkOwnablePermission('page-create', $book);
+ $draft = $this->entityRepo->getById('page', $pageId, true);
+ $this->checkOwnablePermission('page-create', $draft->book);
$this->setPageTitle(trans('entities.pages_edit_draft'));
$draftsEnabled = $this->signedIn;
return view('pages/edit', [
'page' => $draft,
- 'book' => $book,
+ 'book' => $draft->book,
'isDraft' => true,
'draftsEnabled' => $draftsEnabled
]);
]);
$input = $request->all();
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
- $draftPage = $this->pageRepo->getById($pageId, true);
+ $draftPage = $this->entityRepo->getById('page', $pageId, true);
$chapterId = intval($draftPage->chapter_id);
- $parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book;
+ $parent = $chapterId !== 0 ? $this->entityRepo->getById('chapter', $chapterId) : $book;
$this->checkOwnablePermission('page-create', $parent);
if ($parent->isA('chapter')) {
/**
* Display the specified page.
- * If the page is not found via the slug the
- * revisions are searched for a match.
+ * If the page is not found via the slug the revisions are searched for a match.
* @param string $bookSlug
* @param string $pageSlug
* @return Response
*/
public function show($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
-
try {
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
} catch (NotFoundException $e) {
$page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug);
if ($page === null) abort(404);
$this->checkOwnablePermission('page-view', $page);
- $sidebarTree = $this->bookRepo->getChildren($book);
+ $sidebarTree = $this->bookRepo->getChildren($page->book);
$pageNav = $this->pageRepo->getPageNav($page);
Views::add($page);
$this->setPageTitle($page->getShortName());
- return view('pages/show', ['page' => $page, 'book' => $book,
+ return view('pages/show', ['page' => $page, 'book' => $page->book,
'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]);
}
*/
public function getPageAjax($pageId)
{
- $page = $this->pageRepo->getById($pageId);
+ $page = $this->entityRepo->getById('page', $pageId);
return response()->json($page);
}
*/
public function edit($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->checkOwnablePermission('page-update', $page);
$this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
$page->isDraft = false;
$draftsEnabled = $this->signedIn;
return view('pages/edit', [
'page' => $page,
- 'book' => $book,
+ 'book' => $page->book,
'current' => $page,
'draftsEnabled' => $draftsEnabled
]);
$this->validate($request, [
'name' => 'required|string|max:255'
]);
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->checkOwnablePermission('page-update', $page);
- $this->pageRepo->updatePage($page, $book->id, $request->all());
- Activity::add($page, 'page_update', $book->id);
+ $this->pageRepo->updatePage($page, $page->book->id, $request->all());
+ Activity::add($page, 'page_update', $page->book->id);
return redirect($page->getUrl());
}
*/
public function saveDraft(Request $request, $pageId)
{
- $page = $this->pageRepo->getById($pageId, true);
+ $page = $this->entityRepo->getById('page', $pageId, true);
$this->checkOwnablePermission('page-update', $page);
if (!$this->signedIn) {
*/
public function redirectFromLink($pageId)
{
- $page = $this->pageRepo->getById($pageId);
+ $page = $this->entityRepo->getById('page', $pageId);
return redirect($page->getUrl());
}
*/
public function showDelete($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->checkOwnablePermission('page-delete', $page);
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
- return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
+ return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
}
*/
public function showDeleteDraft($bookSlug, $pageId)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getById($pageId, true);
+ $page = $this->entityRepo->getById('page', $pageId, true);
$this->checkOwnablePermission('page-update', $page);
$this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
- return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
+ return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
}
/**
*/
public function destroy($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
+ $book = $page->book;
$this->checkOwnablePermission('page-delete', $page);
Activity::addMessage('page_delete', $book->id, $page->name);
session()->flash('success', trans('entities.pages_delete_success'));
*/
public function destroyDraft($bookSlug, $pageId)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getById($pageId, true);
+ $page = $this->entityRepo->getById('page', $pageId, true);
+ $book = $page->book;
$this->checkOwnablePermission('page-update', $page);
session()->flash('success', trans('entities.pages_delete_draft_success'));
$this->pageRepo->destroy($page);
*/
public function showRevisions($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
- return view('pages/revisions', ['page' => $page, 'book' => $book, 'current' => $page]);
+ return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
}
/**
*/
public function showRevision($bookSlug, $pageSlug, $revisionId)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$revision = $this->pageRepo->getRevisionById($revisionId);
$page->fill($revision->toArray());
return view('pages/revision', [
'page' => $page,
- 'book' => $book,
+ 'book' => $page->book,
]);
}
*/
public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$revision = $this->pageRepo->getRevisionById($revisionId);
$prev = $revision->getPrevious();
return view('pages/revision', [
'page' => $page,
- 'book' => $book,
+ 'book' => $page->book,
'diff' => $diff,
]);
}
*/
public function restoreRevision($bookSlug, $pageSlug, $revisionId)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->checkOwnablePermission('page-update', $page);
- $page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
- Activity::add($page, 'page_restore', $book->id);
+ $page = $this->pageRepo->restoreRevision($page, $page->book, $revisionId);
+ Activity::add($page, 'page_restore', $page->book->id);
return redirect($page->getUrl());
}
*/
public function exportPdf($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$pdfContent = $this->exportService->pageToPdf($page);
return response()->make($pdfContent, 200, [
'Content-Type' => 'application/octet-stream',
*/
public function exportHtml($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$containedHtml = $this->exportService->pageToContainedHtml($page);
return response()->make($containedHtml, 200, [
'Content-Type' => 'application/octet-stream',
*/
public function exportPlainText($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$containedHtml = $this->exportService->pageToPlainText($page);
return response()->make($containedHtml, 200, [
'Content-Type' => 'application/octet-stream',
*/
public function showRestrict($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $page);
$roles = $this->userRepo->getRestrictableRoles();
return view('pages/restrictions', [
*/
public function showMove($bookSlug, $pageSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->checkOwnablePermission('page-update', $page);
return view('pages/move', [
- 'book' => $book,
+ 'book' => $page->book,
'page' => $page
]);
}
*/
public function move($bookSlug, $pageSlug, Request $request)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->checkOwnablePermission('page-update', $page);
$entitySelection = $request->get('entity_selection', null);
$entityType = $stringExploded[0];
$entityId = intval($stringExploded[1]);
- $parent = false;
-
- if ($entityType == 'chapter') {
- $parent = $this->chapterRepo->getById($entityId);
- } else if ($entityType == 'book') {
- $parent = $this->bookRepo->getById($entityId);
- }
- if ($parent === false || $parent === null) {
+ try {
+ $parent = $this->entityRepo->getById($entityType, $entityId);
+ } catch (\Exception $e) {
session()->flash(trans('entities.selected_book_chapter_not_found'));
return redirect()->back();
}
*/
public function restrict($bookSlug, $pageSlug, Request $request)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
- $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $page);
$this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
session()->flash('success', trans('entities.pages_permissions_success'));
protected $simpleAttributes = ['name', 'id', 'slug'];
+ protected $with = ['book'];
+
/**
* Converts this page into a simplified array.
* @return mixed
<?php namespace BookStack\Repos;
-use Alpha\B;
-use BookStack\Exceptions\NotFoundException;
-use Illuminate\Database\Eloquent\Collection;
-use Illuminate\Support\Str;
use BookStack\Book;
-use Views;
class BookRepo extends EntityRepo
{
parent::__construct();
}
- /**
- * Base query for getting books.
- * Takes into account any restrictions.
- * @return mixed
- */
- private function bookQuery()
- {
- return $this->permissionService->enforceBookRestrictions($this->book, 'view');
- }
-
- /**
- * Get the book that has the given id.
- * @param $id
- * @return mixed
- */
- public function getById($id)
- {
- return $this->bookQuery()->findOrFail($id);
- }
-
- /**
- * Get all books, Limited by count.
- * @param int $count
- * @return mixed
- */
- public function getAll($count = 10)
- {
- $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
- if (!$count) return $bookQuery->get();
- return $bookQuery->take($count)->get();
- }
-
- /**
- * Get all books paginated.
- * @param int $count
- * @return mixed
- */
- public function getAllPaginated($count = 10)
- {
- return $this->bookQuery()
- ->orderBy('name', 'asc')->paginate($count);
- }
-
-
- /**
- * Get the latest books.
- * @param int $count
- * @return mixed
- */
- public function getLatest($count = 10)
- {
- return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
- }
-
- /**
- * Gets the most recently viewed for a user.
- * @param int $count
- * @param int $page
- * @return mixed
- */
- public function getRecentlyViewed($count = 10, $page = 0)
- {
- return Views::getUserRecentlyViewed($count, $page, $this->book);
- }
-
- /**
- * Gets the most viewed books.
- * @param int $count
- * @param int $page
- * @return mixed
- */
- public function getPopular($count = 10, $page = 0)
- {
- return Views::getPopular($count, $page, $this->book);
- }
-
- /**
- * Get a book by slug
- * @param $slug
- * @return mixed
- * @throws NotFoundException
- */
- public function getBySlug($slug)
- {
- $book = $this->bookQuery()->where('slug', '=', $slug)->first();
- if ($book === null) throw new NotFoundException(trans('errors.book_not_found'));
- return $book;
- }
-
- /**
- * Checks if a book exists.
- * @param $id
- * @return bool
- */
- public function exists($id)
- {
- return $this->bookQuery()->where('id', '=', $id)->exists();
- }
-
/**
* Get a new book instance from request input.
* @param array $input
parent::__construct();
}
- /**
- * Base query for getting chapters, Takes permissions into account.
- * @return mixed
- */
- private function chapterQuery()
- {
- return $this->permissionService->enforceChapterRestrictions($this->chapter, 'view');
- }
-
- /**
- * Check if an id exists.
- * @param $id
- * @return bool
- */
- public function idExists($id)
- {
- return $this->chapterQuery()->where('id', '=', $id)->count() > 0;
- }
-
- /**
- * Get a chapter by a specific id.
- * @param $id
- * @return mixed
- */
- public function getById($id)
- {
- return $this->chapterQuery()->findOrFail($id);
- }
-
- /**
- * Get all chapters.
- * @return \Illuminate\Database\Eloquent\Collection|static[]
- */
- public function getAll()
- {
- return $this->chapterQuery()->all();
- }
-
- /**
- * Get a chapter that has the given slug within the given book.
- * @param $slug
- * @param $bookId
- * @return mixed
- * @throws NotFoundException
- */
- public function getBySlug($slug, $bookId)
- {
- $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
- if ($chapter === null) throw new NotFoundException(trans('errors.chapter_not_found'));
- return $chapter;
- }
-
/**
* Get the child items for a chapter
* @param Chapter $chapter
use BookStack\Book;
use BookStack\Chapter;
use BookStack\Entity;
+use BookStack\Exceptions\NotFoundException;
use BookStack\Page;
use BookStack\Services\PermissionService;
-use BookStack\User;
+use BookStack\Services\ViewService;
+use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
-use Illuminate\Support\Facades\Log;
class EntityRepo
{
*/
public $page;
+ /**
+ * Base entity instances keyed by type
+ * @var []Entity
+ */
+ protected $entities;
+
/**
* @var PermissionService
*/
protected $permissionService;
+ /**
+ * @var ViewService
+ */
+ protected $viewService;
+
/**
* Acceptable operators to be used in a query
* @var array
*/
public function __construct()
{
+ // TODO - Redo this to come via injection
$this->book = app(Book::class);
$this->chapter = app(Chapter::class);
$this->page = app(Page::class);
+ $this->entities = [
+ 'page' => $this->page,
+ 'chapter' => $this->chapter,
+ 'book' => $this->book
+ ];
+ $this->viewService = app(ViewService::class);
$this->permissionService = app(PermissionService::class);
}
/**
- * Get the latest books added to the system.
- * @param int $count
- * @param int $page
- * @param bool $additionalQuery
- * @return
+ * Get an entity instance via type.
+ * @param $type
+ * @return Entity
*/
- public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false)
+ protected function getEntity($type)
{
- $query = $this->permissionService->enforceBookRestrictions($this->book)
- ->orderBy('created_at', 'desc');
- if ($additionalQuery !== false && is_callable($additionalQuery)) {
- $additionalQuery($query);
+ return $this->entities[strtolower($type)];
+ }
+
+ /**
+ * Base query for searching entities via permission system
+ * @param string $type
+ * @param bool $allowDrafts
+ * @return \Illuminate\Database\Query\Builder
+ */
+ protected function entityQuery($type, $allowDrafts = false)
+ {
+ $q = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type), 'view');
+ if (strtolower($type) === 'page' && !$allowDrafts) {
+ $q = $q->where('draft', '=', false);
}
- return $query->skip($page * $count)->take($count)->get();
+ return $q;
}
/**
- * Get the most recently updated books.
- * @param $count
- * @param int $page
- * @return mixed
+ * Check if an entity with the given id exists.
+ * @param $type
+ * @param $id
+ * @return bool
*/
- public function getRecentlyUpdatedBooks($count = 20, $page = 0)
+ public function exists($type, $id)
{
- return $this->permissionService->enforceBookRestrictions($this->book)
- ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
+ return $this->entityQuery($type)->where('id', '=', $id)->exists();
}
/**
- * Get the latest pages added to the system.
+ * Get an entity by ID
+ * @param string $type
+ * @param integer $id
+ * @param bool $allowDrafts
+ * @return Entity
+ */
+ public function getById($type, $id, $allowDrafts = false)
+ {
+ return $this->entityQuery($type, $allowDrafts)->findOrFail($id);
+ }
+
+ /**
+ * Get an entity by its url slug.
+ * @param string $type
+ * @param string $slug
+ * @param string|bool $bookSlug
+ * @return Entity
+ * @throws NotFoundException
+ */
+ public function getBySlug($type, $slug, $bookSlug = false)
+ {
+ $q = $this->entityQuery($type)->where('slug', '=', $slug);
+ if (strtolower($type) === 'chapter' || strtolower($type) === 'page') {
+ $q = $q->where('book_id', '=', function($query) use ($bookSlug) {
+ $query->select('id')
+ ->from($this->book->getTable())
+ ->where('slug', '=', $bookSlug)->limit(1);
+ });
+ }
+ $entity = $q->first();
+ if ($entity === null) throw new NotFoundException(trans('errors.' . strtolower($type) . '_not_found'));
+ return $entity;
+ }
+
+ /**
+ * Get all entities of a type limited by count unless count if false.
+ * @param string $type
+ * @param integer|bool $count
+ * @return Collection
+ */
+ public function getAll($type, $count = 20)
+ {
+ $q = $this->entityQuery($type)->orderBy('name', 'asc');
+ if ($count !== false) $q = $q->take($count);
+ return $q->get();
+ }
+
+ /**
+ * Get all entities in a paginated format
+ * @param $type
+ * @param int $count
+ * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
+ */
+ public function getAllPaginated($type, $count = 10)
+ {
+ return $this->entityQuery($type)->orderBy('name', 'asc')->paginate($count);
+ }
+
+ /**
+ * Get the most recently created entities of the given type.
+ * @param string $type
* @param int $count
* @param int $page
- * @param bool $additionalQuery
- * @return
+ * @param bool|callable $additionalQuery
*/
- public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false)
+ public function getRecentlyCreated($type, $count = 20, $page = 0, $additionalQuery = false)
{
- $query = $this->permissionService->enforcePageRestrictions($this->page)
- ->orderBy('created_at', 'desc')->where('draft', '=', false);
+ $query = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type))
+ ->orderBy('created_at', 'desc');
+ if (strtolower($type) === 'page') $query = $query->where('draft', '=', false);
if ($additionalQuery !== false && is_callable($additionalQuery)) {
$additionalQuery($query);
}
- return $query->with('book')->skip($page * $count)->take($count)->get();
+ return $query->skip($page * $count)->take($count)->get();
}
/**
- * Get the latest chapters added to the system.
+ * Get the most recently updated entities of the given type.
+ * @param string $type
* @param int $count
* @param int $page
- * @param bool $additionalQuery
- * @return
+ * @param bool|callable $additionalQuery
*/
- public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false)
+ public function getRecentlyUpdated($type, $count = 20, $page = 0, $additionalQuery = false)
{
- $query = $this->permissionService->enforceChapterRestrictions($this->chapter)
- ->orderBy('created_at', 'desc');
+ $query = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type))
+ ->orderBy('updated_at', 'desc');
+ if (strtolower($type) === 'page') $query = $query->where('draft', '=', false);
if ($additionalQuery !== false && is_callable($additionalQuery)) {
$additionalQuery($query);
}
}
/**
- * Get the most recently updated pages.
- * @param $count
+ * Get the most recently viewed entities.
+ * @param string|bool $type
+ * @param int $count
+ * @param int $page
+ * @return mixed
+ */
+ public function getRecentlyViewed($type, $count = 10, $page = 0)
+ {
+ $filter = is_bool($type) ? false : $this->getEntity($type);
+ return $this->viewService->getUserRecentlyViewed($count, $page, $filter);
+ }
+
+ /**
+ * Get the most popular entities base on all views.
+ * @param string|bool $type
+ * @param int $count
* @param int $page
* @return mixed
*/
- public function getRecentlyUpdatedPages($count = 20, $page = 0)
+ public function getPopular($type, $count = 10, $page = 0)
{
- return $this->permissionService->enforcePageRestrictions($this->page)
- ->where('draft', '=', false)
- ->orderBy('updated_at', 'desc')->with('book')->skip($page * $count)->take($count)->get();
+ $filter = is_bool($type) ? false : $this->getEntity($type);
+ return $this->viewService->getPopular($count, $page, $filter);
}
/**
return $query;
}
- /**
- * Get a page via a specific ID.
- * @param $id
- * @param bool $allowDrafts
- * @return Page
- */
- public function getById($id, $allowDrafts = false)
- {
- return $this->pageQuery($allowDrafts)->findOrFail($id);
- }
-
- /**
- * Get a page identified by the given slug.
- * @param $slug
- * @param $bookId
- * @return Page
- * @throws NotFoundException
- */
- public function getBySlug($slug, $bookId)
- {
- $page = $this->pageQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
- if ($page === null) throw new NotFoundException(trans('errors.page_not_found'));
- return $page;
- }
-
/**
* Search through page revisions and retrieve
* the last page in the current book that
public function getRecentlyCreated(User $user, $count = 20)
{
return [
- 'pages' => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) {
+ 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
$query->where('created_by', '=', $user->id);
}),
- 'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) {
+ 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
$query->where('created_by', '=', $user->id);
}),
- 'books' => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) {
+ 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
$query->where('created_by', '=', $user->id);
})
];
use BookStack\Page;
use BookStack\Role;
use BookStack\User;
+use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
-use Illuminate\Support\Facades\Log;
class PermissionService
{
*/
public function enforcePageRestrictions($query, $action = 'view')
{
- // Prevent drafts being visible to others.
- $query = $query->where(function ($query) {
- $query->where('draft', '=', false);
- if ($this->currentUser()) {
- $query->orWhere(function ($query) {
- $query->where('draft', '=', true)->where('created_by', '=', $this->currentUser()->id);
- });
- }
- });
-
- return $this->enforceEntityRestrictions($query, $action);
+ // TODO - remove this
+ return $this->enforceEntityRestrictions('page', $query, $action);
}
/**
*/
public function enforceChapterRestrictions($query, $action = 'view')
{
- return $this->enforceEntityRestrictions($query, $action);
+ // TODO - remove this
+ return $this->enforceEntityRestrictions('chapter', $query, $action);
}
/**
*/
public function enforceBookRestrictions($query, $action = 'view')
{
- return $this->enforceEntityRestrictions($query, $action);
+ // TODO - remove this
+ return $this->enforceEntityRestrictions('book', $query, $action);
}
/**
* Add restrictions for a generic entity
- * @param $query
+ * @param string $entityType
+ * @param Builder|Entity $query
* @param string $action
* @return mixed
*/
- public function enforceEntityRestrictions($query, $action = 'view')
+ public function enforceEntityRestrictions($entityType, $query, $action = 'view')
{
+ if (strtolower($entityType) === 'page') {
+ // Prevent drafts being visible to others.
+ $query = $query->where(function ($query) {
+ $query->where('draft', '=', false);
+ if ($this->currentUser()) {
+ $query->orWhere(function ($query) {
+ $query->where('draft', '=', true)->where('created_by', '=', $this->currentUser()->id);
+ });
+ }
+ });
+ }
+
if ($this->isAdmin()) {
$this->clean();
return $query;
}
+
$this->currentAction = $action;
return $this->entityRestrictionQuery($query);
}
$this->forceVisit($bookUrl)
->see('Book not found');
$this->forceVisit($bookPage->getUrl())
- ->see('Book not found');
+ ->see('Page not found');
$this->forceVisit($bookChapter->getUrl())
- ->see('Book not found');
+ ->see('Chapter not found');
$this->setEntityRestrictions($book, ['view']);