]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Controller.php
Update PageRepo.php
[bookstack] / app / Http / Controllers / Controller.php
index 2db96ccf7a60fab8cdc49017db4635ba88e57674..ab37a44a1d7223c816c9ca9aade22b7f96ac193c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Oxbow\Http\Controllers;
+namespace BookStack\Http\Controllers;
 
 use HttpRequestException;
 use Illuminate\Foundation\Bus\DispatchesJobs;
@@ -9,7 +9,7 @@ use Illuminate\Routing\Controller as BaseController;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Session;
-use Oxbow\User;
+use BookStack\User;
 
 abstract class Controller extends BaseController
 {
@@ -30,16 +30,46 @@ abstract class Controller extends BaseController
     public function __construct()
     {
         // Get a user instance for the current user
-        $user = Auth::user();
-        if (!$user) {
-            $user = User::getDefault();
-        }
+        $user = auth()->user();
+        if (!$user) $user = User::getDefault();
+
         // Share variables with views
-        view()->share('signedIn', Auth::check());
+        view()->share('signedIn', auth()->check());
         view()->share('currentUser', $user);
+
         // Share variables with controllers
         $this->currentUser = $user;
-        $this->signedIn = Auth::check();
+        $this->signedIn = auth()->check();
+    }
+
+    /**
+     * Stops the application and shows a permission error if
+     * the application is in demo mode.
+     */
+    protected function preventAccessForDemoUsers()
+    {
+        if (config('app.env') === 'demo') $this->showPermissionError();
+    }
+
+    /**
+     * Adds the page title into the view.
+     * @param $title
+     */
+    public function setPageTitle($title)
+    {
+        view()->share('pageTitle', $title);
+    }
+
+    /**
+     * On a permission error redirect to home and display
+     * the error as a notification.
+     */
+    protected function showPermissionError()
+    {
+        Session::flash('error', trans('errors.permission'));
+        throw new HttpResponseException(
+            redirect('/')
+        );
     }
 
     /**
@@ -51,15 +81,18 @@ abstract class Controller extends BaseController
     protected function checkPermission($permissionName)
     {
         if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
-            Session::flash('error', trans('errors.permission'));
-            throw new HttpResponseException(
-                redirect()->back()
-            );
+            $this->showPermissionError();
         }
 
         return true;
     }
 
+    /**
+     * Check if a user has a permission or bypass if the callback is true.
+     * @param $permissionName
+     * @param $callback
+     * @return bool
+     */
     protected function checkPermissionOr($permissionName, $callback)
     {
         $callbackResult = $callback();