3 namespace BookStack\Http\Controllers;
5 use BookStack\Exceptions\NotifyException;
6 use BookStack\Facades\Activity;
7 use BookStack\Interfaces\Loggable;
9 use BookStack\Util\WebSafeMimeSniffer;
10 use Illuminate\Foundation\Bus\DispatchesJobs;
11 use Illuminate\Foundation\Validation\ValidatesRequests;
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 $message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
57 throw new NotifyException($message, '/', 403);
61 * Checks that the current user has the given permission otherwise throw an exception.
63 protected function checkPermission(string $permission): void
65 if (!user() || !user()->can($permission)) {
66 $this->showPermissionError();
71 * Check the current user's permissions against an ownable item otherwise throw an exception.
73 protected function checkOwnablePermission(string $permission, Model $ownable): void
75 if (!userCan($permission, $ownable)) {
76 $this->showPermissionError();
81 * Check if a user has a permission or bypass the permission
82 * check if the given callback resolves true.
84 protected function checkPermissionOr(string $permission, callable $callback): void
86 if ($callback() !== true) {
87 $this->checkPermission($permission);
92 * Check if the current user has a permission or bypass if the provided user
93 * id matches the current user.
95 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
97 $this->checkPermissionOr($permission, function () use ($userId) {
98 return $userId === user()->id;
103 * Send back a json error message.
105 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
107 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
111 * Create a response that forces a download in the browser.
113 protected function downloadResponse(string $content, string $fileName): Response
115 return response()->make($content, 200, [
116 'Content-Type' => 'application/octet-stream',
117 'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
118 'X-Content-Type-Options' => 'nosniff',
123 * Create a file download response that provides the file with a content-type
124 * correct for the file, in a way so the browser can show the content in browser.
126 protected function inlineDownloadResponse(string $content, string $fileName): Response
128 $mime = (new WebSafeMimeSniffer())->sniff($content);
130 return response()->make($content, 200, [
131 'Content-Type' => $mime,
132 'Content-Disposition' => 'inline; filename="' . $fileName . '"',
133 'X-Content-Type-Options' => 'nosniff',
138 * Show a positive, successful notification to the user on next view load.
140 protected function showSuccessNotification(string $message): void
142 session()->flash('success', $message);
146 * Show a warning notification to the user on next view load.
148 protected function showWarningNotification(string $message): void
150 session()->flash('warning', $message);
154 * Show an error notification to the user on next view load.
156 protected function showErrorNotification(string $message): void
158 session()->flash('error', $message);
162 * Log an activity in the system.
164 * @param string|Loggable $detail
166 protected function logActivity(string $type, $detail = ''): void
168 Activity::add($type, $detail);
172 * Get the validation rules for image files.
174 protected function getImageValidationRules(): array
176 return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];