$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');
}
$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('/');
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');
}
}
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));
}
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());
}
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));
}
$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('/');
}
if ($exception instanceof UserTokenExpiredException) {
- session()->flash('error', trans('errors.invite_token_expired'));
+ $this->showErrorNotification( trans('errors.invite_token_expired'));
return redirect('/password/email');
}
$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());
}
$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());
}
$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());
}
}
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());
}
$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());
}
}
* @var User static
*/
protected $currentUser;
+
/**
* @var bool
*/
*/
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();
$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);
'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);
+ }
}
}
if (count($warnings) > 0) {
- session()->flash('warning', implode("\n", $warnings));
+ $this->showWarningNotification( implode("\n", $warnings));
}
$draftsEnabled = $this->signedIn;
$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());
}
$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());
}
// 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'));
}
$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());
}
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();
}
}
$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());
}
$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());
}
}
]);
$this->permissionsRepo->saveNewRole($request->all());
- session()->flash('success', trans('settings.role_create_success'));
+ $this->showSuccessNotification( trans('settings.role_create_success'));
return redirect('/settings/roles');
}
]);
$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');
}
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');
}
}
*/
public function update(Request $request)
{
- $this->preventAccessForDemoUsers();
+ $this->preventAccessInDemoMode();
$this->checkPermission('settings-manage');
$this->validate($request, [
'app_logo' => $this->imageRepo->getImageValidationRules(),
setting()->remove('app-logo');
}
- session()->flash('success', trans('settings.settings_save_success'));
+ $this->showSuccessNotification( trans('settings.settings_save_success'));
return redirect('/settings');
}
$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();
*/
public function update(Request $request, $id)
{
- $this->preventAccessForDemoUsers();
+ $this->preventAccessInDemoMode();
$this->checkPermissionOrCurrentUser('users-manage', $id);
$this->validate($request, [
}
$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);
*/
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');
}
\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',
--- /dev/null
+<?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);
+ }
+
+}
\ No newline at end of file