X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/fff5bbcee458992443e3732fbcbbbe34f765fcc3..91220239e5d545115ae5844fa978eb5541e4d77e:/app/Http/Controllers/Controller.php diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 43292d941..ac430065a 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -33,17 +33,16 @@ abstract class Controller extends BaseController $this->middleware(function ($request, $next) { // Get a user instance for the current user - $user = auth()->user(); - if (!$user) $user = User::getDefault(); - - // Share variables with views - view()->share('signedIn', auth()->check()); - view()->share('currentUser', $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); }); } @@ -72,8 +71,13 @@ abstract class Controller extends BaseController */ protected function showPermissionError() { - Session::flash('error', trans('errors.permission')); - $response = request()->wantsJson() ? response()->json(['error' => trans('errors.permissionJson')], 403) : redirect('/'); + if (request()->wantsJson()) { + $response = response()->json(['error' => trans('errors.permissionJson')], 403); + } else { + $response = redirect('/'); + session()->flash('error', trans('errors.permission')); + } + throw new HttpResponseException($response); } @@ -84,7 +88,7 @@ abstract class Controller extends BaseController */ protected function checkPermission($permissionName) { - if (!$this->currentUser || !$this->currentUser->can($permissionName)) { + if (!user() || !user()->can($permissionName)) { $this->showPermissionError(); } return true;