0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-04-21 03:30:38 +00:00

Refactored notification showing and global view data

This commit is contained in:
Dan Brown 2019-09-19 15:12:10 +01:00
parent 60d0f96cd7
commit 2a2cc858f0
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
15 changed files with 102 additions and 58 deletions

View file

@ -65,14 +65,14 @@ class ConfirmEmailController extends Controller
$userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
} catch (Exception $exception) {
if ($exception instanceof UserTokenNotFoundException) {
session()->flash('error', trans('errors.email_confirmation_invalid'));
$this->showErrorNotification( trans('errors.email_confirmation_invalid'));
return redirect('/register');
}
if ($exception instanceof UserTokenExpiredException) {
$user = $this->userRepo->getById($exception->userId);
$this->emailConfirmationService->sendConfirmation($user);
session()->flash('error', trans('errors.email_confirmation_expired'));
$this->showErrorNotification( trans('errors.email_confirmation_expired'));
return redirect('/register/confirm');
}
@ -84,7 +84,7 @@ class ConfirmEmailController extends Controller
$user->save();
auth()->login($user);
session()->flash('success', trans('auth.email_confirm_success'));
$this->showSuccessNotification( trans('auth.email_confirm_success'));
$this->emailConfirmationService->deleteByUser($user);
return redirect('/');
@ -106,11 +106,11 @@ class ConfirmEmailController extends Controller
try {
$this->emailConfirmationService->sendConfirmation($user);
} catch (Exception $e) {
session()->flash('error', trans('auth.email_confirm_send_error'));
$this->showErrorNotification( trans('auth.email_confirm_send_error'));
return redirect('/register/confirm');
}
session()->flash('success', trans('auth.email_confirm_resent'));
$this->showSuccessNotification( trans('auth.email_confirm_resent'));
return redirect('/register/confirm');
}
}

View file

@ -53,7 +53,7 @@ class ForgotPasswordController extends Controller
if ($response === Password::RESET_LINK_SENT) {
$message = trans('auth.reset_password_sent_success', ['email' => $request->get('email')]);
session()->flash('success', $message);
$this->showSuccessNotification( $message);
return back()->with('status', trans($response));
}

View file

@ -166,14 +166,14 @@ class RegisterController extends Controller
try {
$this->emailConfirmationService->sendConfirmation($newUser);
} catch (Exception $e) {
session()->flash('error', trans('auth.email_confirm_send_error'));
$this->showErrorNotification(trans('auth.email_confirm_send_error'));
}
return redirect('/register/confirm');
}
auth()->login($newUser);
session()->flash('success', trans('auth.register_success'));
$this->showSuccessNotification(trans('auth.register_success'));
return redirect($this->redirectPath());
}

View file

@ -44,7 +44,7 @@ class ResetPasswordController extends Controller
protected function sendResetResponse(Request $request, $response)
{
$message = trans('auth.reset_password_success');
session()->flash('success', $message);
$this->showSuccessNotification( $message);
return redirect($this->redirectPath())
->with('status', trans($response));
}

View file

@ -77,7 +77,7 @@ class UserInviteController extends Controller
$user->save();
auth()->login($user);
session()->flash('success', trans('auth.user_invite_success', ['appName' => setting('app-name')]));
$this->showSuccessNotification( trans('auth.user_invite_success', ['appName' => setting('app-name')]));
$this->inviteService->deleteByUser($user);
return redirect('/');
@ -96,7 +96,7 @@ class UserInviteController extends Controller
}
if ($exception instanceof UserTokenExpiredException) {
session()->flash('error', trans('errors.invite_token_expired'));
$this->showErrorNotification( trans('errors.invite_token_expired'));
return redirect('/password/email');
}

View file

@ -380,7 +380,7 @@ class BookController extends Controller
$book = $this->bookRepo->getBySlug($bookSlug);
$this->checkOwnablePermission('restrictions-manage', $book);
$this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
session()->flash('success', trans('entities.books_permissions_updated'));
$this->showSuccessNotification(trans('entities.books_permissions_updated'));
return redirect($book->getUrl());
}

View file

@ -254,7 +254,7 @@ class BookshelfController extends Controller
$this->checkOwnablePermission('restrictions-manage', $shelf);
$this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
session()->flash('success', trans('entities.shelves_permissions_updated'));
$this->showSuccessNotification( trans('entities.shelves_permissions_updated'));
return redirect($shelf->getUrl());
}
@ -270,7 +270,7 @@ class BookshelfController extends Controller
$this->checkOwnablePermission('restrictions-manage', $shelf);
$updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
$this->showSuccessNotification( trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
return redirect($shelf->getUrl());
}

View file

