]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Upgraded to Laravel 5.4
[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') $this->showPermissionError();
55     }
56
57     /**
58      * Adds the page title into the view.
59      * @param $title
60      */
61     public function setPageTitle($title)
62     {
63         view()->share('pageTitle', $title);
64     }
65
66     /**
67      * On a permission error redirect to home and display.
68      * the error as a notification.
69      */
70     protected function showPermissionError()
71     {
72         if (request()->wantsJson()) {
73             $response = response()->json(['error' => trans('errors.permissionJson')], 403);
74         } else {
75             $response = redirect('/');
76             session()->flash('error', trans('errors.permission'));
77         }
78
79         throw new HttpResponseException($response);
80     }
81
82     /**
83      * Checks for a permission.
84      * @param string $permissionName
85      * @return bool|\Illuminate\Http\RedirectResponse
86      */
87     protected function checkPermission($permissionName)
88     {
89         if (!user() || !user()->can($permissionName)) {
90             $this->showPermissionError();
91         }
92         return true;
93     }
94
95     /**
96      * Check the current user's permissions against an ownable item.
97      * @param $permission
98      * @param Ownable $ownable
99      * @return bool
100      */
101     protected function checkOwnablePermission($permission, Ownable $ownable)
102     {
103         if (userCan($permission, $ownable)) return true;
104         return $this->showPermissionError();
105     }
106
107     /**
108      * Check if a user has a permission or bypass if the callback is true.
109      * @param $permissionName
110      * @param $callback
111      * @return bool
112      */
113     protected function checkPermissionOr($permissionName, $callback)
114     {
115         $callbackResult = $callback();
116         if ($callbackResult === false) $this->checkPermission($permissionName);
117         return true;
118     }
119
120     /**
121      * Send back a json error message.
122      * @param string $messageText
123      * @param int $statusCode
124      * @return mixed
125      */
126     protected function jsonError($messageText = "", $statusCode = 500)
127     {
128         return response()->json(['message' => $messageText], $statusCode);
129     }
130
131     /**
132      * Create the response for when a request fails validation.
133      *
134      * @param  \Illuminate\Http\Request  $request
135      * @param  array  $errors
136      * @return \Symfony\Component\HttpFoundation\Response
137      */
138     protected function buildFailedValidationResponse(Request $request, array $errors)
139     {
140         if ($request->expectsJson()) {
141             return response()->json(['validation' => $errors], 422);
142         }
143
144         return redirect()->to($this->getRedirectUrl())
145             ->withInput($request->input())
146             ->withErrors($errors, $this->errorBag());
147     }
148
149 }