mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-07 23:03:00 +03:00
Refactored some core entity actions
- Created BookChild class to share some page/chapter logic. - Gave entities the power to generate their own permissions and slugs. - Moved bits out of BaseController constructor since it was overly sticky. - Moved slug generation logic into its own class. - Created a facade for permissions due to high use. - Fixed failing test issues from last commits
This commit is contained in:
@@ -54,12 +54,12 @@ class BookController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books'));
|
||||
$sort = setting()->getUser($this->currentUser, 'books_sort', 'name');
|
||||
$order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc');
|
||||
$view = setting()->getForCurrentUser('books_view_type', config('app.views.books'));
|
||||
$sort = setting()->getForCurrentUser('books_sort', 'name');
|
||||
$order = setting()->getForCurrentUser('books_sort_order', 'asc');
|
||||
|
||||
$books = $this->bookRepo->getAllPaginated('book', 18, $sort, $order);
|
||||
$recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed('book', 4, 0) : false;
|
||||
$recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed('book', 4, 0) : false;
|
||||
$popular = $this->bookRepo->getPopular('book', 4, 0);
|
||||
$new = $this->bookRepo->getRecentlyCreated('book', 4, 0);
|
||||
|
||||
@@ -107,7 +107,6 @@ class BookController extends Controller
|
||||
* @throws NotFoundException
|
||||
* @throws ImageUploadException
|
||||
* @throws ValidationException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function store(Request $request, string $shelfSlug = null)
|
||||
{
|
||||
@@ -246,7 +245,7 @@ class BookController extends Controller
|
||||
* @return Factory|View
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function getSortItem(string $bookSlug)
|
||||
public function sortItem(string $bookSlug)
|
||||
{
|
||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
||||
$bookChildren = $this->bookRepo->getBookChildren($book);
|
||||
@@ -286,10 +285,12 @@ class BookController extends Controller
|
||||
// Get the books involved in the sort
|
||||
$bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
|
||||
$booksInvolved = $this->bookRepo->getManyById('book', $bookIdsInvolved, false, true);
|
||||
|
||||
// Throw permission error if invalid ids or inaccessible books given.
|
||||
if (count($bookIdsInvolved) !== count($booksInvolved)) {
|
||||
$this->showPermissionError();
|
||||
}
|
||||
|
||||
// Check permissions of involved books
|
||||
$booksInvolved->each(function (Book $book) {
|
||||
$this->checkOwnablePermission('book-update', $book);
|
||||
@@ -304,7 +305,7 @@ class BookController extends Controller
|
||||
$chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
|
||||
|
||||
if ($bookChanged) {
|
||||
$this->bookRepo->changeBook($mapItem->type, $mapItem->book, $model);
|
||||
$this->bookRepo->changeBook($model, $mapItem->book);
|
||||
}
|
||||
if ($chapterChanged) {
|
||||
$model->chapter_id = intval($mapItem->parentChapter);
|
||||
@@ -318,7 +319,7 @@ class BookController extends Controller
|
||||
|
||||
// Rebuild permissions and add activity for involved books.
|
||||
$booksInvolved->each(function (Book $book) {
|
||||
$this->bookRepo->buildJointPermissionsForBook($book);
|
||||
$book->rebuildPermissions();
|
||||
Activity::add($book, 'book_sort', $book->id);
|
||||
});
|
||||
|
||||
|
@@ -40,9 +40,9 @@ class BookshelfController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
|
||||
$sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
|
||||
$order = setting()->getUser($this->currentUser, 'bookshelves_sort_order', 'asc');
|
||||
$view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid'));
|
||||
$sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
|
||||
$order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
|
||||
$sortOptions = [
|
||||
'name' => trans('common.sort_name'),
|
||||
'created_at' => trans('common.sort_created_at'),
|
||||
@@ -54,7 +54,7 @@ class BookshelfController extends Controller
|
||||
$shelf->books = $this->entityRepo->getBookshelfChildren($shelf);
|
||||
}
|
||||
|
||||
$recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
|
||||
$recents = $this->isSignedIn() ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
|
||||
$popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
|
||||
$new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
|
||||
|
||||
|
@@ -201,7 +201,9 @@ class ChapterController extends Controller
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
$this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
|
||||
$this->entityRepo->changeBook($chapter, $parent->id);
|
||||
$chapter->rebuildPermissions();
|
||||
|
||||
Activity::add($chapter, 'chapter_move', $chapter->book->id);
|
||||
$this->showSuccessNotification( trans('entities.chapter_move_success', ['bookName' => $parent->name]));
|
||||
|
||||
|
@@ -2,9 +2,6 @@
|
||||
|
||||
namespace BookStack\Http\Controllers;
|
||||
|
||||
use BookStack\Auth\User;
|
||||
use BookStack\Entities\Entity;
|
||||
use BookStack\Facades\Activity;
|
||||
use BookStack\Ownable;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
@@ -16,23 +13,20 @@ abstract class Controller extends BaseController
|
||||
{
|
||||
use DispatchesJobs, ValidatesRequests;
|
||||
|
||||
/**
|
||||
* @var User static
|
||||
*/
|
||||
protected $currentUser;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $signedIn;
|
||||
|
||||
/**
|
||||
* Controller constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->currentUser = user();
|
||||
$this->signedIn = auth()->check();
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user is signed in.
|
||||
*/
|
||||
protected function isSignedIn(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,7 +117,7 @@ abstract class Controller extends BaseController
|
||||
protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
|
||||
{
|
||||
return $this->checkPermissionOr($permissionName, function () use ($userId) {
|
||||
return $userId === $this->currentUser->id;
|
||||
return $userId === user()->id;
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -26,9 +26,9 @@ class HomeController extends Controller
|
||||
public function index()
|
||||
{
|
||||
$activity = Activity::latest(10);
|
||||
$draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
|
||||
$draftPages = $this->isSignedIn() ? $this->entityRepo->getUserDraftPages(6) : [];
|
||||
$recentFactor = count($draftPages) > 0 ? 0.5 : 1;
|
||||
$recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 12*$recentFactor);
|
||||
$recents = $this->isSignedIn() ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 12*$recentFactor);
|
||||
$recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 12);
|
||||
|
||||
$homepageOptions = ['default', 'books', 'bookshelves', 'page'];
|
||||
@@ -47,9 +47,9 @@ class HomeController extends Controller
|
||||
// Add required list ordering & sorting for books & shelves views.
|
||||
if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
|
||||
$key = $homepageOption;
|
||||
$view = setting()->getUser($this->currentUser, $key . '_view_type', config('app.views.' . $key));
|
||||
$sort = setting()->getUser($this->currentUser, $key . '_sort', 'name');
|
||||
$order = setting()->getUser($this->currentUser, $key . '_sort_order', 'asc');
|
||||
$view = setting()->getForCurrentUser($key . '_view_type', config('app.views.' . $key));
|
||||
$sort = setting()->getForCurrentUser($key . '_sort', 'name');
|
||||
$order = setting()->getForCurrentUser($key . '_sort_order', 'asc');
|
||||
|
||||
$sortOptions = [
|
||||
'name' => trans('common.sort_name'),
|
||||
|
@@ -56,7 +56,7 @@ class PageController extends Controller
|
||||
$this->checkOwnablePermission('page-create', $parent);
|
||||
|
||||
// Redirect to draft edit screen if signed in
|
||||
if ($this->signedIn) {
|
||||
if ($this->isSignedIn()) {
|
||||
$draft = $this->pageRepo->getDraftPage($book, $chapter);
|
||||
return redirect($draft->getUrl());
|
||||
}
|
||||
@@ -111,7 +111,7 @@ class PageController extends Controller
|
||||
$this->checkOwnablePermission('page-create', $draft->parent);
|
||||
$this->setPageTitle(trans('entities.pages_edit_draft'));
|
||||
|
||||
$draftsEnabled = $this->signedIn;
|
||||
$draftsEnabled = $this->isSignedIn();
|
||||
$templates = $this->pageRepo->getPageTemplates(10);
|
||||
|
||||
return view('pages.edit', [
|
||||
@@ -230,7 +230,7 @@ class PageController extends Controller
|
||||
}
|
||||
|
||||
// Check for a current draft version for this user
|
||||
$userPageDraft = $this->pageRepo->getUserPageDraft($page, $this->currentUser->id);
|
||||
$userPageDraft = $this->pageRepo->getUserPageDraft($page, user()->id);
|
||||
if ($userPageDraft !== null) {
|
||||
$page->name = $userPageDraft->name;
|
||||
$page->html = $userPageDraft->html;
|
||||
@@ -243,7 +243,7 @@ class PageController extends Controller
|
||||
$this->showWarningNotification( implode("\n", $warnings));
|
||||
}
|
||||
|
||||
$draftsEnabled = $this->signedIn;
|
||||
$draftsEnabled = $this->isSignedIn();
|
||||
$templates = $this->pageRepo->getPageTemplates(10);
|
||||
|
||||
return view('pages.edit', [
|
||||
@@ -285,7 +285,7 @@ class PageController extends Controller
|
||||
$page = $this->pageRepo->getById('page', $pageId, true);
|
||||
$this->checkOwnablePermission('page-update', $page);
|
||||
|
||||
if (!$this->signedIn) {
|
||||
if (!$this->isSignedIn()) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => trans('errors.guests_cannot_save_drafts'),
|
||||
|
@@ -177,7 +177,7 @@ class UserController extends Controller
|
||||
}
|
||||
|
||||
// External auth id updates
|
||||
if ($this->currentUser->can('users-manage') && $request->filled('external_auth_id')) {
|
||||
if (user()->can('users-manage') && $request->filled('external_auth_id')) {
|
||||
$user->external_auth_id = $request->get('external_auth_id');
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user