@ -197,13 +197,13 @@ class ChapterController extends Controller
}
if ($parent === false || $parent === null) {
session()->flash('error', trans('errors.selected_book_not_found'));
$this->showErrorNotification( trans('errors.selected_book_not_found'));
return redirect()->back();
}
$this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
Activity::add($chapter, 'chapter_move', $chapter->book->id);
session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
$this->showSuccessNotification( trans('entities.chapter_move_success', ['bookName' => $parent->name]));
return redirect($chapter->getUrl());
}
@ -240,7 +240,7 @@ class ChapterController extends Controller
$chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $chapter);
$this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
session()->flash('success', trans('entities.chapters_permissions_success'));
$this->showSuccessNotification( trans('entities.chapters_permissions_success'));
return redirect($chapter->getUrl());
}
}

View file

@ -18,6 +18,7 @@ abstract class Controller extends BaseController
* @var User static
*/
protected $currentUser;
/**
* @var bool
*/
@ -28,28 +29,15 @@ abstract class Controller extends BaseController
*/
public function __construct()
{
$this->middleware(function ($request, $next) {
// Get a user instance for the current user
$user = user();
// Share variables with controllers
$this->currentUser = $user;
$this->signedIn = auth()->check();
// Share variables with views
view()->share('signedIn', $this->signedIn);
view()->share('currentUser', $user);
return $next($request);
});
$this->currentUser = user();
$this->signedIn = auth()->check();
}
/**
* Stops the application and shows a permission error if
* the application is in demo mode.
*/
protected function preventAccessForDemoUsers()
protected function preventAccessInDemoMode()
{
if (config('app.env') === 'demo') {
$this->showPermissionError();
@ -75,7 +63,7 @@ abstract class Controller extends BaseController
$response = response()->json(['error' => trans('errors.permissionJson')], 403);
} else {
$response = redirect('/');
session()->flash('error', trans('errors.permission'));
$this->showErrorNotification( trans('errors.permission'));
}
throw new HttpResponseException($response);
@ -178,4 +166,31 @@ abstract class Controller extends BaseController
'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
]);
}
/**
* Show a positive, successful notification to the user on next view load.
* @param string $message
*/
protected function showSuccessNotification(string $message)
{
session()->flash('success', $message);
}
/**
* Show a warning notification to the user on next view load.
* @param string $message
*/
protected function showWarningNotification(string $message)
{
session()->flash('warning', $message);
}
/**
* Show an error notification to the user on next view load.
* @param string $message
*/
protected function showErrorNotification(string $message)
{
session()->flash('error', $message);
}
}

View file

@ -240,7 +240,7 @@ class PageController extends Controller
}
if (count($warnings) > 0) {
session()->flash('warning', implode("\n", $warnings));
$this->showWarningNotification( implode("\n", $warnings));
}
$draftsEnabled = $this->signedIn;
@ -359,7 +359,7 @@ class PageController extends Controller
$this->pageRepo->destroyPage($page);
Activity::addMessage('page_delete', $book->id, $page->name);
session()->flash('success', trans('entities.pages_delete_success'));
$this->showSuccessNotification( trans('entities.pages_delete_success'));
return redirect($book->getUrl());
}
@ -375,7 +375,7 @@ class PageController extends Controller
$page = $this->pageRepo->getById('page', $pageId, true);
$book = $page->book;
$this->checkOwnablePermission('page-update', $page);
session()->flash('success', trans('entities.pages_delete_draft_success'));
$this->showSuccessNotification( trans('entities.pages_delete_draft_success'));
$this->pageRepo->destroyPage($page);
return redirect($book->getUrl());
}
@ -491,12 +491,12 @@ class PageController extends Controller
// Check if its the latest revision, cannot delete latest revision.
if (intval($currentRevision->id) === intval($revId)) {
session()->flash('error', trans('entities.revision_cannot_delete_latest'));
$this->showErrorNotification( trans('entities.revision_cannot_delete_latest'));
return response()->view('pages.revisions', ['page' => $page, 'book' => $page->book, 'current' => $page], 400);
}
$revision->delete();
session()->flash('success', trans('entities.revision_delete_success'));
$this->showSuccessNotification( trans('entities.revision_delete_success'));
return redirect($page->getUrl('/revisions'));
}
@ -568,7 +568,7 @@ class PageController extends Controller
$this->pageRepo->changePageParent($page, $parent);
Activity::add($page, 'page_move', $page->book->id);
session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
$this->showSuccessNotification( trans('entities.pages_move_success', ['parentName' => $parent->name]));
return redirect($page->getUrl());
}
@ -616,7 +616,7 @@ class PageController extends Controller
try {
$parent = $this->pageRepo->getById($entityType, $entityId);
} catch (Exception $e) {
session()->flash(trans('entities.selected_book_chapter_not_found'));
$this->showErrorNotification(trans('entities.selected_book_chapter_not_found'));
return redirect()->back();
}
}
@ -626,7 +626,7 @@ class PageController extends Controller
$pageCopy = $this->pageRepo->copyPage($page, $parent, $request->get('name', ''));
Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
session()->flash('success', trans('entities.pages_copy_success'));
$this->showSuccessNotification( trans('entities.pages_copy_success'));
return redirect($pageCopy->getUrl());
}
@ -663,7 +663,7 @@ class PageController extends Controller
$page = $this->pageRepo->getBySlug($pageSlug, $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $page);
$this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
session()->flash('success', trans('entities.pages_permissions_success'));
$this->showSuccessNotification( trans('entities.pages_permissions_success'));
return redirect($page->getUrl());
}
}

