]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Merge branch 'master' into attachment_drag_drop
[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\Foundation\Validation\ValidatesRequests;
8 use Illuminate\Http\Exceptions\HttpResponseException;
9 use Illuminate\Http\Request;
10 use Illuminate\Routing\Controller as BaseController;
11 use Illuminate\Validation\ValidationException;
12
13 abstract class Controller extends BaseController
14 {
15     use DispatchesJobs, ValidatesRequests;
16
17     /**
18      * Controller constructor.
19      */
20     public function __construct()
21     {
22         //
23     }
24
25     /**
26      * Check if the current user is signed in.
27      */
28     protected function isSignedIn(): bool
29     {
30         return auth()->check();
31     }
32
33     /**
34      * Stops the application and shows a permission error if
35      * the application is in demo mode.
36      */
37     protected function preventAccessInDemoMode()
38     {
39         if (config('app.env') === 'demo') {
40             $this->showPermissionError();
41         }
42     }
43
44     /**
45      * Adds the page title into the view.
46      * @param $title
47      */
48     public function setPageTitle($title)
49     {
50         view()->share('pageTitle', $title);
51     }
52
53     /**
54      * On a permission error redirect to home and display.
55      * the error as a notification.
56      */
57     protected function showPermissionError()
58     {
59         if (request()->wantsJson()) {
60             $response = response()->json(['error' => trans('errors.permissionJson')], 403);
61         } else {
62             $response = redirect('/');
63             $this->showErrorNotification(trans('errors.permission'));
64         }
65
66         throw new HttpResponseException($response);
67     }
68
69     /**
70      * Checks for a permission.
71      * @param string $permissionName
72      * @return bool|\Illuminate\Http\RedirectResponse
73      */
74     protected function checkPermission($permissionName)
75     {
76         if (!user() || !user()->can($permissionName)) {
77             $this->showPermissionError();
78         }
79         return true;
80     }
81
82     /**
83      * Check the current user's permissions against an ownable item.
84      * @param $permission
85      * @param Ownable $ownable
86      * @return bool
87      */
88     protected function checkOwnablePermission($permission, Ownable $ownable)
89     {
90         if (userCan($permission, $ownable)) {
91             return true;
92         }
93         return $this->showPermissionError();
94     }
95
96     /**
97      * Check if a user has a permission or bypass if the callback is true.
98      * @param $permissionName
99      * @param $callback
100      * @return bool
101      */
102     protected function checkPermissionOr($permissionName, $callback)
103     {
104         $callbackResult = $callback();
105         if ($callbackResult === false) {
106             $this->checkPermission($permissionName);
107         }
108         return true;
109     }
110
111     /**
112      * Check if the current user has a permission or bypass if the provided user
113      * id matches the current user.
114      * @param string $permissionName
115      * @param int $userId
116      * @return bool
117      */
118     protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
119     {
120         return $this->checkPermissionOr($permissionName, function () use ($userId) {
121             return $userId === user()->id;
122         });
123     }
124
125     /**
126      * Send back a json error message.
127      * @param string $messageText
128      * @param int $statusCode
129      * @return mixed
130      */
131     protected function jsonError($messageText = "", $statusCode = 500)
132     {
133         return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
134     }
135
136     /**
137      * Create a response that forces a download in the browser.
138      * @param string $content
139      * @param string $fileName
140      * @return \Illuminate\Http\Response
141      */
142     protected function downloadResponse(string $content, string $fileName)
143     {
144         return response()->make($content, 200, [
145             'Content-Type'        => 'application/octet-stream',
146             'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
147         ]);
148     }
149
150     /**
151      * Show a positive, successful notification to the user on next view load.
152      * @param string $message
153      */
154     protected function showSuccessNotification(string $message)
155     {
156         session()->flash('success', $message);
157     }
158
159     /**
160      * Show a warning notification to the user on next view load.
161      * @param string $message
162      */
163     protected function showWarningNotification(string $message)
164     {
165         session()->flash('warning', $message);
166     }
167
168     /**
169      * Show an error notification to the user on next view load.
170      * @param string $message
171      */
172     protected function showErrorNotification(string $message)
173     {
174         session()->flash('error', $message);
175     }
176
177     /**
178      * Get the validation rules for image files.
179      */
180     protected function getImageValidationRules(): string
181     {
182         return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';
183     }
184 }