3 namespace BookStack\Http\Controllers;
5 use BookStack\Facades\Activity;
6 use BookStack\Interfaces\Loggable;
8 use BookStack\Util\WebSafeMimeSniffer;
10 use Illuminate\Foundation\Bus\DispatchesJobs;
11 use Illuminate\Foundation\Validation\ValidatesRequests;
12 use Illuminate\Http\Exceptions\HttpResponseException;
13 use Illuminate\Http\JsonResponse;
14 use Illuminate\Http\Response;
15 use Illuminate\Routing\Controller as BaseController;
17 abstract class Controller extends BaseController
20 use ValidatesRequests;
23 * Check if the current user is signed in.
25 protected function isSignedIn(): bool
27 return auth()->check();
31 * Stops the application and shows a permission error if
32 * the application is in demo mode.
34 protected function preventAccessInDemoMode()
36 if (config('app.env') === 'demo') {
37 $this->showPermissionError();
42 * Adds the page title into the view.
44 public function setPageTitle(string $title)
46 view()->share('pageTitle', $title);
50 * On a permission error redirect to home and display.
51 * the error as a notification.
53 protected function showPermissionError()
55 if (request()->wantsJson()) {
56 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
58 $response = redirect('/');
59 $this->showErrorNotification(trans('errors.permission'));
62 throw new HttpResponseException($response);
66 * Checks that the current user has the given permission otherwise throw an exception.
68 protected function checkPermission(string $permission): void
70 if (!user() || !user()->can($permission)) {
71 $this->showPermissionError();
76 * Check the current user's permissions against an ownable item otherwise throw an exception.
78 protected function checkOwnablePermission(string $permission, Model $ownable): void
80 if (!userCan($permission, $ownable)) {
81 $this->showPermissionError();
86 * Check if a user has a permission or bypass the permission
87 * check if the given callback resolves true.
89 protected function checkPermissionOr(string $permission, callable $callback): void
91 if ($callback() !== true) {
92 $this->checkPermission($permission);
97 * Check if the current user has a permission or bypass if the provided user
98 * id matches the current user.
100 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
102 $this->checkPermissionOr($permission, function () use ($userId) {
103 return $userId === user()->id;
108 * Send back a json error message.
110 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
112 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
116 * Create a response that forces a download in the browser.
118 protected function downloadResponse(string $content, string $fileName): Response
120 return response()->make($content, 200, [
121 'Content-Type' => 'application/octet-stream',
122 'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
123 'X-Content-Type-Options' => 'nosniff',
128 * Create a file download response that provides the file with a content-type
129 * correct for the file, in a way so the browser can show the content in browser.
131 protected function inlineDownloadResponse(string $content, string $fileName): Response
134 $mime = (new WebSafeMimeSniffer)->sniff($content);
136 return response()->make($content, 200, [
137 'Content-Type' => $mime,
138 'Content-Disposition' => 'inline; filename="' . $fileName . '"',
139 'X-Content-Type-Options' => 'nosniff',
144 * Show a positive, successful notification to the user on next view load.
146 protected function showSuccessNotification(string $message): void
148 session()->flash('success', $message);
152 * Show a warning notification to the user on next view load.
154 protected function showWarningNotification(string $message): void
156 session()->flash('warning', $message);
160 * Show an error notification to the user on next view load.
162 protected function showErrorNotification(string $message): void
164 session()->flash('error', $message);
168 * Log an activity in the system.
170 * @param string|Loggable
172 protected function logActivity(string $type, $detail = ''): void
174 Activity::add($type, $detail);
178 * Get the validation rules for image files.
180 protected function getImageValidationRules(): string
182 return 'image_extension|mimes:jpeg,png,gif,webp';