View file

@ -53,7 +53,7 @@ class PermissionController extends Controller
]);
$this->permissionsRepo->saveNewRole($request->all());
session()->flash('success', trans('settings.role_create_success'));
$this->showSuccessNotification( trans('settings.role_create_success'));
return redirect('/settings/roles');
}
@ -90,7 +90,7 @@ class PermissionController extends Controller
]);
$this->permissionsRepo->updateRole($id, $request->all());
session()->flash('success', trans('settings.role_update_success'));
$this->showSuccessNotification( trans('settings.role_update_success'));
return redirect('/settings/roles');
}
@ -124,11 +124,11 @@ class PermissionController extends Controller
try {
$this->permissionsRepo->deleteRole($id, $request->get('migrate_role_id'));
} catch (PermissionsException $e) {
session()->flash('error', $e->getMessage());
$this->showErrorNotification( $e->getMessage());
return redirect()->back();
}
session()->flash('success', trans('settings.role_delete_success'));
$this->showSuccessNotification( trans('settings.role_delete_success'));
return redirect('/settings/roles');
}
}

View file

@ -47,7 +47,7 @@ class SettingController extends Controller
*/
public function update(Request $request)
{
$this->preventAccessForDemoUsers();
$this->preventAccessInDemoMode();
$this->checkPermission('settings-manage');
$this->validate($request, [
'app_logo' => $this->imageRepo->getImageValidationRules(),
@ -76,7 +76,7 @@ class SettingController extends Controller
setting()->remove('app-logo');
}
session()->flash('success', trans('settings.settings_save_success'));
$this->showSuccessNotification( trans('settings.settings_save_success'));
return redirect('/settings');
}
@ -111,14 +111,14 @@ class SettingController extends Controller
$imagesToDelete = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
$deleteCount = count($imagesToDelete);
if ($deleteCount === 0) {
session()->flash('warning', trans('settings.maint_image_cleanup_nothing_found'));
$this->showWarningNotification( trans('settings.maint_image_cleanup_nothing_found'));
return redirect('/settings/maintenance')->withInput();
}
if ($dryRun) {
session()->flash('cleanup-images-warning', trans('settings.maint_image_cleanup_warning', ['count' => $deleteCount]));
} else {
session()->flash('success', trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
$this->showSuccessNotification( trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
}
return redirect('/settings/maintenance#image-cleanup')->withInput();

View file

@ -144,7 +144,7 @@ class UserController extends Controller
*/
public function update(Request $request, $id)
{
$this->preventAccessForDemoUsers();
$this->preventAccessInDemoMode();
$this->checkPermissionOrCurrentUser('users-manage', $id);
$this->validate($request, [
@ -202,7 +202,7 @@ class UserController extends Controller
}
$user->save();
session()->flash('success', trans('settings.users_edit_success'));
$this->showSuccessNotification( trans('settings.users_edit_success'));
$redirectUrl = userCan('users-manage') ? '/settings/users' : ('/settings/users/' . $user->id);
return redirect($redirectUrl);
@ -230,23 +230,23 @@ class UserController extends Controller
*/
public function destroy($id)
{
$this->preventAccessForDemoUsers();
$this->preventAccessInDemoMode();
$this->checkPermissionOrCurrentUser('users-manage', $id);
$user = $this->userRepo->getById($id);
if ($this->userRepo->isOnlyAdmin($user)) {
session()->flash('error', trans('errors.users_cannot_delete_only_admin'));
$this->showErrorNotification( trans('errors.users_cannot_delete_only_admin'));
return redirect($user->getEditUrl());
}
if ($user->system_name === 'public') {
session()->flash('error', trans('errors.users_cannot_delete_guest'));
$this->showErrorNotification( trans('errors.users_cannot_delete_guest'));
return redirect($user->getEditUrl());
}
$this->userRepo->destroy($user);
session()->flash('success', trans('settings.users_delete_success'));
$this->showSuccessNotification( trans('settings.users_delete_success'));
return redirect('/settings/users');
}

View file

@ -32,7 +32,8 @@ class Kernel extends HttpKernel
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\BookStack\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\BookStack\Http\Middleware\Localization::class
\BookStack\Http\Middleware\Localization::class,
\BookStack\Http\Middleware\GlobalViewData::class,
],
'api' => [
'throttle:60,1',

View file

@ -0,0 +1,28 @@
<?php namespace BookStack\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
/**
* Class GlobalViewData
* Sets up data that is accessible to any view rendered by the web routes.
*/
class GlobalViewData
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
view()->share('signedIn', auth()->check());
view()->share('currentUser', user());
return $next($request);
}
}