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');
58 throw new NotifyException($message, '/', 403);
62 * Checks that the current user has the given permission otherwise throw an exception.
64 protected function checkPermission(string $permission): void
66 if (!user() || !user()->can($permission)) {
67 $this->showPermissionError();
72 * Check the current user's permissions against an ownable item otherwise throw an exception.
74 protected function checkOwnablePermission(string $permission, Model $ownable): void
76 if (!userCan($permission, $ownable)) {
77 $this->showPermissionError();
82 * Check if a user has a permission or bypass the permission
83 * check if the given callback resolves true.
85 protected function checkPermissionOr(string $permission, callable $callback): void
87 if ($callback() !== true) {
88 $this->checkPermission($permission);
93 * Check if the current user has a permission or bypass if the provided user
94 * id matches the current user.
96 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
98 $this->checkPermissionOr($permission, function () use ($userId) {
99 return $userId === user()->id;
104 * Send back a json error message.
106 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
108 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
112 * Create a response that forces a download in the browser.
114 protected function downloadResponse(string $content, string $fileName): Response
116 return response()->make($content, 200, [
117 'Content-Type' => 'application/octet-stream',
118 'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
119 'X-Content-Type-Options' => 'nosniff',
124 * Create a file download response that provides the file with a content-type
125 * correct for the file, in a way so the browser can show the content in browser.
127 protected function inlineDownloadResponse(string $content, string $fileName): Response
129 $mime = (new WebSafeMimeSniffer())->sniff($content);
131 return response()->make($content, 200, [
132 'Content-Type' => $mime,
133 'Content-Disposition' => 'inline; filename="' . $fileName . '"',
134 'X-Content-Type-Options' => 'nosniff',
139 * Show a positive, successful notification to the user on next view load.
141 protected function showSuccessNotification(string $message): void
143 session()->flash('success', $message);
147 * Show a warning notification to the user on next view load.
149 protected function showWarningNotification(string $message): void
151 session()->flash('warning', $message);
155 * Show an error notification to the user on next view load.
157 protected function showErrorNotification(string $message): void
159 session()->flash('error', $message);
163 * Log an activity in the system.
165 * @param string|Loggable $detail
167 protected function logActivity(string $type, $detail = ''): void
169 Activity::add($type, $detail);
173 * Get the validation rules for image files.
175 protected function getImageValidationRules(): array
177 return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];