]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Merge branch 'master' of git://github.com/kmoj86/BookStack into kmoj86-master
[bookstack] / app / Http / Controllers / Controller.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Ownable;
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;
11 use BookStack\User;
12
13 abstract class Controller extends BaseController
14 {
15     use DispatchesJobs, ValidatesRequests;
16
17     /**
18      * @var User static
19      */
20     protected $currentUser;
21     /**
22      * @var bool
23      */
24     protected $signedIn;
25
26     /**
27      * Controller constructor.
28      */
29     public function __construct()
30     {
31         $this->middleware(function ($request, $next) {
32
33             // Get a user instance for the current user
34             $user = user();
35
36             // Share variables with controllers
37             $this->currentUser = $user;
38             $this->signedIn = auth()->check();
39
40             // Share variables with views
41             view()->share('signedIn', $this->signedIn);
42             view()->share('currentUser', $user);
43
44             return $next($request);
45         });
46     }
47
48     /**
49      * Stops the application and shows a permission error if
50      * the application is in demo mode.
51      */
52     protected function preventAccessForDemoUsers()
53     {
54         if (config('app.env') === 'demo') {
55             $this->showPermissionError();
56         }
57     }
58
59     /**
60      * Adds the page title into the view.
61      * @param $title
62      */
63     public function setPageTitle($title)
64     {
65         view()->share('pageTitle', $title);
66     }
67
68     /**
69      * On a permission error redirect to home and display.
70      * the error as a notification.
71      */
72     protected function showPermissionError()
73     {
74         if (request()->wantsJson()) {
75             $response = response()->json(['error' => trans('errors.permissionJson')], 403);
76         } else {
77             $response = redirect('/');
78             session()->flash('error', trans('errors.permission'));
79         }
80
81         throw new HttpResponseException($response);
82     }
83
84     /**
85      * Checks for a permission.
86      * @param string $permissionName
87      * @return bool|\Illuminate\Http\RedirectResponse
88      */
89     protected function checkPermission($permissionName)
90     {
91         if (!user() || !user()->can($permissionName)) {
92             $this->showPermissionError();
93         }
94         return true;
95     }
96
97     /**
98      * Check the current user's permissions against an ownable item.
99      * @param $permission
100      * @param Ownable $ownable
101      * @return bool
102      */
103     protected function checkOwnablePermission($permission, Ownable $ownable)
104     {
105         if (userCan($permission, $ownable)) {
106             return true;
107         }
108         return $this->showPermissionError();
109     }
110
111     /**
112      * Check if a user has a permission or bypass if the callback is true.
113      * @param $permissionName
114      * @param $callback
115      * @return bool
116      */
117     protected function checkPermissionOr($permissionName, $callback)
118     {
119         $callbackResult = $callback();
120         if ($callbackResult === false) {
121             $this->checkPermission($permissionName);
122         }
123         return true;
124     }
125
126     /**
127      * Send back a json error message.
128      * @param string $messageText
129      * @param int $statusCode
130      * @return mixed
131      */
132     protected function jsonError($messageText = "", $statusCode = 500)
133     {
134         return response()->json(['message' => $messageText], $statusCode);
135     }
136
137     /**
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
142      */
143     protected function buildFailedValidationResponse(Request $request, array $errors)
144     {
145         if ($request->expectsJson()) {
146             return response()->json(['validation' => $errors], 422);
147         }
148
149         return redirect()->to($this->getRedirectUrl())
150             ->withInput($request->input())
151             ->withErrors($errors, $this->errorBag());
152     }
153
154     /**
155      * Create a response that forces a download in the browser.
156      * @param string $content
157      * @param string $fileName
158      * @return \Illuminate\Http\Response
159      */
160     protected function downloadResponse(string $content, string $fileName)
161     {
162         return response()->make($content, 200, [
163             'Content-Type'        => 'application/octet-stream',
164             'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
165         ]);
166     }
167 }