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