]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Finished initial implementation of custom role system
[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         // Get a user instance for the current user
34         $user = auth()->user();
35         if (!$user) $user = User::getDefault();
36
37         // Share variables with views
38         view()->share('signedIn', auth()->check());
39         view()->share('currentUser', $user);
40
41         // Share variables with controllers
42         $this->currentUser = $user;
43         $this->signedIn = auth()->check();
44     }
45
46     /**
47      * Stops the application and shows a permission error if
48      * the application is in demo mode.
49      */
50     protected function preventAccessForDemoUsers()
51     {
52         if (config('app.env') === 'demo') $this->showPermissionError();
53     }
54
55     /**
56      * Adds the page title into the view.
57      * @param $title
58      */
59     public function setPageTitle($title)
60     {
61         view()->share('pageTitle', $title);
62     }
63
64     /**
65      * On a permission error redirect to home and display.
66      * the error as a notification.
67      */
68     protected function showPermissionError()
69     {
70         Session::flash('error', trans('errors.permission'));
71         throw new HttpResponseException(
72             redirect('/')
73         );
74     }
75
76     /**
77      * Checks for a permission.
78      * @param string $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         return true;
87     }
88
89     /**
90      * Check the current user's permissions against an ownable item.
91      * @param $permission
92      * @param Ownable $ownable
93      * @return bool
94      */
95     protected function checkOwnablePermission($permission, Ownable $ownable)
96     {
97         $permissionBaseName = strtolower($permission) . '-';
98         if (userCan($permissionBaseName . 'all')) return true;
99         if (userCan($permissionBaseName . 'own') && $ownable->createdBy->id === $this->currentUser->id) return true;
100         $this->showPermissionError();
101     }
102
103     /**
104      * Check if a user has a permission or bypass if the callback is true.
105      * @param $permissionName
106      * @param $callback
107      * @return bool
108      */
109     protected function checkPermissionOr($permissionName, $callback)
110     {
111         $callbackResult = $callback();
112         if ($callbackResult === false) $this->checkPermission($permissionName);
113         return true;
114     }
115
116 }