3 namespace BookStack\Http\Controllers;
5 use BookStack\Auth\User;
7 use Illuminate\Foundation\Bus\DispatchesJobs;
8 use Illuminate\Foundation\Validation\ValidatesRequests;
9 use Illuminate\Http\Exceptions\HttpResponseException;
10 use Illuminate\Http\Request;
11 use Illuminate\Routing\Controller as BaseController;
13 abstract class Controller extends BaseController
15 use DispatchesJobs, ValidatesRequests;
20 protected $currentUser;
28 * Controller constructor.
30 public function __construct()
32 $this->currentUser = user();
33 $this->signedIn = auth()->check();
37 * Stops the application and shows a permission error if
38 * the application is in demo mode.
40 protected function preventAccessInDemoMode()
42 if (config('app.env') === 'demo') {
43 $this->showPermissionError();
48 * Adds the page title into the view.
51 public function setPageTitle($title)
53 view()->share('pageTitle', $title);
57 * On a permission error redirect to home and display.
58 * the error as a notification.
60 protected function showPermissionError()
62 if (request()->wantsJson()) {
63 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
65 $response = redirect('/');
66 $this->showErrorNotification( trans('errors.permission'));
69 throw new HttpResponseException($response);
73 * Checks for a permission.
74 * @param string $permissionName
75 * @return bool|\Illuminate\Http\RedirectResponse
77 protected function checkPermission($permissionName)
79 if (!user() || !user()->can($permissionName)) {
80 $this->showPermissionError();
86 * Check the current user's permissions against an ownable item.
88 * @param Ownable $ownable
91 protected function checkOwnablePermission($permission, Ownable $ownable)
93 if (userCan($permission, $ownable)) {
96 return $this->showPermissionError();
100 * Check if a user has a permission or bypass if the callback is true.
101 * @param $permissionName
105 protected function checkPermissionOr($permissionName, $callback)
107 $callbackResult = $callback();
108 if ($callbackResult === false) {
109 $this->checkPermission($permissionName);
115 * Check if the current user has a permission or bypass if the provided user
116 * id matches the current user.
117 * @param string $permissionName
121 protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
123 return $this->checkPermissionOr($permissionName, function () use ($userId) {
124 return $userId === $this->currentUser->id;
129 * Send back a json error message.
130 * @param string $messageText
131 * @param int $statusCode
134 protected function jsonError($messageText = "", $statusCode = 500)
136 return response()->json(['message' => $messageText], $statusCode);
140 * Create the response for when a request fails validation.
141 * @param \Illuminate\Http\Request $request
142 * @param array $errors
143 * @return \Symfony\Component\HttpFoundation\Response
145 protected function buildFailedValidationResponse(Request $request, array $errors)
147 if ($request->expectsJson()) {
148 return response()->json(['validation' => $errors], 422);
151 return redirect()->to($this->getRedirectUrl())
152 ->withInput($request->input())
153 ->withErrors($errors, $this->errorBag());
157 * Create a response that forces a download in the browser.
158 * @param string $content
159 * @param string $fileName
160 * @return \Illuminate\Http\Response
162 protected function downloadResponse(string $content, string $fileName)
164 return response()->make($content, 200, [
165 'Content-Type' => 'application/octet-stream',
166 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
171 * Show a positive, successful notification to the user on next view load.
172 * @param string $message
174 protected function showSuccessNotification(string $message)
176 session()->flash('success', $message);
180 * Show a warning notification to the user on next view load.
181 * @param string $message
183 protected function showWarningNotification(string $message)
185 session()->flash('warning', $message);
189 * Show an error notification to the user on next view load.
190 * @param string $message
192 protected function showErrorNotification(string $message)
194 session()->flash('error', $message);