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 Illuminate\Foundation\Bus\DispatchesJobs;
10 use Illuminate\Foundation\Validation\ValidatesRequests;
11 use Illuminate\Http\JsonResponse;
12 use Illuminate\Http\RedirectResponse;
13 use Illuminate\Http\Request;
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.
52 * @throws NotifyException
54 protected function showPermissionError(string $redirectLocation = '/'): never
56 $message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
58 throw new NotifyException($message, $redirectLocation, 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 * Prevent access for guest users beyond this point.
74 protected function preventGuestAccess(): void
76 if (user()->isGuest()) {
77 $this->showPermissionError();
82 * Check the current user's permissions against an ownable item otherwise throw an exception.
84 protected function checkOwnablePermission(string $permission, Model $ownable, string $redirectLocation = '/'): void
86 if (!userCan($permission, $ownable)) {
87 $this->showPermissionError($redirectLocation);
92 * Check if a user has a permission or bypass the permission
93 * check if the given callback resolves true.
95 protected function checkPermissionOr(string $permission, callable $callback): void
97 if ($callback() !== true) {
98 $this->checkPermission($permission);
103 * Check if the current user has a permission or bypass if the provided user
104 * id matches the current user.
106 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
108 $this->checkPermissionOr($permission, function () use ($userId) {
109 return $userId === user()->id;
114 * Send back a json error message.
116 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
118 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
122 * Create and return a new download response factory using the current request.
124 protected function download(): DownloadResponseFactory
126 return new DownloadResponseFactory(request());
130 * Show a positive, successful notification to the user on next view load.
132 protected function showSuccessNotification(string $message): void
134 session()->flash('success', $message);
138 * Show a warning notification to the user on next view load.
140 protected function showWarningNotification(string $message): void
142 session()->flash('warning', $message);
146 * Show an error notification to the user on next view load.
148 protected function showErrorNotification(string $message): void
150 session()->flash('error', $message);
154 * Log an activity in the system.
156 protected function logActivity(string $type, string|Loggable $detail = ''): void
158 Activity::add($type, $detail);
162 * Get the validation rules for image files.
164 protected function getImageValidationRules(): array
166 return ['image_extension', 'mimes:jpeg,png,gif,webp,avif', 'max:' . (config('app.upload_limit') * 1000)];
170 * Redirect to the URL provided in the request as a '_return' parameter.
171 * Will check that the parameter leads to a URL under the root path of the system.
173 protected function redirectToRequest(Request $request): RedirectResponse
175 $basePath = url('/');
176 $returnUrl = $request->input('_return') ?? $basePath;
178 if (!str_starts_with($returnUrl, $basePath)) {
179 return redirect($basePath);
182 return redirect($returnUrl);