3 namespace BookStack\Http\Controllers;
6 use Illuminate\Foundation\Bus\DispatchesJobs;
7 use Illuminate\Http\Exceptions\HttpResponseException;
8 use Illuminate\Http\Request;
9 use Illuminate\Routing\Controller as BaseController;
10 use Illuminate\Foundation\Validation\ValidatesRequests;
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 * Send back a json error message.
128 * @param string $messageText
129 * @param int $statusCode
132 protected function jsonError($messageText = "", $statusCode = 500)
134 return response()->json(['message' => $messageText], $statusCode);
138 * Create the response for when a request fails validation.
139 * @param \Illuminate\Http\Request $request
140 * @param array $errors
141 * @return \Symfony\Component\HttpFoundation\Response
143 protected function buildFailedValidationResponse(Request $request, array $errors)
145 if ($request->expectsJson()) {
146 return response()->json(['validation' => $errors], 422);
149 return redirect()->to($this->getRedirectUrl())
150 ->withInput($request->input())
151 ->withErrors($errors, $this->errorBag());
155 * Create a response that forces a download in the browser.
156 * @param string $content
157 * @param string $fileName
158 * @return \Illuminate\Http\Response
160 protected function downloadResponse(string $content, string $fileName)
162 return response()->make($content, 200, [
163 'Content-Type' => 'application/octet-stream',
164 'Content-Disposition' => 'attachment; filename="' . $fileName . '"'