3 namespace BookStack\Http\Controllers;
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\App\Model;
7 use BookStack\Exceptions\NotifyException;
8 use BookStack\Facades\Activity;
9 use BookStack\Http\Responses\DownloadResponseFactory;
10 use Illuminate\Foundation\Bus\DispatchesJobs;
11 use Illuminate\Foundation\Validation\ValidatesRequests;
12 use Illuminate\Http\JsonResponse;
13 use Illuminate\Routing\Controller as BaseController;
15 abstract class Controller extends BaseController
18 use 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.
53 protected function showPermissionError()
55 $message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
57 throw new NotifyException($message, '/', 403);
61 * Checks that the current user has the given permission otherwise throw an exception.
63 protected function checkPermission(string $permission): void
65 if (!user() || !user()->can($permission)) {
66 $this->showPermissionError();
71 * Check the current user's permissions against an ownable item otherwise throw an exception.
73 protected function checkOwnablePermission(string $permission, Model $ownable): void
75 if (!userCan($permission, $ownable)) {
76 $this->showPermissionError();
81 * Check if a user has a permission or bypass the permission
82 * check if the given callback resolves true.
84 protected function checkPermissionOr(string $permission, callable $callback): void
86 if ($callback() !== true) {
87 $this->checkPermission($permission);
92 * Check if the current user has a permission or bypass if the provided user
93 * id matches the current user.
95 protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
97 $this->checkPermissionOr($permission, function () use ($userId) {
98 return $userId === user()->id;
103 * Send back a json error message.
105 protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
107 return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
111 * Create and return a new download response factory using the current request.
113 protected function download(): DownloadResponseFactory
115 return new DownloadResponseFactory(request());
119 * Show a positive, successful notification to the user on next view load.
121 protected function showSuccessNotification(string $message): void
123 session()->flash('success', $message);
127 * Show a warning notification to the user on next view load.
129 protected function showWarningNotification(string $message): void
131 session()->flash('warning', $message);
135 * Show an error notification to the user on next view load.
137 protected function showErrorNotification(string $message): void
139 session()->flash('error', $message);
143 * Log an activity in the system.
145 * @param string|Loggable $detail
147 protected function logActivity(string $type, $detail = ''): void
149 Activity::add($type, $detail);
153 * Get the validation rules for image files.
155 protected function getImageValidationRules(): array
157 return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];