3 namespace BookStack\Http\Controllers;
6 use Illuminate\Foundation\Bus\DispatchesJobs;
7 use Illuminate\Foundation\Validation\ValidatesRequests;
8 use Illuminate\Http\Exceptions\HttpResponseException;
9 use Illuminate\Http\Request;
10 use Illuminate\Routing\Controller as BaseController;
12 abstract class Controller extends BaseController
14 use DispatchesJobs, ValidatesRequests;
17 * Controller constructor.
19 public function __construct()
25 * Check if the current user is signed in.
27 protected function isSignedIn(): bool
29 return auth()->check();
33 * Stops the application and shows a permission error if
34 * the application is in demo mode.
36 protected function preventAccessInDemoMode()
38 if (config('app.env') === 'demo') {
39 $this->showPermissionError();
44 * Adds the page title into the view.
47 public function setPageTitle($title)
49 view()->share('pageTitle', $title);
53 * On a permission error redirect to home and display.
54 * the error as a notification.
56 protected function showPermissionError()
58 if (request()->wantsJson()) {
59 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
61 $response = redirect('/');
62 $this->showErrorNotification(trans('errors.permission'));
65 throw new HttpResponseException($response);
69 * Checks for a permission.
70 * @param string $permissionName
71 * @return bool|\Illuminate\Http\RedirectResponse
73 protected function checkPermission($permissionName)
75 if (!user() || !user()->can($permissionName)) {
76 $this->showPermissionError();
82 * Check the current user's permissions against an ownable item.
84 * @param Ownable $ownable
87 protected function checkOwnablePermission($permission, Ownable $ownable)
89 if (userCan($permission, $ownable)) {
92 return $this->showPermissionError();
96 * Check if a user has a permission or bypass if the callback is true.
97 * @param $permissionName
101 protected function checkPermissionOr($permissionName, $callback)
103 $callbackResult = $callback();
104 if ($callbackResult === false) {
105 $this->checkPermission($permissionName);
111 * Check if the current user has a permission or bypass if the provided user
112 * id matches the current user.
113 * @param string $permissionName
117 protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
119 return $this->checkPermissionOr($permissionName, function () use ($userId) {
120 return $userId === user()->id;
125 * Send back a json error message.
126 * @param string $messageText
127 * @param int $statusCode
130 protected function jsonError($messageText = "", $statusCode = 500)
132 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
136 * Create the response for when a request fails validation.
137 * @param \Illuminate\Http\Request $request
138 * @param array $errors
139 * @return \Symfony\Component\HttpFoundation\Response
141 protected function buildFailedValidationResponse(Request $request, array $errors)
143 if ($request->expectsJson()) {
144 return response()->json(['validation' => $errors], 422);
147 return redirect()->to($this->getRedirectUrl())
148 ->withInput($request->input())
149 ->withErrors($errors, $this->errorBag());
153 * Create a response that forces a download in the browser.
154 * @param string $content
155 * @param string $fileName
156 * @return \Illuminate\Http\Response
158 protected function downloadResponse(string $content, string $fileName)
160 return response()->make($content, 200, [
161 'Content-Type' => 'application/octet-stream',
162 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
167 * Show a positive, successful notification to the user on next view load.
168 * @param string $message
170 protected function showSuccessNotification(string $message)
172 session()->flash('success', $message);
176 * Show a warning notification to the user on next view load.
177 * @param string $message
179 protected function showWarningNotification(string $message)
181 session()->flash('warning', $message);
185 * Show an error notification to the user on next view load.
186 * @param string $message
188 protected function showErrorNotification(string $message)
190 session()->flash('error', $message);
194 * Get the validation rules for image files.
196 protected function getImageValidationRules(): string
198 return 'image_extension|no_double_extension|mimes:jpeg,png,gif,bmp,webp,tiff';