]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Added mulit image-type compatability to manager & app and added scaled image selection
[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      * Adds the page title into the view.
47      * @param $title
48      */
49     public function setPageTitle($title)
50     {
51         view()->share('pageTitle', $title);
52     }
53
54     /**
55      * Checks for a permission.
56      *
57      * @param $permissionName
58      * @return bool|\Illuminate\Http\RedirectResponse
59      */
60     protected function checkPermission($permissionName)
61     {
62         if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
63             Session::flash('error', trans('errors.permission'));
64             throw new HttpResponseException(
65                 redirect('/')
66             );
67         }
68
69         return true;
70     }
71
72     protected function checkPermissionOr($permissionName, $callback)
73     {
74         $callbackResult = $callback();
75         if ($callbackResult === false) $this->checkPermission($permissionName);
76         return true;
77     }
78
79 }