]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Controller.php
Refactored notification showing and global view data
[bookstack] / app / Http / Controllers / Controller.php
index fc4f184fcabdff0bf292b6f6c257912ce6d98a2d..e34cb7e5941a137a11bbc6d20d73f97ef1af4a75 100644 (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);
@@ -132,7 +120,7 @@ abstract class Controller extends BaseController
      */
     protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
     {
-        return $this->checkPermissionOr($permissionName, function() use ($userId) {
+        return $this->checkPermissionOr($permissionName, function () use ($userId) {
             return $userId === $this->currentUser->id;
         });
     }
@@ -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);
+    }
 }