3 namespace BookStack\Http;
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\App\Model;
7 use BookStack\Exceptions\NotifyException;
8 use BookStack\Facades\Activity;
9 use BookStack\Permissions\Permission;
10 use Illuminate\Foundation\Bus\DispatchesJobs;
11 use Illuminate\Foundation\Validation\ValidatesRequests;
12 use Illuminate\Http\JsonResponse;
13 use Illuminate\Http\RedirectResponse;
14 use Illuminate\Http\Request;
15 use Illuminate\Routing\Controller as BaseController;
17 abstract class Controller extends BaseController
20 use ValidatesRequests;
23 * Check if the current user is signed in.
25 protected function isSignedIn(): bool
27 return auth()->check();
31 * Stops the application and shows a permission error if the application is in demo mode.
33 protected function preventAccessInDemoMode(): void
35 if (config('app.env') === 'demo') {
36 $this->showPermissionError();
41 * Adds the page title into the view.
43 public function setPageTitle(string $title): void
45 view()->share('pageTitle', $title);
49 * On a permission error redirect to home and display the error as a notification.
51 * @throws NotifyException
53 protected function showPermissionError(string $redirectLocation = '/'): never
55 $message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
57 throw new NotifyException($message, $redirectLocation, 403);
61 * Checks that the current user has the given permission otherwise throw an exception.
63 protected function checkPermission(string|Permission $permission): void
65 if (!user() || !user()->can($permission)) {
66 $this->showPermissionError();
71 * Prevent access for guest users beyond this point.
73 protected function preventGuestAccess(): void
75 if (user()->isGuest()) {
76 $this->showPermissionError();
81 * Check the current user's permissions against an ownable item otherwise throw an exception.
83 protected function checkOwnablePermission(string|Permission $permission, Model $ownable, string $redirectLocation = '/'): void
85 if (!userCan($permission, $ownable)) {
86 $this->showPermissionError($redirectLocation);
91 * Check if a user has a permission or bypass the permission
92 * check if the given callback resolves true.
94 protected function checkPermissionOr(string|Permission $permission, callable $callback): void
96 if ($callback() !== true) {
97 $this->checkPermission($permission);
102 * Check if the current user has a permission or bypass if the provided user
103 * id matches the current user.
105 protected function checkPermissionOrCurrentUser(string|Permission $permission, int $userId): void
107 $this->checkPermissionOr($permission, function () use ($userId) {
108 return $userId === user()->id;
113 * Send back a JSON error message.
115 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
117 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
121 * Create and return a new download response factory using the current request.
123 protected function download(): DownloadResponseFactory
125 return new DownloadResponseFactory(request());
129 * Show a positive, successful notification to the user on the next view load.
131 protected function showSuccessNotification(string $message): void
133 session()->flash('success', $message);
137 * Show a warning notification to the user on the next view load.
139 protected function showWarningNotification(string $message): void
141 session()->flash('warning', $message);
145 * Show an error notification to the user on the next view load.
147 protected function showErrorNotification(string $message): void
149 session()->flash('error', $message);
153 * Log an activity in the system.
155 protected function logActivity(string $type, string|Loggable $detail = ''): void
157 Activity::add($type, $detail);
161 * Get the validation rules for image files.
163 protected function getImageValidationRules(): array
165 return ['image_extension', 'mimes:jpeg,png,gif,webp,avif', 'max:' . (config('app.upload_limit') * 1000)];
169 * Redirect to the URL provided in the request as a '_return' parameter.
170 * Will check that the parameter leads to a URL under the root path of the system.
172 protected function redirectToRequest(Request $request): RedirectResponse
174 $basePath = url('/');
175 $returnUrl = $request->input('_return') ?? $basePath;
177 if (!str_starts_with($returnUrl, $basePath)) {
178 return redirect($basePath);
181 return redirect($returnUrl);