]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Increased LDAP testing and fixed any Auth-based bugs found
[bookstack] / app / Http / Controllers / Controller.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use HttpRequestException;
6 use Illuminate\Foundation\Bus\DispatchesJobs;
7 use Illuminate\Http\Exception\HttpResponseException;
8 use Illuminate\Routing\Controller as BaseController;
9 use Illuminate\Foundation\Validation\ValidatesRequests;
10 use Illuminate\Support\Facades\Auth;
11 use Illuminate\Support\Facades\Session;
12 use BookStack\User;
13
14 abstract class Controller extends BaseController
15 {
16     use DispatchesJobs, ValidatesRequests;
17
18     /**
19      * @var User static
20      */
21     protected $currentUser;
22     /**
23      * @var bool
24      */
25     protected $signedIn;
26
27     /**
28      * Controller constructor.
29      */
30     public function __construct()
31     {
32         // Get a user instance for the current user
33         $user = auth()->user();
34         if (!$user) $user = User::getDefault();
35
36         // Share variables with views
37         view()->share('signedIn', auth()->check());
38         view()->share('currentUser', $user);
39
40         // Share variables with controllers
41         $this->currentUser = $user;
42         $this->signedIn = auth()->check();
43     }
44
45     /**
46      * Stops the application and shows a permission error if
47      * the application is in demo mode.
48      */
49     protected function preventAccessForDemoUsers()
50     {
51         if (config('app.env') === 'demo') $this->showPermissionError();
52     }
53
54     /**
55      * Adds the page title into the view.
56      * @param $title
57      */
58     public function setPageTitle($title)
59     {
60         view()->share('pageTitle', $title);
61     }
62
63     /**
64      * On a permission error redirect to home and display
65      * the error as a notification.
66      */
67     protected function showPermissionError()
68     {
69         Session::flash('error', trans('errors.permission'));
70         throw new HttpResponseException(
71             redirect('/')
72         );
73     }
74
75     /**
76      * Checks for a permission.
77      *
78      * @param $permissionName
79      * @return bool|\Illuminate\Http\RedirectResponse
80      */
81     protected function checkPermission($permissionName)
82     {
83         if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
84             $this->showPermissionError();
85         }
86
87         return true;
88     }
89
90     /**
91      * Check if a user has a permission or bypass if the callback is true.
92      * @param $permissionName
93      * @param $callback
94      * @return bool
95      */
96     protected function checkPermissionOr($permissionName, $callback)
97     {
98         $callbackResult = $callback();
99         if ($callbackResult === false) $this->checkPermission($permissionName);
100         return true;
101     }
102
103 }