3 namespace BookStack\Http\Controllers;
5 use BookStack\Facades\Activity;
6 use BookStack\Interfaces\Loggable;
8 use Illuminate\Foundation\Bus\DispatchesJobs;
9 use Illuminate\Foundation\Validation\ValidatesRequests;
10 use Illuminate\Http\Exceptions\HttpResponseException;
11 use Illuminate\Http\JsonResponse;
12 use Illuminate\Http\Response;
13 use Illuminate\Routing\Controller as BaseController;
15 abstract class Controller extends BaseController
17 use DispatchesJobs, ValidatesRequests;
20 * Check if the current user is signed in.
22 protected function isSignedIn(): bool
24 return auth()->check();
28 * Stops the application and shows a permission error if
29 * the application is in demo mode.
31 protected function preventAccessInDemoMode()
33 if (config('app.env') === 'demo') {
34 $this->showPermissionError();
39 * Adds the page title into the view.
41 public function setPageTitle(string $title)
43 view()->share('pageTitle', $title);
47 * On a permission error redirect to home and display.
48 * the error as a notification.
50 protected function showPermissionError()
52 if (request()->wantsJson()) {
53 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
55 $response = redirect('/');
56 $this->showErrorNotification(trans('errors.permission'));
59 throw new HttpResponseException($response);
63 * Checks that the current user has the given permission otherwise throw an exception.
65 protected function checkPermission(string $permission): void
67 if (!user() || !user()->can($permission)) {
68 $this->showPermissionError();
73 * Check the current user's permissions against an ownable item otherwise throw an exception.
75 protected function checkOwnablePermission(string $permission, Ownable $ownable): void
77 if (!userCan($permission, $ownable)) {
78 $this->showPermissionError();
83 * Check if a user has a permission or bypass the permission
84 * check if the given callback resolves true.
86 protected function checkPermissionOr(string $permission, callable $callback): void
88 if ($callback() !== true) {
89 $this->checkPermission($permission);
94 * Check if the current user has a permission or bypass if the provided user
95 * id matches the current user.
97 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
99 $this->checkPermissionOr($permission, function () use ($userId) {
100 return $userId === user()->id;
105 * Send back a json error message.
107 protected function jsonError(string $messageText = "", int $statusCode = 500): JsonResponse
109 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
113 * Create a response that forces a download in the browser.
115 protected function downloadResponse(string $content, string $fileName): Response
117 return response()->make($content, 200, [
118 'Content-Type' => 'application/octet-stream',
119 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
124 * Show a positive, successful notification to the user on next view load.
126 protected function showSuccessNotification(string $message): void
128 session()->flash('success', $message);
132 * Show a warning notification to the user on next view load.
134 protected function showWarningNotification(string $message): void
136 session()->flash('warning', $message);
140 * Show an error notification to the user on next view load.
142 protected function showErrorNotification(string $message): void
144 session()->flash('error', $message);
148 * Log an activity in the system.
149 * @param string|Loggable
151 protected function logActivity(string $type, $detail = ''): void
153 Activity::add($type, $detail);
157 * Get the validation rules for image files.
159 protected function getImageValidationRules(): string
161 return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';