]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Controller.php
Added in attachment tests
[bookstack] / app / Http / Controllers / Controller.php
index 43292d941a19140f7b341430187173aa1493d4ff..ac430065a70caae150bf880044371193b55468d0 100644 (file)
@@ -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;