3 namespace BookStack\Http\Controllers;
6 use Illuminate\Foundation\Bus\DispatchesJobs;
7 use Illuminate\Http\Exceptions\HttpResponseException;
8 use Illuminate\Http\Request;
9 use Illuminate\Routing\Controller as BaseController;
10 use Illuminate\Foundation\Validation\ValidatesRequests;
13 abstract class Controller extends BaseController
15 use DispatchesJobs, ValidatesRequests;
20 protected $currentUser;
27 * Controller constructor.
29 public function __construct()
31 $this->middleware(function ($request, $next) {
33 // Get a user instance for the current user
36 // Share variables with controllers
37 $this->currentUser = $user;
38 $this->signedIn = auth()->check();
40 // Share variables with views
41 view()->share('signedIn', $this->signedIn);
42 view()->share('currentUser', $user);
44 return $next($request);
49 * Stops the application and shows a permission error if
50 * the application is in demo mode.
52 protected function preventAccessForDemoUsers()
54 if (config('app.env') === 'demo') $this->showPermissionError();
58 * Adds the page title into the view.
61 public function setPageTitle($title)
63 view()->share('pageTitle', $title);
67 * On a permission error redirect to home and display.
68 * the error as a notification.
70 protected function showPermissionError()
72 if (request()->wantsJson()) {
73 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
75 $response = redirect('/');
76 session()->flash('error', trans('errors.permission'));
79 throw new HttpResponseException($response);
83 * Checks for a permission.
84 * @param string $permissionName
85 * @return bool|\Illuminate\Http\RedirectResponse
87 protected function checkPermission($permissionName)
89 if (!user() || !user()->can($permissionName)) {
90 $this->showPermissionError();
96 * Check the current user's permissions against an ownable item.
98 * @param Ownable $ownable
101 protected function checkOwnablePermission($permission, Ownable $ownable)
103 if (userCan($permission, $ownable)) return true;
104 return $this->showPermissionError();
108 * Check if a user has a permission or bypass if the callback is true.
109 * @param $permissionName
113 protected function checkPermissionOr($permissionName, $callback)
115 $callbackResult = $callback();
116 if ($callbackResult === false) $this->checkPermission($permissionName);
121 * Send back a json error message.
122 * @param string $messageText
123 * @param int $statusCode
126 protected function jsonError($messageText = "", $statusCode = 500)
128 return response()->json(['message' => $messageText], $statusCode);
132 * Create the response for when a request fails validation.
134 * @param \Illuminate\Http\Request $request
135 * @param array $errors
136 * @return \Symfony\Component\HttpFoundation\Response
138 protected function buildFailedValidationResponse(Request $request, array $errors)
140 if ($request->expectsJson()) {
141 return response()->json(['validation' => $errors], 422);
144 return redirect()->to($this->getRedirectUrl())
145 ->withInput($request->input())
146 ->withErrors($errors, $this->errorBag());