mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-26 20:22:18 +00:00
Started refactor to merge entity repos
This commit is contained in:
parent
f91f33c236
commit
7f9de2c8ab
@ -5,6 +5,8 @@ class Chapter extends Entity
|
|||||||
{
|
{
|
||||||
protected $fillable = ['name', 'description', 'priority', 'book_id'];
|
protected $fillable = ['name', 'description', 'priority', 'book_id'];
|
||||||
|
|
||||||
|
protected $with = ['book'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the book this chapter is within.
|
* Get the book this chapter is within.
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use BookStack\Exceptions\FileUploadException;
|
use BookStack\Exceptions\FileUploadException;
|
||||||
use BookStack\Attachment;
|
use BookStack\Attachment;
|
||||||
|
use BookStack\Repos\EntityRepo;
|
||||||
use BookStack\Repos\PageRepo;
|
use BookStack\Repos\PageRepo;
|
||||||
use BookStack\Services\AttachmentService;
|
use BookStack\Services\AttachmentService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@ -11,6 +12,7 @@ class AttachmentController extends Controller
|
|||||||
protected $attachmentService;
|
protected $attachmentService;
|
||||||
protected $attachment;
|
protected $attachment;
|
||||||
protected $pageRepo;
|
protected $pageRepo;
|
||||||
|
protected $entityRepo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AttachmentController constructor.
|
* AttachmentController constructor.
|
||||||
@ -18,11 +20,13 @@ class AttachmentController extends Controller
|
|||||||
* @param Attachment $attachment
|
* @param Attachment $attachment
|
||||||
* @param PageRepo $pageRepo
|
* @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->attachmentService = $attachmentService;
|
||||||
$this->attachment = $attachment;
|
$this->attachment = $attachment;
|
||||||
|
// TODO - Remove this
|
||||||
$this->pageRepo = $pageRepo;
|
$this->pageRepo = $pageRepo;
|
||||||
|
$this->entityRepo = $entityRepo;
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +44,7 @@ class AttachmentController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$pageId = $request->get('uploaded_to');
|
$pageId = $request->get('uploaded_to');
|
||||||
$page = $this->pageRepo->getById($pageId, true);
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||||
|
|
||||||
$this->checkPermission('attachment-create-all');
|
$this->checkPermission('attachment-create-all');
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
@ -70,7 +74,7 @@ class AttachmentController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$pageId = $request->get('uploaded_to');
|
$pageId = $request->get('uploaded_to');
|
||||||
$page = $this->pageRepo->getById($pageId, true);
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||||
$attachment = $this->attachment->findOrFail($attachmentId);
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
||||||
|
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
@ -106,7 +110,7 @@ class AttachmentController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$pageId = $request->get('uploaded_to');
|
$pageId = $request->get('uploaded_to');
|
||||||
$page = $this->pageRepo->getById($pageId, true);
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||||
$attachment = $this->attachment->findOrFail($attachmentId);
|
$attachment = $this->attachment->findOrFail($attachmentId);
|
||||||
|
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
@ -134,7 +138,7 @@ class AttachmentController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$pageId = $request->get('uploaded_to');
|
$pageId = $request->get('uploaded_to');
|
||||||
$page = $this->pageRepo->getById($pageId, true);
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||||
|
|
||||||
$this->checkPermission('attachment-create-all');
|
$this->checkPermission('attachment-create-all');
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
@ -153,7 +157,7 @@ class AttachmentController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function listForPage($pageId)
|
public function listForPage($pageId)
|
||||||
{
|
{
|
||||||
$page = $this->pageRepo->getById($pageId, true);
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||||
$this->checkOwnablePermission('page-view', $page);
|
$this->checkOwnablePermission('page-view', $page);
|
||||||
return response()->json($page->attachments);
|
return response()->json($page->attachments);
|
||||||
}
|
}
|
||||||
@ -170,7 +174,7 @@ class AttachmentController extends Controller
|
|||||||
'files' => 'required|array',
|
'files' => 'required|array',
|
||||||
'files.*.id' => 'required|integer',
|
'files.*.id' => 'required|integer',
|
||||||
]);
|
]);
|
||||||
$page = $this->pageRepo->getById($pageId);
|
$page = $this->entityRepo->getById('page', $pageId);
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
|
|
||||||
$attachments = $request->get('files');
|
$attachments = $request->get('files');
|
||||||
@ -186,7 +190,7 @@ class AttachmentController extends Controller
|
|||||||
public function get($attachmentId)
|
public function get($attachmentId)
|
||||||
{
|
{
|
||||||
$attachment = $this->attachment->findOrFail($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);
|
$this->checkOwnablePermission('page-view', $page);
|
||||||
|
|
||||||
if ($attachment->external) {
|
if ($attachment->external) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php namespace BookStack\Http\Controllers;
|
<?php namespace BookStack\Http\Controllers;
|
||||||
|
|
||||||
use Activity;
|
use Activity;
|
||||||
|
use BookStack\Repos\EntityRepo;
|
||||||
use BookStack\Repos\UserRepo;
|
use BookStack\Repos\UserRepo;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use BookStack\Http\Requests;
|
use BookStack\Http\Requests;
|
||||||
@ -13,6 +14,7 @@ use Views;
|
|||||||
class BookController extends Controller
|
class BookController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
|
protected $entityRepo;
|
||||||
protected $bookRepo;
|
protected $bookRepo;
|
||||||
protected $pageRepo;
|
protected $pageRepo;
|
||||||
protected $chapterRepo;
|
protected $chapterRepo;
|
||||||
@ -25,8 +27,10 @@ class BookController extends Controller
|
|||||||
* @param ChapterRepo $chapterRepo
|
* @param ChapterRepo $chapterRepo
|
||||||
* @param UserRepo $userRepo
|
* @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->bookRepo = $bookRepo;
|
||||||
$this->pageRepo = $pageRepo;
|
$this->pageRepo = $pageRepo;
|
||||||
$this->chapterRepo = $chapterRepo;
|
$this->chapterRepo = $chapterRepo;
|
||||||
@ -40,9 +44,9 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$books = $this->bookRepo->getAllPaginated(10);
|
$books = $this->entityRepo->getAllPaginated('book', 10);
|
||||||
$recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
|
$recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
|
||||||
$popular = $this->bookRepo->getPopular(4, 0);
|
$popular = $this->entityRepo->getPopular('book', 4, 0);
|
||||||
$this->setPageTitle('Books');
|
$this->setPageTitle('Books');
|
||||||
return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
|
return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
|
||||||
}
|
}
|
||||||
@ -83,7 +87,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function show($slug)
|
public function show($slug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($slug);
|
$book = $this->entityRepo->getBySlug('book', $slug);
|
||||||
$this->checkOwnablePermission('book-view', $book);
|
$this->checkOwnablePermission('book-view', $book);
|
||||||
$bookChildren = $this->bookRepo->getChildren($book);
|
$bookChildren = $this->bookRepo->getChildren($book);
|
||||||
Views::add($book);
|
Views::add($book);
|
||||||
@ -98,7 +102,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function edit($slug)
|
public function edit($slug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($slug);
|
$book = $this->entityRepo->getBySlug('book', $slug);
|
||||||
$this->checkOwnablePermission('book-update', $book);
|
$this->checkOwnablePermission('book-update', $book);
|
||||||
$this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
|
$this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
|
||||||
return view('books/edit', ['book' => $book, 'current' => $book]);
|
return view('books/edit', ['book' => $book, 'current' => $book]);
|
||||||
@ -112,7 +116,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function update(Request $request, $slug)
|
public function update(Request $request, $slug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($slug);
|
$book = $this->entityRepo->getBySlug('book', $slug);
|
||||||
$this->checkOwnablePermission('book-update', $book);
|
$this->checkOwnablePermission('book-update', $book);
|
||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
'name' => 'required|string|max:255',
|
'name' => 'required|string|max:255',
|
||||||
@ -130,7 +134,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showDelete($bookSlug)
|
public function showDelete($bookSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$this->checkOwnablePermission('book-delete', $book);
|
$this->checkOwnablePermission('book-delete', $book);
|
||||||
$this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
|
$this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
|
||||||
return view('books/delete', ['book' => $book, 'current' => $book]);
|
return view('books/delete', ['book' => $book, 'current' => $book]);
|
||||||
@ -143,10 +147,10 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function sort($bookSlug)
|
public function sort($bookSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$this->checkOwnablePermission('book-update', $book);
|
$this->checkOwnablePermission('book-update', $book);
|
||||||
$bookChildren = $this->bookRepo->getChildren($book, true);
|
$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()]));
|
$this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
|
||||||
return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
|
return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
|
||||||
}
|
}
|
||||||
@ -159,7 +163,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function getSortItem($bookSlug)
|
public function getSortItem($bookSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$bookChildren = $this->bookRepo->getChildren($book);
|
$bookChildren = $this->bookRepo->getChildren($book);
|
||||||
return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
|
return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
|
||||||
}
|
}
|
||||||
@ -172,7 +176,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function saveSort($bookSlug, Request $request)
|
public function saveSort($bookSlug, Request $request)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$this->checkOwnablePermission('book-update', $book);
|
$this->checkOwnablePermission('book-update', $book);
|
||||||
|
|
||||||
// Return if no map sent
|
// Return if no map sent
|
||||||
@ -191,9 +195,9 @@ class BookController extends Controller
|
|||||||
$priority = $bookChild->sort;
|
$priority = $bookChild->sort;
|
||||||
$id = intval($bookChild->id);
|
$id = intval($bookChild->id);
|
||||||
$isPage = $bookChild->type == 'page';
|
$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);
|
$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.
|
// 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)) {
|
if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
|
||||||
@ -212,7 +216,7 @@ class BookController extends Controller
|
|||||||
|
|
||||||
// Add activity for books
|
// Add activity for books
|
||||||
foreach ($sortedBooks as $bookId) {
|
foreach ($sortedBooks as $bookId) {
|
||||||
$updatedBook = $this->bookRepo->getById($bookId);
|
$updatedBook = $this->entityRepo->getById('book', $bookId);
|
||||||
Activity::add($updatedBook, 'book_sort', $updatedBook->id);
|
Activity::add($updatedBook, 'book_sort', $updatedBook->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,7 +233,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function destroy($bookSlug)
|
public function destroy($bookSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$this->checkOwnablePermission('book-delete', $book);
|
$this->checkOwnablePermission('book-delete', $book);
|
||||||
Activity::addMessage('book_delete', 0, $book->name);
|
Activity::addMessage('book_delete', 0, $book->name);
|
||||||
Activity::removeEntity($book);
|
Activity::removeEntity($book);
|
||||||
@ -244,7 +248,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showRestrict($bookSlug)
|
public function showRestrict($bookSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$this->checkOwnablePermission('restrictions-manage', $book);
|
$this->checkOwnablePermission('restrictions-manage', $book);
|
||||||
$roles = $this->userRepo->getRestrictableRoles();
|
$roles = $this->userRepo->getRestrictableRoles();
|
||||||
return view('books/restrictions', [
|
return view('books/restrictions', [
|
||||||
@ -262,7 +266,7 @@ class BookController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function restrict($bookSlug, Request $request)
|
public function restrict($bookSlug, Request $request)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$this->checkOwnablePermission('restrictions-manage', $book);
|
$this->checkOwnablePermission('restrictions-manage', $book);
|
||||||
$this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
|
$this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
|
||||||
session()->flash('success', trans('entities.books_permissions_updated'));
|
session()->flash('success', trans('entities.books_permissions_updated'));
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php namespace BookStack\Http\Controllers;
|
<?php namespace BookStack\Http\Controllers;
|
||||||
|
|
||||||
use Activity;
|
use Activity;
|
||||||
|
use BookStack\Repos\EntityRepo;
|
||||||
use BookStack\Repos\UserRepo;
|
use BookStack\Repos\UserRepo;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use BookStack\Repos\BookRepo;
|
use BookStack\Repos\BookRepo;
|
||||||
@ -14,15 +15,19 @@ class ChapterController extends Controller
|
|||||||
protected $bookRepo;
|
protected $bookRepo;
|
||||||
protected $chapterRepo;
|
protected $chapterRepo;
|
||||||
protected $userRepo;
|
protected $userRepo;
|
||||||
|
protected $entityRepo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ChapterController constructor.
|
* ChapterController constructor.
|
||||||
|
* @param EntityRepo $entityRepo
|
||||||
* @param BookRepo $bookRepo
|
* @param BookRepo $bookRepo
|
||||||
* @param ChapterRepo $chapterRepo
|
* @param ChapterRepo $chapterRepo
|
||||||
* @param UserRepo $userRepo
|
* @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->bookRepo = $bookRepo;
|
||||||
$this->chapterRepo = $chapterRepo;
|
$this->chapterRepo = $chapterRepo;
|
||||||
$this->userRepo = $userRepo;
|
$this->userRepo = $userRepo;
|
||||||
@ -36,7 +41,7 @@ class ChapterController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function create($bookSlug)
|
public function create($bookSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$this->checkOwnablePermission('chapter-create', $book);
|
$this->checkOwnablePermission('chapter-create', $book);
|
||||||
$this->setPageTitle(trans('entities.chapters_create'));
|
$this->setPageTitle(trans('entities.chapters_create'));
|
||||||
return view('chapters/create', ['book' => $book, 'current' => $book]);
|
return view('chapters/create', ['book' => $book, 'current' => $book]);
|
||||||
@ -54,7 +59,7 @@ class ChapterController extends Controller
|
|||||||
'name' => 'required|string|max:255'
|
'name' => 'required|string|max:255'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$this->checkOwnablePermission('chapter-create', $book);
|
$this->checkOwnablePermission('chapter-create', $book);
|
||||||
|
|
||||||
$input = $request->all();
|
$input = $request->all();
|
||||||
@ -72,15 +77,14 @@ class ChapterController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function show($bookSlug, $chapterSlug)
|
public function show($bookSlug, $chapterSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('chapter-view', $chapter);
|
$this->checkOwnablePermission('chapter-view', $chapter);
|
||||||
$sidebarTree = $this->bookRepo->getChildren($book);
|
$sidebarTree = $this->bookRepo->getChildren($chapter->book);
|
||||||
Views::add($chapter);
|
Views::add($chapter);
|
||||||
$this->setPageTitle($chapter->getShortName());
|
$this->setPageTitle($chapter->getShortName());
|
||||||
$pages = $this->chapterRepo->getChildren($chapter);
|
$pages = $this->chapterRepo->getChildren($chapter);
|
||||||
return view('chapters/show', [
|
return view('chapters/show', [
|
||||||
'book' => $book,
|
'book' => $chapter->book,
|
||||||
'chapter' => $chapter,
|
'chapter' => $chapter,
|
||||||
'current' => $chapter,
|
'current' => $chapter,
|
||||||
'sidebarTree' => $sidebarTree,
|
'sidebarTree' => $sidebarTree,
|
||||||
@ -96,11 +100,10 @@ class ChapterController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function edit($bookSlug, $chapterSlug)
|
public function edit($bookSlug, $chapterSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('chapter-update', $chapter);
|
$this->checkOwnablePermission('chapter-update', $chapter);
|
||||||
$this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
|
$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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -112,16 +115,15 @@ class ChapterController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function update(Request $request, $bookSlug, $chapterSlug)
|
public function update(Request $request, $bookSlug, $chapterSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('chapter-update', $chapter);
|
$this->checkOwnablePermission('chapter-update', $chapter);
|
||||||
if ($chapter->name !== $request->get('name')) {
|
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->fill($request->all());
|
||||||
$chapter->updated_by = user()->id;
|
$chapter->updated_by = user()->id;
|
||||||
$chapter->save();
|
$chapter->save();
|
||||||
Activity::add($chapter, 'chapter_update', $book->id);
|
Activity::add($chapter, 'chapter_update', $chapter->book->id);
|
||||||
return redirect($chapter->getUrl());
|
return redirect($chapter->getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,11 +135,10 @@ class ChapterController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showDelete($bookSlug, $chapterSlug)
|
public function showDelete($bookSlug, $chapterSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('chapter-delete', $chapter);
|
$this->checkOwnablePermission('chapter-delete', $chapter);
|
||||||
$this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
|
$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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -148,8 +149,8 @@ class ChapterController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function destroy($bookSlug, $chapterSlug)
|
public function destroy($bookSlug, $chapterSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
$book = $chapter->book;
|
||||||
$this->checkOwnablePermission('chapter-delete', $chapter);
|
$this->checkOwnablePermission('chapter-delete', $chapter);
|
||||||
Activity::addMessage('chapter_delete', $book->id, $chapter->name);
|
Activity::addMessage('chapter_delete', $book->id, $chapter->name);
|
||||||
$this->chapterRepo->destroy($chapter);
|
$this->chapterRepo->destroy($chapter);
|
||||||
@ -164,13 +165,12 @@ class ChapterController extends Controller
|
|||||||
* @throws \BookStack\Exceptions\NotFoundException
|
* @throws \BookStack\Exceptions\NotFoundException
|
||||||
*/
|
*/
|
||||||
public function showMove($bookSlug, $chapterSlug) {
|
public function showMove($bookSlug, $chapterSlug) {
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
|
||||||
$this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
|
$this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
|
||||||
$this->checkOwnablePermission('chapter-update', $chapter);
|
$this->checkOwnablePermission('chapter-update', $chapter);
|
||||||
return view('chapters/move', [
|
return view('chapters/move', [
|
||||||
'chapter' => $chapter,
|
'chapter' => $chapter,
|
||||||
'book' => $book
|
'book' => $chapter->book
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,8 +183,7 @@ class ChapterController extends Controller
|
|||||||
* @throws \BookStack\Exceptions\NotFoundException
|
* @throws \BookStack\Exceptions\NotFoundException
|
||||||
*/
|
*/
|
||||||
public function move($bookSlug, $chapterSlug, Request $request) {
|
public function move($bookSlug, $chapterSlug, Request $request) {
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('chapter-update', $chapter);
|
$this->checkOwnablePermission('chapter-update', $chapter);
|
||||||
|
|
||||||
$entitySelection = $request->get('entity_selection', null);
|
$entitySelection = $request->get('entity_selection', null);
|
||||||
@ -199,7 +198,7 @@ class ChapterController extends Controller
|
|||||||
$parent = false;
|
$parent = false;
|
||||||
|
|
||||||
if ($entityType == 'book') {
|
if ($entityType == 'book') {
|
||||||
$parent = $this->bookRepo->getById($entityId);
|
$parent = $this->entityRepo->getById('book', $entityId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($parent === false || $parent === null) {
|
if ($parent === false || $parent === null) {
|
||||||
@ -222,8 +221,7 @@ class ChapterController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showRestrict($bookSlug, $chapterSlug)
|
public function showRestrict($bookSlug, $chapterSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('restrictions-manage', $chapter);
|
$this->checkOwnablePermission('restrictions-manage', $chapter);
|
||||||
$roles = $this->userRepo->getRestrictableRoles();
|
$roles = $this->userRepo->getRestrictableRoles();
|
||||||
return view('chapters/restrictions', [
|
return view('chapters/restrictions', [
|
||||||
@ -241,8 +239,7 @@ class ChapterController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function restrict($bookSlug, $chapterSlug, Request $request)
|
public function restrict($bookSlug, $chapterSlug, Request $request)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
|
||||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('restrictions-manage', $chapter);
|
$this->checkOwnablePermission('restrictions-manage', $chapter);
|
||||||
$this->chapterRepo->updateEntityPermissionsFromRequest($request, $chapter);
|
$this->chapterRepo->updateEntityPermissionsFromRequest($request, $chapter);
|
||||||
session()->flash('success', trans('entities.chapters_permissions_success'));
|
session()->flash('success', trans('entities.chapters_permissions_success'));
|
||||||
|
@ -5,6 +5,7 @@ namespace BookStack\Http\Controllers;
|
|||||||
use Activity;
|
use Activity;
|
||||||
use BookStack\Repos\EntityRepo;
|
use BookStack\Repos\EntityRepo;
|
||||||
use BookStack\Http\Requests;
|
use BookStack\Http\Requests;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
use Views;
|
use Views;
|
||||||
|
|
||||||
class HomeController extends Controller
|
class HomeController extends Controller
|
||||||
@ -31,9 +32,9 @@ class HomeController extends Controller
|
|||||||
$activity = Activity::latest(10);
|
$activity = Activity::latest(10);
|
||||||
$draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
|
$draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
|
||||||
$recentFactor = count($draftPages) > 0 ? 0.5 : 1;
|
$recentFactor = count($draftPages) > 0 ? 0.5 : 1;
|
||||||
$recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreatedBooks(10*$recentFactor);
|
$recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 10*$recentFactor);
|
||||||
$recentlyCreatedPages = $this->entityRepo->getRecentlyCreatedPages(5);
|
$recentlyCreatedPages = $this->entityRepo->getRecentlyCreated('page', 5);
|
||||||
$recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdatedPages(5);
|
$recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 5);
|
||||||
return view('home', [
|
return view('home', [
|
||||||
'activity' => $activity,
|
'activity' => $activity,
|
||||||
'recents' => $recents,
|
'recents' => $recents,
|
||||||
|
@ -2,22 +2,22 @@
|
|||||||
|
|
||||||
use Activity;
|
use Activity;
|
||||||
use BookStack\Exceptions\NotFoundException;
|
use BookStack\Exceptions\NotFoundException;
|
||||||
|
use BookStack\Repos\EntityRepo;
|
||||||
use BookStack\Repos\UserRepo;
|
use BookStack\Repos\UserRepo;
|
||||||
use BookStack\Services\ExportService;
|
use BookStack\Services\ExportService;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use BookStack\Http\Requests;
|
|
||||||
use BookStack\Repos\BookRepo;
|
use BookStack\Repos\BookRepo;
|
||||||
use BookStack\Repos\ChapterRepo;
|
use BookStack\Repos\ChapterRepo;
|
||||||
use BookStack\Repos\PageRepo;
|
use BookStack\Repos\PageRepo;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
||||||
use Views;
|
use Views;
|
||||||
use GatherContent\Htmldiff\Htmldiff;
|
use GatherContent\Htmldiff\Htmldiff;
|
||||||
|
|
||||||
class PageController extends Controller
|
class PageController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
|
protected $entityRepo;
|
||||||
protected $pageRepo;
|
protected $pageRepo;
|
||||||
protected $bookRepo;
|
protected $bookRepo;
|
||||||
protected $chapterRepo;
|
protected $chapterRepo;
|
||||||
@ -32,8 +32,10 @@ class PageController extends Controller
|
|||||||
* @param ExportService $exportService
|
* @param ExportService $exportService
|
||||||
* @param UserRepo $userRepo
|
* @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->pageRepo = $pageRepo;
|
||||||
$this->bookRepo = $bookRepo;
|
$this->bookRepo = $bookRepo;
|
||||||
$this->chapterRepo = $chapterRepo;
|
$this->chapterRepo = $chapterRepo;
|
||||||
@ -51,8 +53,8 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function create($bookSlug, $chapterSlug = null)
|
public function create($bookSlug, $chapterSlug = null)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
|
$chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
|
||||||
$parent = $chapter ? $chapter : $book;
|
$parent = $chapter ? $chapter : $book;
|
||||||
$this->checkOwnablePermission('page-create', $parent);
|
$this->checkOwnablePermission('page-create', $parent);
|
||||||
|
|
||||||
@ -81,8 +83,8 @@ class PageController extends Controller
|
|||||||
'name' => 'required|string|max:255'
|
'name' => 'required|string|max:255'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$book = $this->entityRepo->getBySlug('book', $bookSlug);
|
||||||
$chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
|
$chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
|
||||||
$parent = $chapter ? $chapter : $book;
|
$parent = $chapter ? $chapter : $book;
|
||||||
$this->checkOwnablePermission('page-create', $parent);
|
$this->checkOwnablePermission('page-create', $parent);
|
||||||
|
|
||||||
@ -102,15 +104,14 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function editDraft($bookSlug, $pageId)
|
public function editDraft($bookSlug, $pageId)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$draft = $this->entityRepo->getById('page', $pageId, true);
|
||||||
$draft = $this->pageRepo->getById($pageId, true);
|
$this->checkOwnablePermission('page-create', $draft->book);
|
||||||
$this->checkOwnablePermission('page-create', $book);
|
|
||||||
$this->setPageTitle(trans('entities.pages_edit_draft'));
|
$this->setPageTitle(trans('entities.pages_edit_draft'));
|
||||||
|
|
||||||
$draftsEnabled = $this->signedIn;
|
$draftsEnabled = $this->signedIn;
|
||||||
return view('pages/edit', [
|
return view('pages/edit', [
|
||||||
'page' => $draft,
|
'page' => $draft,
|
||||||
'book' => $book,
|
'book' => $draft->book,
|
||||||
'isDraft' => true,
|
'isDraft' => true,
|
||||||
'draftsEnabled' => $draftsEnabled
|
'draftsEnabled' => $draftsEnabled
|
||||||
]);
|
]);
|
||||||
@ -130,12 +131,12 @@ class PageController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$input = $request->all();
|
$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);
|
$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);
|
$this->checkOwnablePermission('page-create', $parent);
|
||||||
|
|
||||||
if ($parent->isA('chapter')) {
|
if ($parent->isA('chapter')) {
|
||||||
@ -152,18 +153,15 @@ class PageController extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the specified page.
|
* Display the specified page.
|
||||||
* If the page is not found via the slug the
|
* If the page is not found via the slug the revisions are searched for a match.
|
||||||
* revisions are searched for a match.
|
|
||||||
* @param string $bookSlug
|
* @param string $bookSlug
|
||||||
* @param string $pageSlug
|
* @param string $pageSlug
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function show($bookSlug, $pageSlug)
|
public function show($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
} catch (NotFoundException $e) {
|
} catch (NotFoundException $e) {
|
||||||
$page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug);
|
$page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug);
|
||||||
if ($page === null) abort(404);
|
if ($page === null) abort(404);
|
||||||
@ -172,12 +170,12 @@ class PageController extends Controller
|
|||||||
|
|
||||||
$this->checkOwnablePermission('page-view', $page);
|
$this->checkOwnablePermission('page-view', $page);
|
||||||
|
|
||||||
$sidebarTree = $this->bookRepo->getChildren($book);
|
$sidebarTree = $this->bookRepo->getChildren($page->book);
|
||||||
$pageNav = $this->pageRepo->getPageNav($page);
|
$pageNav = $this->pageRepo->getPageNav($page);
|
||||||
|
|
||||||
Views::add($page);
|
Views::add($page);
|
||||||
$this->setPageTitle($page->getShortName());
|
$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]);
|
'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,7 +186,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function getPageAjax($pageId)
|
public function getPageAjax($pageId)
|
||||||
{
|
{
|
||||||
$page = $this->pageRepo->getById($pageId);
|
$page = $this->entityRepo->getById('page', $pageId);
|
||||||
return response()->json($page);
|
return response()->json($page);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,8 +198,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function edit($bookSlug, $pageSlug)
|
public function edit($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
$this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
|
$this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
|
||||||
$page->isDraft = false;
|
$page->isDraft = false;
|
||||||
@ -227,7 +224,7 @@ class PageController extends Controller
|
|||||||
$draftsEnabled = $this->signedIn;
|
$draftsEnabled = $this->signedIn;
|
||||||
return view('pages/edit', [
|
return view('pages/edit', [
|
||||||
'page' => $page,
|
'page' => $page,
|
||||||
'book' => $book,
|
'book' => $page->book,
|
||||||
'current' => $page,
|
'current' => $page,
|
||||||
'draftsEnabled' => $draftsEnabled
|
'draftsEnabled' => $draftsEnabled
|
||||||
]);
|
]);
|
||||||
@ -245,11 +242,10 @@ class PageController extends Controller
|
|||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
'name' => 'required|string|max:255'
|
'name' => 'required|string|max:255'
|
||||||
]);
|
]);
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
$this->pageRepo->updatePage($page, $book->id, $request->all());
|
$this->pageRepo->updatePage($page, $page->book->id, $request->all());
|
||||||
Activity::add($page, 'page_update', $book->id);
|
Activity::add($page, 'page_update', $page->book->id);
|
||||||
return redirect($page->getUrl());
|
return redirect($page->getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +257,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function saveDraft(Request $request, $pageId)
|
public function saveDraft(Request $request, $pageId)
|
||||||
{
|
{
|
||||||
$page = $this->pageRepo->getById($pageId, true);
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
|
|
||||||
if (!$this->signedIn) {
|
if (!$this->signedIn) {
|
||||||
@ -294,7 +290,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function redirectFromLink($pageId)
|
public function redirectFromLink($pageId)
|
||||||
{
|
{
|
||||||
$page = $this->pageRepo->getById($pageId);
|
$page = $this->entityRepo->getById('page', $pageId);
|
||||||
return redirect($page->getUrl());
|
return redirect($page->getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -306,11 +302,10 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showDelete($bookSlug, $pageSlug)
|
public function showDelete($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('page-delete', $page);
|
$this->checkOwnablePermission('page-delete', $page);
|
||||||
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
|
$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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -323,11 +318,10 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showDeleteDraft($bookSlug, $pageId)
|
public function showDeleteDraft($bookSlug, $pageId)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||||
$page = $this->pageRepo->getById($pageId, true);
|
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
$this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
|
$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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -339,8 +333,8 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function destroy($bookSlug, $pageSlug)
|
public function destroy($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
$book = $page->book;
|
||||||
$this->checkOwnablePermission('page-delete', $page);
|
$this->checkOwnablePermission('page-delete', $page);
|
||||||
Activity::addMessage('page_delete', $book->id, $page->name);
|
Activity::addMessage('page_delete', $book->id, $page->name);
|
||||||
session()->flash('success', trans('entities.pages_delete_success'));
|
session()->flash('success', trans('entities.pages_delete_success'));
|
||||||
@ -357,8 +351,8 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function destroyDraft($bookSlug, $pageId)
|
public function destroyDraft($bookSlug, $pageId)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||||
$page = $this->pageRepo->getById($pageId, true);
|
$book = $page->book;
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
session()->flash('success', trans('entities.pages_delete_draft_success'));
|
session()->flash('success', trans('entities.pages_delete_draft_success'));
|
||||||
$this->pageRepo->destroy($page);
|
$this->pageRepo->destroy($page);
|
||||||
@ -373,10 +367,9 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showRevisions($bookSlug, $pageSlug)
|
public function showRevisions($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
|
$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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -388,8 +381,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showRevision($bookSlug, $pageSlug, $revisionId)
|
public function showRevision($bookSlug, $pageSlug, $revisionId)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$revision = $this->pageRepo->getRevisionById($revisionId);
|
$revision = $this->pageRepo->getRevisionById($revisionId);
|
||||||
|
|
||||||
$page->fill($revision->toArray());
|
$page->fill($revision->toArray());
|
||||||
@ -397,7 +389,7 @@ class PageController extends Controller
|
|||||||
|
|
||||||
return view('pages/revision', [
|
return view('pages/revision', [
|
||||||
'page' => $page,
|
'page' => $page,
|
||||||
'book' => $book,
|
'book' => $page->book,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -410,8 +402,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
|
public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$revision = $this->pageRepo->getRevisionById($revisionId);
|
$revision = $this->pageRepo->getRevisionById($revisionId);
|
||||||
|
|
||||||
$prev = $revision->getPrevious();
|
$prev = $revision->getPrevious();
|
||||||
@ -423,7 +414,7 @@ class PageController extends Controller
|
|||||||
|
|
||||||
return view('pages/revision', [
|
return view('pages/revision', [
|
||||||
'page' => $page,
|
'page' => $page,
|
||||||
'book' => $book,
|
'book' => $page->book,
|
||||||
'diff' => $diff,
|
'diff' => $diff,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@ -437,11 +428,10 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function restoreRevision($bookSlug, $pageSlug, $revisionId)
|
public function restoreRevision($bookSlug, $pageSlug, $revisionId)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
$page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
|
$page = $this->pageRepo->restoreRevision($page, $page->book, $revisionId);
|
||||||
Activity::add($page, 'page_restore', $book->id);
|
Activity::add($page, 'page_restore', $page->book->id);
|
||||||
return redirect($page->getUrl());
|
return redirect($page->getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -454,8 +444,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function exportPdf($bookSlug, $pageSlug)
|
public function exportPdf($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$pdfContent = $this->exportService->pageToPdf($page);
|
$pdfContent = $this->exportService->pageToPdf($page);
|
||||||
return response()->make($pdfContent, 200, [
|
return response()->make($pdfContent, 200, [
|
||||||
'Content-Type' => 'application/octet-stream',
|
'Content-Type' => 'application/octet-stream',
|
||||||
@ -471,8 +460,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function exportHtml($bookSlug, $pageSlug)
|
public function exportHtml($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$containedHtml = $this->exportService->pageToContainedHtml($page);
|
$containedHtml = $this->exportService->pageToContainedHtml($page);
|
||||||
return response()->make($containedHtml, 200, [
|
return response()->make($containedHtml, 200, [
|
||||||
'Content-Type' => 'application/octet-stream',
|
'Content-Type' => 'application/octet-stream',
|
||||||
@ -488,8 +476,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function exportPlainText($bookSlug, $pageSlug)
|
public function exportPlainText($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$containedHtml = $this->exportService->pageToPlainText($page);
|
$containedHtml = $this->exportService->pageToPlainText($page);
|
||||||
return response()->make($containedHtml, 200, [
|
return response()->make($containedHtml, 200, [
|
||||||
'Content-Type' => 'application/octet-stream',
|
'Content-Type' => 'application/octet-stream',
|
||||||
@ -531,8 +518,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showRestrict($bookSlug, $pageSlug)
|
public function showRestrict($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('restrictions-manage', $page);
|
$this->checkOwnablePermission('restrictions-manage', $page);
|
||||||
$roles = $this->userRepo->getRestrictableRoles();
|
$roles = $this->userRepo->getRestrictableRoles();
|
||||||
return view('pages/restrictions', [
|
return view('pages/restrictions', [
|
||||||
@ -550,11 +536,10 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function showMove($bookSlug, $pageSlug)
|
public function showMove($bookSlug, $pageSlug)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
return view('pages/move', [
|
return view('pages/move', [
|
||||||
'book' => $book,
|
'book' => $page->book,
|
||||||
'page' => $page
|
'page' => $page
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@ -569,8 +554,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function move($bookSlug, $pageSlug, Request $request)
|
public function move($bookSlug, $pageSlug, Request $request)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('page-update', $page);
|
$this->checkOwnablePermission('page-update', $page);
|
||||||
|
|
||||||
$entitySelection = $request->get('entity_selection', null);
|
$entitySelection = $request->get('entity_selection', null);
|
||||||
@ -582,15 +566,10 @@ class PageController extends Controller
|
|||||||
$entityType = $stringExploded[0];
|
$entityType = $stringExploded[0];
|
||||||
$entityId = intval($stringExploded[1]);
|
$entityId = intval($stringExploded[1]);
|
||||||
|
|
||||||
$parent = false;
|
|
||||||
|
|
||||||
if ($entityType == 'chapter') {
|
try {
|
||||||
$parent = $this->chapterRepo->getById($entityId);
|
$parent = $this->entityRepo->getById($entityType, $entityId);
|
||||||
} else if ($entityType == 'book') {
|
} catch (\Exception $e) {
|
||||||
$parent = $this->bookRepo->getById($entityId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($parent === false || $parent === null) {
|
|
||||||
session()->flash(trans('entities.selected_book_chapter_not_found'));
|
session()->flash(trans('entities.selected_book_chapter_not_found'));
|
||||||
return redirect()->back();
|
return redirect()->back();
|
||||||
}
|
}
|
||||||
@ -611,8 +590,7 @@ class PageController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function restrict($bookSlug, $pageSlug, Request $request)
|
public function restrict($bookSlug, $pageSlug, Request $request)
|
||||||
{
|
{
|
||||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
|
||||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
|
||||||
$this->checkOwnablePermission('restrictions-manage', $page);
|
$this->checkOwnablePermission('restrictions-manage', $page);
|
||||||
$this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
|
$this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
|
||||||
session()->flash('success', trans('entities.pages_permissions_success'));
|
session()->flash('success', trans('entities.pages_permissions_success'));
|
||||||
|
@ -7,6 +7,8 @@ class Page extends Entity
|
|||||||
|
|
||||||
protected $simpleAttributes = ['name', 'id', 'slug'];
|
protected $simpleAttributes = ['name', 'id', 'slug'];
|
||||||
|
|
||||||
|
protected $with = ['book'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts this page into a simplified array.
|
* Converts this page into a simplified array.
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
<?php namespace BookStack\Repos;
|
<?php namespace BookStack\Repos;
|
||||||
|
|
||||||
use Alpha\B;
|
|
||||||
use BookStack\Exceptions\NotFoundException;
|
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use BookStack\Book;
|
use BookStack\Book;
|
||||||
use Views;
|
|
||||||
|
|
||||||
class BookRepo extends EntityRepo
|
class BookRepo extends EntityRepo
|
||||||
{
|
{
|
||||||
@ -24,105 +19,6 @@ class BookRepo extends EntityRepo
|
|||||||
parent::__construct();
|
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.
|
* Get a new book instance from request input.
|
||||||
* @param array $input
|
* @param array $input
|
||||||
|
@ -21,58 +21,6 @@ class ChapterRepo extends EntityRepo
|
|||||||
parent::__construct();
|
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
|
* Get the child items for a chapter
|
||||||
* @param Chapter $chapter
|
* @param Chapter $chapter
|
||||||
|
@ -3,11 +3,12 @@
|
|||||||
use BookStack\Book;
|
use BookStack\Book;
|
||||||
use BookStack\Chapter;
|
use BookStack\Chapter;
|
||||||
use BookStack\Entity;
|
use BookStack\Entity;
|
||||||
|
use BookStack\Exceptions\NotFoundException;
|
||||||
use BookStack\Page;
|
use BookStack\Page;
|
||||||
use BookStack\Services\PermissionService;
|
use BookStack\Services\PermissionService;
|
||||||
use BookStack\User;
|
use BookStack\Services\ViewService;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
|
|
||||||
class EntityRepo
|
class EntityRepo
|
||||||
{
|
{
|
||||||
@ -27,11 +28,22 @@ class EntityRepo
|
|||||||
*/
|
*/
|
||||||
public $page;
|
public $page;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base entity instances keyed by type
|
||||||
|
* @var []Entity
|
||||||
|
*/
|
||||||
|
protected $entities;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var PermissionService
|
* @var PermissionService
|
||||||
*/
|
*/
|
||||||
protected $permissionService;
|
protected $permissionService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ViewService
|
||||||
|
*/
|
||||||
|
protected $viewService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Acceptable operators to be used in a query
|
* Acceptable operators to be used in a query
|
||||||
* @var array
|
* @var array
|
||||||
@ -43,23 +55,126 @@ class EntityRepo
|
|||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
// TODO - Redo this to come via injection
|
||||||
$this->book = app(Book::class);
|
$this->book = app(Book::class);
|
||||||
$this->chapter = app(Chapter::class);
|
$this->chapter = app(Chapter::class);
|
||||||
$this->page = app(Page::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);
|
$this->permissionService = app(PermissionService::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the latest books added to the system.
|
* Get an entity instance via type.
|
||||||
|
* @param $type
|
||||||
|
* @return Entity
|
||||||
|
*/
|
||||||
|
protected function getEntity($type)
|
||||||
|
{
|
||||||
|
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 $q;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an entity with the given id exists.
|
||||||
|
* @param $type
|
||||||
|
* @param $id
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function exists($type, $id)
|
||||||
|
{
|
||||||
|
return $this->entityQuery($type)->where('id', '=', $id)->exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 $count
|
||||||
* @param int $page
|
* @param int $page
|
||||||
* @param bool $additionalQuery
|
* @param bool|callable $additionalQuery
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false)
|
public function getRecentlyCreated($type, $count = 20, $page = 0, $additionalQuery = false)
|
||||||
{
|
{
|
||||||
$query = $this->permissionService->enforceBookRestrictions($this->book)
|
$query = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type))
|
||||||
->orderBy('created_at', 'desc');
|
->orderBy('created_at', 'desc');
|
||||||
|
if (strtolower($type) === 'page') $query = $query->where('draft', '=', false);
|
||||||
if ($additionalQuery !== false && is_callable($additionalQuery)) {
|
if ($additionalQuery !== false && is_callable($additionalQuery)) {
|
||||||
$additionalQuery($query);
|
$additionalQuery($query);
|
||||||
}
|
}
|
||||||
@ -67,45 +182,17 @@ class EntityRepo
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the most recently updated books.
|
* Get the most recently updated entities of the given type.
|
||||||
* @param $count
|
* @param string $type
|
||||||
* @param int $page
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getRecentlyUpdatedBooks($count = 20, $page = 0)
|
|
||||||
{
|
|
||||||
return $this->permissionService->enforceBookRestrictions($this->book)
|
|
||||||
->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the latest pages added to the system.
|
|
||||||
* @param int $count
|
* @param int $count
|
||||||
* @param int $page
|
* @param int $page
|
||||||
* @param bool $additionalQuery
|
* @param bool|callable $additionalQuery
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false)
|
public function getRecentlyUpdated($type, $count = 20, $page = 0, $additionalQuery = false)
|
||||||
{
|
{
|
||||||
$query = $this->permissionService->enforcePageRestrictions($this->page)
|
$query = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type))
|
||||||
->orderBy('created_at', 'desc')->where('draft', '=', false);
|
->orderBy('updated_at', 'desc');
|
||||||
if ($additionalQuery !== false && is_callable($additionalQuery)) {
|
if (strtolower($type) === 'page') $query = $query->where('draft', '=', false);
|
||||||
$additionalQuery($query);
|
|
||||||
}
|
|
||||||
return $query->with('book')->skip($page * $count)->take($count)->get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the latest chapters added to the system.
|
|
||||||
* @param int $count
|
|
||||||
* @param int $page
|
|
||||||
* @param bool $additionalQuery
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false)
|
|
||||||
{
|
|
||||||
$query = $this->permissionService->enforceChapterRestrictions($this->chapter)
|
|
||||||
->orderBy('created_at', 'desc');
|
|
||||||
if ($additionalQuery !== false && is_callable($additionalQuery)) {
|
if ($additionalQuery !== false && is_callable($additionalQuery)) {
|
||||||
$additionalQuery($query);
|
$additionalQuery($query);
|
||||||
}
|
}
|
||||||
@ -113,16 +200,29 @@ class EntityRepo
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the most recently updated pages.
|
* Get the most recently viewed entities.
|
||||||
* @param $count
|
* @param string|bool $type
|
||||||
|
* @param int $count
|
||||||
* @param int $page
|
* @param int $page
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getRecentlyUpdatedPages($count = 20, $page = 0)
|
public function getRecentlyViewed($type, $count = 10, $page = 0)
|
||||||
{
|
{
|
||||||
return $this->permissionService->enforcePageRestrictions($this->page)
|
$filter = is_bool($type) ? false : $this->getEntity($type);
|
||||||
->where('draft', '=', false)
|
return $this->viewService->getUserRecentlyViewed($count, $page, $filter);
|
||||||
->orderBy('updated_at', 'desc')->with('book')->skip($page * $count)->take($count)->get();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the most popular entities base on all views.
|
||||||
|
* @param string|bool $type
|
||||||
|
* @param int $count
|
||||||
|
* @param int $page
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getPopular($type, $count = 10, $page = 0)
|
||||||
|
{
|
||||||
|
$filter = is_bool($type) ? false : $this->getEntity($type);
|
||||||
|
return $this->viewService->getPopular($count, $page, $filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,31 +45,6 @@ class PageRepo extends EntityRepo
|
|||||||
return $query;
|
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
|
* Search through page revisions and retrieve
|
||||||
* the last page in the current book that
|
* the last page in the current book that
|
||||||
|
@ -168,13 +168,13 @@ class UserRepo
|
|||||||
public function getRecentlyCreated(User $user, $count = 20)
|
public function getRecentlyCreated(User $user, $count = 20)
|
||||||
{
|
{
|
||||||
return [
|
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);
|
$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);
|
$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);
|
$query->where('created_by', '=', $user->id);
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
|
@ -8,8 +8,8 @@ use BookStack\Ownable;
|
|||||||
use BookStack\Page;
|
use BookStack\Page;
|
||||||
use BookStack\Role;
|
use BookStack\Role;
|
||||||
use BookStack\User;
|
use BookStack\User;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
|
|
||||||
class PermissionService
|
class PermissionService
|
||||||
{
|
{
|
||||||
@ -469,17 +469,8 @@ class PermissionService
|
|||||||
*/
|
*/
|
||||||
public function enforcePageRestrictions($query, $action = 'view')
|
public function enforcePageRestrictions($query, $action = 'view')
|
||||||
{
|
{
|
||||||
// Prevent drafts being visible to others.
|
// TODO - remove this
|
||||||
$query = $query->where(function ($query) {
|
return $this->enforceEntityRestrictions('page', $query, $action);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -490,7 +481,8 @@ class PermissionService
|
|||||||
*/
|
*/
|
||||||
public function enforceChapterRestrictions($query, $action = 'view')
|
public function enforceChapterRestrictions($query, $action = 'view')
|
||||||
{
|
{
|
||||||
return $this->enforceEntityRestrictions($query, $action);
|
// TODO - remove this
|
||||||
|
return $this->enforceEntityRestrictions('chapter', $query, $action);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -501,21 +493,36 @@ class PermissionService
|
|||||||
*/
|
*/
|
||||||
public function enforceBookRestrictions($query, $action = 'view')
|
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
|
* Add restrictions for a generic entity
|
||||||
* @param $query
|
* @param string $entityType
|
||||||
|
* @param Builder|Entity $query
|
||||||
* @param string $action
|
* @param string $action
|
||||||
* @return mixed
|
* @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()) {
|
if ($this->isAdmin()) {
|
||||||
$this->clean();
|
$this->clean();
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->currentAction = $action;
|
$this->currentAction = $action;
|
||||||
return $this->entityRestrictionQuery($query);
|
return $this->entityRestrictionQuery($query);
|
||||||
}
|
}
|
||||||
|
@ -65,9 +65,9 @@ class RestrictionsTest extends TestCase
|
|||||||
$this->forceVisit($bookUrl)
|
$this->forceVisit($bookUrl)
|
||||||
->see('Book not found');
|
->see('Book not found');
|
||||||
$this->forceVisit($bookPage->getUrl())
|
$this->forceVisit($bookPage->getUrl())
|
||||||
->see('Book not found');
|
->see('Page not found');
|
||||||
$this->forceVisit($bookChapter->getUrl())
|
$this->forceVisit($bookChapter->getUrl())
|
||||||
->see('Book not found');
|
->see('Chapter not found');
|
||||||
|
|
||||||
$this->setEntityRestrictions($book, ['view']);
|
$this->setEntityRestrictions($book, ['view']);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user