]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Merge branch 'diff' of git://github.com/younes0/BookStack into younes0-diff
[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 = auth()->user();
37             if (!$user) $user = User::getDefault();
38
39             // Share variables with views
40             view()->share('signedIn', auth()->check());
41             view()->share('currentUser', $user);
42
43             // Share variables with controllers
44             $this->currentUser = $user;
45             $this->signedIn = auth()->check();
46
47             return $next($request);
48         });
49     }
50
51     /**
52      * Stops the application and shows a permission error if
53      * the application is in demo mode.
54      */
55     protected function preventAccessForDemoUsers()
56     {
57         if (config('app.env') === 'demo') $this->showPermissionError();
58     }
59
60     /**
61      * Adds the page title into the view.
62      * @param $title
63      */
64     public function setPageTitle($title)
65     {
66         view()->share('pageTitle', $title);
67     }
68
69     /**
70      * On a permission error redirect to home and display.
71      * the error as a notification.
72      */
73     protected function showPermissionError()
74     {
75         Session::flash('error', trans('errors.permission'));
76         $response = request()->wantsJson() ? response()->json(['error' => trans('errors.permissionJson')], 403) : redirect('/');
77         throw new HttpResponseException($response);
78     }
79
80     /**
81      * Checks for a permission.
82      * @param string $permissionName
83      * @return bool|\Illuminate\Http\RedirectResponse
84      */
85     protected function checkPermission($permissionName)
86     {
87         if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
88             $this->showPermissionError();
89         }
90         return true;
91     }
92
93     /**
94      * Check the current user's permissions against an ownable item.
95      * @param $permission
96      * @param Ownable $ownable
97      * @return bool
98      */
99     protected function checkOwnablePermission($permission, Ownable $ownable)
100     {
101         if (userCan($permission, $ownable)) return true;
102         return $this->showPermissionError();
103     }
104
105     /**
106      * Check if a user has a permission or bypass if the callback is true.
107      * @param $permissionName
108      * @param $callback
109      * @return bool
110      */
111     protected function checkPermissionOr($permissionName, $callback)
112     {
113         $callbackResult = $callback();
114         if ($callbackResult === false) $this->checkPermission($permissionName);
115         return true;
116     }
117
118     /**
119      * Send back a json error message.
120      * @param string $messageText
121      * @param int $statusCode
122      * @return mixed
123      */
124     protected function jsonError($messageText = "", $statusCode = 500)
125     {
126         return response()->json(['message' => $messageText], $statusCode);
127     }
128
129 }