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 * Prevent access for guest users beyond this point.
72 protected function preventGuestAccess(): void
74 if (user()->isGuest()) {
75 $this->showPermissionError();
80 * Check the current user's permissions against an ownable item otherwise throw an exception.
82 protected function checkOwnablePermission(string $permission, Model $ownable): void
84 if (!userCan($permission, $ownable)) {
85 $this->showPermissionError();
90 * Check if a user has a permission or bypass the permission
91 * check if the given callback resolves true.
93 protected function checkPermissionOr(string $permission, callable $callback): void
95 if ($callback() !== true) {
96 $this->checkPermission($permission);
101 * Check if the current user has a permission or bypass if the provided user
102 * id matches the current user.
104 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
106 $this->checkPermissionOr($permission, function () use ($userId) {
107 return $userId === user()->id;
112 * Send back a json error message.
114 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
116 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
120 * Create and return a new download response factory using the current request.
122 protected function download(): DownloadResponseFactory
124 return new DownloadResponseFactory(request());
128 * Show a positive, successful notification to the user on next view load.
130 protected function showSuccessNotification(string $message): void
132 session()->flash('success', $message);
136 * Show a warning notification to the user on next view load.
138 protected function showWarningNotification(string $message): void
140 session()->flash('warning', $message);
144 * Show an error notification to the user on next view load.
146 protected function showErrorNotification(string $message): void
148 session()->flash('error', $message);
152 * Log an activity in the system.
154 * @param string|Loggable $detail
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(): array
166 return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];