]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Started work on making the public role/user configurable
[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         Session::flash('error', trans('errors.permission'));
75         $response = request()->wantsJson() ? response()->json(['error' => trans('errors.permissionJson')], 403) : redirect('/');
76         throw new HttpResponseException($response);
77     }
78
79     /**
80      * Checks for a permission.
81      * @param string $permissionName
82      * @return bool|\Illuminate\Http\RedirectResponse
83      */
84     protected function checkPermission($permissionName)
85     {
86         if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
87             $this->showPermissionError();
88         }
89         return true;
90     }
91
92     /**
93      * Check the current user's permissions against an ownable item.
94      * @param $permission
95      * @param Ownable $ownable
96      * @return bool
97      */
98     protected function checkOwnablePermission($permission, Ownable $ownable)
99     {
100         if (userCan($permission, $ownable)) return true;
101         return $this->showPermissionError();
102     }
103
104     /**
105      * Check if a user has a permission or bypass if the callback is true.
106      * @param $permissionName
107      * @param $callback
108      * @return bool
109      */
110     protected function checkPermissionOr($permissionName, $callback)
111     {
112         $callbackResult = $callback();
113         if ($callbackResult === false) $this->checkPermission($permissionName);
114         return true;
115     }
116
117     /**
118      * Send back a json error message.
119      * @param string $messageText
120      * @param int $statusCode
121      * @return mixed
122      */
123     protected function jsonError($messageText = "", $statusCode = 500)
124     {
125         return response()->json(['message' => $messageText], $statusCode);
126     }
127
128 }