3 namespace BookStack\Http\Controllers;
5 use BookStack\Facades\Activity;
6 use BookStack\Interfaces\Loggable;
7 use BookStack\HasCreatorAndUpdater;
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
18 use DispatchesJobs, ValidatesRequests;
21 * Check if the current user is signed in.
23 protected function isSignedIn(): bool
25 return auth()->check();
29 * Stops the application and shows a permission error if
30 * the application is in demo mode.
32 protected function preventAccessInDemoMode()
34 if (config('app.env') === 'demo') {
35 $this->showPermissionError();
40 * Adds the page title into the view.
42 public function setPageTitle(string $title)
44 view()->share('pageTitle', $title);
48 * On a permission error redirect to home and display.
49 * the error as a notification.
51 protected function showPermissionError()
53 if (request()->wantsJson()) {
54 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
56 $response = redirect('/');
57 $this->showErrorNotification(trans('errors.permission'));
60 throw new HttpResponseException($response);
64 * Checks that the current user has the given permission otherwise throw an exception.
66 protected function checkPermission(string $permission): void
68 if (!user() || !user()->can($permission)) {
69 $this->showPermissionError();
74 * Check the current user's permissions against an ownable item otherwise throw an exception.
76 protected function checkOwnablePermission(string $permission, Model $ownable): void
78 if (!userCan($permission, $ownable)) {
79 $this->showPermissionError();
84 * Check if a user has a permission or bypass the permission
85 * check if the given callback resolves true.
87 protected function checkPermissionOr(string $permission, callable $callback): void
89 if ($callback() !== true) {
90 $this->checkPermission($permission);
95 * Check if the current user has a permission or bypass if the provided user
96 * id matches the current user.
98 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
100 $this->checkPermissionOr($permission, function () use ($userId) {
101 return $userId === user()->id;
106 * Send back a json error message.
108 protected function jsonError(string $messageText = "", int $statusCode = 500): JsonResponse
110 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
114 * Create a response that forces a download in the browser.
116 protected function downloadResponse(string $content, string $fileName): Response
118 return response()->make($content, 200, [
119 'Content-Type' => 'application/octet-stream',
120 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
125 * Show a positive, successful notification to the user on next view load.
127 protected function showSuccessNotification(string $message): void
129 session()->flash('success', $message);
133 * Show a warning notification to the user on next view load.
135 protected function showWarningNotification(string $message): void
137 session()->flash('warning', $message);
141 * Show an error notification to the user on next view load.
143 protected function showErrorNotification(string $message): void
145 session()->flash('error', $message);
149 * Log an activity in the system.
150 * @param string|Loggable
152 protected function logActivity(string $type, $detail = ''): void
154 Activity::add($type, $detail);
158 * Get the validation rules for image files.
160 protected function getImageValidationRules(): string
162 return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';