]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Controller.php
Added the `test-migrations` workflow
[bookstack] / app / Http / Controllers / Controller.php
index e34cb7e5941a137a11bbc6d20d73f97ef1af4a75..6a1dfcb0140062d0fcabcffe9174f226f1cacc48 100644 (file)
@@ -2,35 +2,32 @@
 
 namespace BookStack\Http\Controllers;
 
-use BookStack\Auth\User;
 use BookStack\Ownable;
 use Illuminate\Foundation\Bus\DispatchesJobs;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Http\Exceptions\HttpResponseException;
 use Illuminate\Http\Request;
 use Illuminate\Routing\Controller as BaseController;
+use Illuminate\Validation\ValidationException;
 
 abstract class Controller extends BaseController
 {
     use DispatchesJobs, ValidatesRequests;
 
     /**
-     * @var User static
-     */
-    protected $currentUser;
-
-    /**
-     * @var bool
+     * Controller constructor.
      */
-    protected $signedIn;
+    public function __construct()
+    {
+        //
+    }
 
     /**
-     * Controller constructor.
+     * Check if the current user is signed in.
      */
-    public function __construct()
+    protected function isSignedIn(): bool
     {
-        $this->currentUser = user();
-        $this->signedIn = auth()->check();
+        return auth()->check();
     }
 
     /**
@@ -63,7 +60,7 @@ abstract class Controller extends BaseController
             $response = response()->json(['error' => trans('errors.permissionJson')], 403);
         } else {
             $response = redirect('/');
-            $this->showErrorNotification( trans('errors.permission'));
+            $this->showErrorNotification(trans('errors.permission'));
         }
 
         throw new HttpResponseException($response);
@@ -121,7 +118,7 @@ abstract class Controller extends BaseController
     protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
     {
         return $this->checkPermissionOr($permissionName, function () use ($userId) {
-            return $userId === $this->currentUser->id;
+            return $userId === user()->id;
         });
     }
 
@@ -133,24 +130,7 @@ abstract class Controller extends BaseController
      */
     protected function jsonError($messageText = "", $statusCode = 500)
     {
-        return response()->json(['message' => $messageText], $statusCode);
-    }
-
-    /**
-     * Create the response for when a request fails validation.
-     * @param  \Illuminate\Http\Request  $request
-     * @param  array  $errors
-     * @return \Symfony\Component\HttpFoundation\Response
-     */
-    protected function buildFailedValidationResponse(Request $request, array $errors)
-    {
-        if ($request->expectsJson()) {
-            return response()->json(['validation' => $errors], 422);
-        }
-
-        return redirect()->to($this->getRedirectUrl())
-            ->withInput($request->input())
-            ->withErrors($errors, $this->errorBag());
+        return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
     }
 
     /**
@@ -193,4 +173,12 @@ abstract class Controller extends BaseController
     {
         session()->flash('error', $message);
     }
+
+    /**
+     * Get the validation rules for image files.
+     */
+    protected function getImageValidationRules(): string
+    {
+        return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';
+    }
 }