3 namespace BookStack\Http\Controllers;
5 use BookStack\Auth\User;
6 use BookStack\Entities\Entity;
7 use BookStack\Facades\Activity;
9 use Illuminate\Foundation\Bus\DispatchesJobs;
10 use Illuminate\Foundation\Validation\ValidatesRequests;
11 use Illuminate\Http\Exceptions\HttpResponseException;
12 use Illuminate\Http\Request;
13 use Illuminate\Routing\Controller as BaseController;
15 abstract class Controller extends BaseController
17 use DispatchesJobs, ValidatesRequests;
22 protected $currentUser;
30 * Controller constructor.
32 public function __construct()
34 $this->currentUser = user();
35 $this->signedIn = auth()->check();
39 * Stops the application and shows a permission error if
40 * the application is in demo mode.
42 protected function preventAccessInDemoMode()
44 if (config('app.env') === 'demo') {
45 $this->showPermissionError();
50 * Adds the page title into the view.
53 public function setPageTitle($title)
55 view()->share('pageTitle', $title);
59 * On a permission error redirect to home and display.
60 * the error as a notification.
62 protected function showPermissionError()
64 if (request()->wantsJson()) {
65 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
67 $response = redirect('/');
68 $this->showErrorNotification( trans('errors.permission'));
71 throw new HttpResponseException($response);
75 * Checks for a permission.
76 * @param string $permissionName
77 * @return bool|\Illuminate\Http\RedirectResponse
79 protected function checkPermission($permissionName)
81 if (!user() || !user()->can($permissionName)) {
82 $this->showPermissionError();
88 * Check the current user's permissions against an ownable item.
90 * @param Ownable $ownable
93 protected function checkOwnablePermission($permission, Ownable $ownable)
95 if (userCan($permission, $ownable)) {
98 return $this->showPermissionError();
102 * Check if a user has a permission or bypass if the callback is true.
103 * @param $permissionName
107 protected function checkPermissionOr($permissionName, $callback)
109 $callbackResult = $callback();
110 if ($callbackResult === false) {
111 $this->checkPermission($permissionName);
117 * Check if the current user has a permission or bypass if the provided user
118 * id matches the current user.
119 * @param string $permissionName
123 protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
125 return $this->checkPermissionOr($permissionName, function () use ($userId) {
126 return $userId === $this->currentUser->id;
131 * Send back a json error message.
132 * @param string $messageText
133 * @param int $statusCode
136 protected function jsonError($messageText = "", $statusCode = 500)
138 return response()->json(['message' => $messageText], $statusCode);
142 * Create the response for when a request fails validation.
143 * @param \Illuminate\Http\Request $request
144 * @param array $errors
145 * @return \Symfony\Component\HttpFoundation\Response
147 protected function buildFailedValidationResponse(Request $request, array $errors)
149 if ($request->expectsJson()) {
150 return response()->json(['validation' => $errors], 422);
153 return redirect()->to($this->getRedirectUrl())
154 ->withInput($request->input())
155 ->withErrors($errors, $this->errorBag());
159 * Create a response that forces a download in the browser.
160 * @param string $content
161 * @param string $fileName
162 * @return \Illuminate\Http\Response
164 protected function downloadResponse(string $content, string $fileName)
166 return response()->make($content, 200, [
167 'Content-Type' => 'application/octet-stream',
168 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
173 * Show a positive, successful notification to the user on next view load.
174 * @param string $message
176 protected function showSuccessNotification(string $message)
178 session()->flash('success', $message);
182 * Show a warning notification to the user on next view load.
183 * @param string $message
185 protected function showWarningNotification(string $message)
187 session()->flash('warning', $message);
191 * Show an error notification to the user on next view load.
192 * @param string $message
194 protected function showErrorNotification(string $message)
196 session()->flash('error', $message);