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