3 namespace BookStack\Http\Controllers;
5 use BookStack\Facades\Activity;
6 use BookStack\Interfaces\Loggable;
8 use BookStack\Util\WebSafeMimeSniffer;
9 use Illuminate\Foundation\Bus\DispatchesJobs;
10 use Illuminate\Foundation\Validation\ValidatesRequests;
11 use Illuminate\Http\Exceptions\HttpResponseException;
12 use Illuminate\Http\JsonResponse;
13 use Illuminate\Http\Response;
14 use Illuminate\Routing\Controller as BaseController;
16 abstract class Controller extends BaseController
19 use ValidatesRequests;
22 * Check if the current user is signed in.
24 protected function isSignedIn(): bool
26 return auth()->check();
30 * Stops the application and shows a permission error if
31 * the application is in demo mode.
33 protected function preventAccessInDemoMode()
35 if (config('app.env') === 'demo') {
36 $this->showPermissionError();
41 * Adds the page title into the view.
43 public function setPageTitle(string $title)
45 view()->share('pageTitle', $title);
49 * On a permission error redirect to home and display.
50 * the error as a notification.
54 protected function showPermissionError()
56 if (request()->wantsJson()) {
57 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
59 $response = redirect('/');
60 $this->showErrorNotification(trans('errors.permission'));
63 throw new HttpResponseException($response);
67 * Checks that the current user has the given permission otherwise throw an exception.
69 protected function checkPermission(string $permission): void
71 if (!user() || !user()->can($permission)) {
72 $this->showPermissionError();
77 * Check the current user's permissions against an ownable item otherwise throw an exception.
79 protected function checkOwnablePermission(string $permission, Model $ownable): void
81 if (!userCan($permission, $ownable)) {
82 $this->showPermissionError();
87 * Check if a user has a permission or bypass the permission
88 * check if the given callback resolves true.
90 protected function checkPermissionOr(string $permission, callable $callback): void
92 if ($callback() !== true) {
93 $this->checkPermission($permission);
98 * Check if the current user has a permission or bypass if the provided user
99 * id matches the current user.
101 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
103 $this->checkPermissionOr($permission, function () use ($userId) {
104 return $userId === user()->id;
109 * Send back a json error message.
111 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
113 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
117 * Create a response that forces a download in the browser.
119 protected function downloadResponse(string $content, string $fileName): Response
121 return response()->make($content, 200, [
122 'Content-Type' => 'application/octet-stream',
123 'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
124 'X-Content-Type-Options' => 'nosniff',
129 * Create a file download response that provides the file with a content-type
130 * correct for the file, in a way so the browser can show the content in browser.
132 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 $detail
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(): array
182 return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];