]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
ac430065a70caae150bf880044371193b55468d0
[bookstack] / app / Http / Controllers / Controller.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Ownable;
6 use HttpRequestException;
7 use Illuminate\Foundation\Bus\DispatchesJobs;
8 use Illuminate\Http\Exception\HttpResponseException;
9 use Illuminate\Routing\Controller as BaseController;
10 use Illuminate\Foundation\Validation\ValidatesRequests;
11 use Illuminate\Support\Facades\Auth;
12 use Illuminate\Support\Facades\Session;
13 use BookStack\User;
14
15 abstract class Controller extends BaseController
16 {
17     use DispatchesJobs, ValidatesRequests;
18
19     /**
20      * @var User static
21      */
22     protected $currentUser;
23     /**
24      * @var bool
25      */
26     protected $signedIn;
27
28     /**
29      * Controller constructor.
30      */
31     public function __construct()
32     {
33         $this->middleware(function ($request, $next) {
34
35             // Get a user instance for the current user
36             $user = user();
37
38             // Share variables with controllers
39             $this->currentUser = $user;
40             $this->signedIn = auth()->check();
41
42             // Share variables with views
43             view()->share('signedIn', $this->signedIn);
44             view()->share('currentUser', $user);
45
46             return $next($request);
47         });
48     }
49
50     /**
51      * Stops the application and shows a permission error if
52      * the application is in demo mode.
53      */
54     protected function preventAccessForDemoUsers()
55     {
56         if (config('app.env') === 'demo') $this->showPermissionError();
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)) return true;
106         return $this->showPermissionError();
107     }
108
109     /**
110      * Check if a user has a permission or bypass if the callback is true.
111      * @param $permissionName
112      * @param $callback
113      * @return bool
114      */
115     protected function checkPermissionOr($permissionName, $callback)
116     {
117         $callbackResult = $callback();
118         if ($callbackResult === false) $this->checkPermission($permissionName);
119         return true;
120     }
121
122     /**
123      * Send back a json error message.
124      * @param string $messageText
125      * @param int $statusCode
126      * @return mixed
127      */
128     protected function jsonError($messageText = "", $statusCode = 500)
129     {
130         return response()->json(['message' => $messageText], $statusCode);
131     }
132
133 }