3 namespace BookStack\Http\Controllers;
5 use BookStack\Auth\User;
7 use Illuminate\Foundation\Bus\DispatchesJobs;
8 use Illuminate\Foundation\Validation\ValidatesRequests;
9 use Illuminate\Http\Exceptions\HttpResponseException;
10 use Illuminate\Http\Request;
11 use Illuminate\Routing\Controller as BaseController;
13 abstract class Controller extends BaseController
15 use DispatchesJobs, ValidatesRequests;
20 protected $currentUser;
27 * Controller constructor.
29 public function __construct()
31 $this->middleware(function ($request, $next) {
33 // Get a user instance for the current user
36 // Share variables with controllers
37 $this->currentUser = $user;
38 $this->signedIn = auth()->check();
40 // Share variables with views
41 view()->share('signedIn', $this->signedIn);
42 view()->share('currentUser', $user);
44 return $next($request);
49 * Stops the application and shows a permission error if
50 * the application is in demo mode.
52 protected function preventAccessForDemoUsers()
54 if (config('app.env') === 'demo') {
55 $this->showPermissionError();
60 * Adds the page title into the view.
63 public function setPageTitle($title)
65 view()->share('pageTitle', $title);
69 * On a permission error redirect to home and display.
70 * the error as a notification.
72 protected function showPermissionError()
74 if (request()->wantsJson()) {
75 $response = response()->json(['error' => trans('errors.permissionJson')], 403);
77 $response = redirect('/');
78 session()->flash('error', trans('errors.permission'));
81 throw new HttpResponseException($response);
85 * Checks for a permission.
86 * @param string $permissionName
87 * @return bool|\Illuminate\Http\RedirectResponse
89 protected function checkPermission($permissionName)
91 if (!user() || !user()->can($permissionName)) {
92 $this->showPermissionError();
98 * Check the current user's permissions against an ownable item.
100 * @param Ownable $ownable
103 protected function checkOwnablePermission($permission, Ownable $ownable)
105 if (userCan($permission, $ownable)) {
108 return $this->showPermissionError();
112 * Check if a user has a permission or bypass if the callback is true.
113 * @param $permissionName
117 protected function checkPermissionOr($permissionName, $callback)
119 $callbackResult = $callback();
120 if ($callbackResult === false) {
121 $this->checkPermission($permissionName);
127 * Check if the current user has a permission or bypass if the provided user
128 * id matches the current user.
129 * @param string $permissionName
133 protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
135 return $this->checkPermissionOr($permissionName, function () use ($userId) {
136 return $userId === $this->currentUser->id;
141 * Send back a json error message.
142 * @param string $messageText
143 * @param int $statusCode
146 protected function jsonError($messageText = "", $statusCode = 500)
148 return response()->json(['message' => $messageText], $statusCode);
152 * Create the response for when a request fails validation.
153 * @param \Illuminate\Http\Request $request
154 * @param array $errors
155 * @return \Symfony\Component\HttpFoundation\Response
157 protected function buildFailedValidationResponse(Request $request, array $errors)
159 if ($request->expectsJson()) {
160 return response()->json(['validation' => $errors], 422);
163 return redirect()->to($this->getRedirectUrl())
164 ->withInput($request->input())
165 ->withErrors($errors, $this->errorBag());
169 * Create a response that forces a download in the browser.
170 * @param string $content
171 * @param string $fileName
172 * @return \Illuminate\Http\Response
174 protected function downloadResponse(string $content, string $fileName)
176 return response()->make($content, 200, [
177 'Content-Type' => 'application/octet-stream',
178 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'