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;
19 public function __construct()
25 * Check if the current user is signed in.
27 protected function isSignedIn(): bool
29 return auth()->check();
33 * Stops the application and shows a permission error if
34 * the application is in demo mode.
36 protected function preventAccessInDemoMode()
38 if (config('app.env') === 'demo') {
39 $this->showPermissionError();
44 * Adds the page title into the view.
46 public function setPageTitle(string $title)
48 view()->share('pageTitle', $title);
52 * On a permission error redirect to home and display.
53 * the error as a notification.
55 protected function showPermissionError()
57 if (request()->wantsJson()) {
58 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
60 $response = redirect('/');
61 $this->showErrorNotification(trans('errors.permission'));
64 throw new HttpResponseException($response);
68 * Checks that the current user has the given permission otherwise throw an exception.
70 protected function checkPermission(string $permission): void
72 if (!user() || !user()->can($permission)) {
73 $this->showPermissionError();
78 * Check the current user's permissions against an ownable item otherwise throw an exception.
80 protected function checkOwnablePermission(string $permission, Ownable $ownable): void
82 if (!userCan($permission, $ownable)) {
83 $this->showPermissionError();
88 * Check if a user has a permission or bypass the permission
89 * check if the given callback resolves true.
91 protected function checkPermissionOr(string $permission, callable $callback): void
93 if ($callback() !== true) {
94 $this->checkPermission($permission);
99 * Check if the current user has a permission or bypass if the provided user
100 * id matches the current user.
102 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
104 $this->checkPermissionOr($permission, function () use ($userId) {
105 return $userId === user()->id;
110 * Send back a json error message.
112 protected function jsonError(string $messageText = "", int $statusCode = 500): JsonResponse
114 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
118 * Create a response that forces a download in the browser.
120 protected function downloadResponse(string $content, string $fileName): Response
122 return response()->make($content, 200, [
123 'Content-Type' => 'application/octet-stream',
124 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
129 * Show a positive, successful notification to the user on next view load.
131 protected function showSuccessNotification(string $message): void
133 session()->flash('success', $message);
137 * Show a warning notification to the user on next view load.
139 protected function showWarningNotification(string $message): void
141 session()->flash('warning', $message);
145 * Show an error notification to the user on next view load.
147 protected function showErrorNotification(string $message): void
149 session()->flash('error', $message);
153 * Log an activity in the system.
154 * @param string|Loggable
156 protected function logActivity(string $type, $detail = ''): void
158 Activity::add($type, $detail);
162 * Get the validation rules for image files.
164 protected function getImageValidationRules(): string
166 return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';