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;
11 use Illuminate\Validation\ValidationException;
13 abstract class Controller extends BaseController
15 use DispatchesJobs, ValidatesRequests;
18 * Controller constructor.
20 public function __construct()
26 * Check if the current user is signed in.
28 protected function isSignedIn(): bool
30 return auth()->check();
34 * Stops the application and shows a permission error if
35 * the application is in demo mode.
37 protected function preventAccessInDemoMode()
39 if (config('app.env') === 'demo') {
40 $this->showPermissionError();
45 * Adds the page title into the view.
48 public function setPageTitle($title)
50 view()->share('pageTitle', $title);
54 * On a permission error redirect to home and display.
55 * the error as a notification.
57 protected function showPermissionError()
59 if (request()->wantsJson()) {
60 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
62 $response = redirect('/');
63 $this->showErrorNotification(trans('errors.permission'));
66 throw new HttpResponseException($response);
70 * Checks for a permission.
71 * @param string $permissionName
72 * @return bool|\Illuminate\Http\RedirectResponse
74 protected function checkPermission($permissionName)
76 if (!user() || !user()->can($permissionName)) {
77 $this->showPermissionError();
83 * Check the current user's permissions against an ownable item.
85 * @param Ownable $ownable
88 protected function checkOwnablePermission($permission, Ownable $ownable)
90 if (userCan($permission, $ownable)) {
93 return $this->showPermissionError();
97 * Check if a user has a permission or bypass if the callback is true.
98 * @param $permissionName
102 protected function checkPermissionOr($permissionName, $callback)
104 $callbackResult = $callback();
105 if ($callbackResult === false) {
106 $this->checkPermission($permissionName);
112 * Check if the current user has a permission or bypass if the provided user
113 * id matches the current user.
114 * @param string $permissionName
118 protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
120 return $this->checkPermissionOr($permissionName, function () use ($userId) {
121 return $userId === user()->id;
126 * Send back a json error message.
127 * @param string $messageText
128 * @param int $statusCode
131 protected function jsonError($messageText = "", $statusCode = 500)
133 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
137 * Create a response that forces a download in the browser.
138 * @param string $content
139 * @param string $fileName
140 * @return \Illuminate\Http\Response
142 protected function downloadResponse(string $content, string $fileName)
144 return response()->make($content, 200, [
145 'Content-Type' => 'application/octet-stream',
146 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
151 * Show a positive, successful notification to the user on next view load.
152 * @param string $message
154 protected function showSuccessNotification(string $message)
156 session()->flash('success', $message);
160 * Show a warning notification to the user on next view load.
161 * @param string $message
163 protected function showWarningNotification(string $message)
165 session()->flash('warning', $message);
169 * Show an error notification to the user on next view load.
170 * @param string $message
172 protected function showErrorNotification(string $message)
174 session()->flash('error', $message);
178 * Get the validation rules for image files.
180 protected function getImageValidationRules(): string
182 return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';