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\Routing\Controller as BaseController;
14 abstract class Controller extends BaseController
17 use 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.
52 protected function showPermissionError()
54 $message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
56 throw new NotifyException($message, '/', 403);
60 * Checks that the current user has the given permission otherwise throw an exception.
62 protected function checkPermission(string $permission): void
64 if (!user() || !user()->can($permission)) {
65 $this->showPermissionError();
70 * Check the current user's permissions against an ownable item otherwise throw an exception.
72 protected function checkOwnablePermission(string $permission, Model $ownable): void
74 if (!userCan($permission, $ownable)) {
75 $this->showPermissionError();
80 * Check if a user has a permission or bypass the permission
81 * check if the given callback resolves true.
83 protected function checkPermissionOr(string $permission, callable $callback): void
85 if ($callback() !== true) {
86 $this->checkPermission($permission);
91 * Check if the current user has a permission or bypass if the provided user
92 * id matches the current user.
94 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
96 $this->checkPermissionOr($permission, function () use ($userId) {
97 return $userId === user()->id;
102 * Send back a json error message.
104 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
106 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
110 * Create and return a new download response factory using the current request.
112 protected function download(): DownloadResponseFactory
114 return new DownloadResponseFactory(request());
118 * Show a positive, successful notification to the user on next view load.
120 protected function showSuccessNotification(string $message): void
122 session()->flash('success', $message);
126 * Show a warning notification to the user on next view load.
128 protected function showWarningNotification(string $message): void
130 session()->flash('warning', $message);
134 * Show an error notification to the user on next view load.
136 protected function showErrorNotification(string $message): void
138 session()->flash('error', $message);
142 * Log an activity in the system.
144 * @param string|Loggable $detail
146 protected function logActivity(string $type, $detail = ''): void
148 Activity::add($type, $detail);
152 * Get the validation rules for image files.
154 protected function getImageValidationRules(): array
156 return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];