X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/a14b5c33fde770ce6b142f9e5535b4caffcdf121..42d8e9e5bda57a26bbaa3f6e4a9e4e5f94a341e7:/app/Http/Controllers/Controller.php diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 4182743a7..43292d941 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -30,17 +30,22 @@ abstract class Controller extends BaseController */ public function __construct() { - // Get a user instance for the current user - $user = auth()->user(); - if (!$user) $user = User::getDefault(); + $this->middleware(function ($request, $next) { - // Share variables with views - view()->share('signedIn', auth()->check()); - view()->share('currentUser', $user); + // Get a user instance for the current user + $user = auth()->user(); + if (!$user) $user = User::getDefault(); - // Share variables with controllers - $this->currentUser = $user; - $this->signedIn = auth()->check(); + // Share variables with views + view()->share('signedIn', auth()->check()); + view()->share('currentUser', $user); + + // Share variables with controllers + $this->currentUser = $user; + $this->signedIn = auth()->check(); + + return $next($request); + }); } /** @@ -68,7 +73,7 @@ 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('/', 403); + $response = request()->wantsJson() ? response()->json(['error' => trans('errors.permissionJson')], 403) : redirect('/'); throw new HttpResponseException($response); } @@ -93,10 +98,8 @@ abstract class Controller extends BaseController */ protected function checkOwnablePermission($permission, Ownable $ownable) { - $permissionBaseName = strtolower($permission) . '-'; - if (userCan($permissionBaseName . 'all')) return true; - if (userCan($permissionBaseName . 'own') && $ownable->createdBy->id === $this->currentUser->id) return true; - $this->showPermissionError(); + if (userCan($permission, $ownable)) return true; + return $this->showPermissionError(); } /** @@ -112,4 +115,15 @@ abstract class Controller extends BaseController return true; } + /** + * Send back a json error message. + * @param string $messageText + * @param int $statusCode + * @return mixed + */ + protected function jsonError($messageText = "", $statusCode = 500) + { + return response()->json(['message' => $messageText], $statusCode); + } + }