]> BookStack Code Mirror - bookstack/blob - app/Http/Controller.php
Guest control: Cleaned methods involved in fetching/handling
[bookstack] / app / Http / Controller.php
1 <?php
2
3 namespace BookStack\Http;
4
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\App\Model;
7 use BookStack\Exceptions\NotifyException;
8 use BookStack\Facades\Activity;
9 use Illuminate\Foundation\Bus\DispatchesJobs;
10 use Illuminate\Foundation\Validation\ValidatesRequests;
11 use Illuminate\Http\JsonResponse;
12 use Illuminate\Routing\Controller as BaseController;
13
14 abstract class Controller extends BaseController
15 {
16     use DispatchesJobs;
17     use ValidatesRequests;
18
19     /**
20      * Check if the current user is signed in.
21      */
22     protected function isSignedIn(): bool
23     {
24         return auth()->check();
25     }
26
27     /**
28      * Stops the application and shows a permission error if
29      * the application is in demo mode.
30      */
31     protected function preventAccessInDemoMode()
32     {
33         if (config('app.env') === 'demo') {
34             $this->showPermissionError();
35         }
36     }
37
38     /**
39      * Adds the page title into the view.
40      */
41     public function setPageTitle(string $title)
42     {
43         view()->share('pageTitle', $title);
44     }
45
46     /**
47      * On a permission error redirect to home and display.
48      * the error as a notification.
49      *
50      * @return never
51      */
52     protected function showPermissionError()
53     {
54         $message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
55
56         throw new NotifyException($message, '/', 403);
57     }
58
59     /**
60      * Checks that the current user has the given permission otherwise throw an exception.
61      */
62     protected function checkPermission(string $permission): void
63     {
64         if (!user() || !user()->can($permission)) {
65             $this->showPermissionError();
66         }
67     }
68
69     /**
70      * Prevent access for guest users beyond this point.
71      */
72     protected function preventGuestAccess(): void
73     {
74         if (user()->isGuest()) {
75             $this->showPermissionError();
76         }
77     }
78
79     /**
80      * Check the current user's permissions against an ownable item otherwise throw an exception.
81      */
82     protected function checkOwnablePermission(string $permission, Model $ownable): void
83     {
84         if (!userCan($permission, $ownable)) {
85             $this->showPermissionError();
86         }
87     }
88
89     /**
90      * Check if a user has a permission or bypass the permission
91      * check if the given callback resolves true.
92      */
93     protected function checkPermissionOr(string $permission, callable $callback): void
94     {
95         if ($callback() !== true) {
96             $this->checkPermission($permission);
97         }
98     }
99
100     /**
101      * Check if the current user has a permission or bypass if the provided user
102      * id matches the current user.
103      */
104     protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
105     {
106         $this->checkPermissionOr($permission, function () use ($userId) {
107             return $userId === user()->id;
108         });
109     }
110
111     /**
112      * Send back a json error message.
113      */
114     protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
115     {
116         return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
117     }
118
119     /**
120      * Create and return a new download response factory using the current request.
121      */
122     protected function download(): DownloadResponseFactory
123     {
124         return new DownloadResponseFactory(request());
125     }
126
127     /**
128      * Show a positive, successful notification to the user on next view load.
129      */
130     protected function showSuccessNotification(string $message): void
131     {
132         session()->flash('success', $message);
133     }
134
135     /**
136      * Show a warning notification to the user on next view load.
137      */
138     protected function showWarningNotification(string $message): void
139     {
140         session()->flash('warning', $message);
141     }
142
143     /**
144      * Show an error notification to the user on next view load.
145      */
146     protected function showErrorNotification(string $message): void
147     {
148         session()->flash('error', $message);
149     }
150
151     /**
152      * Log an activity in the system.
153      *
154      * @param string|Loggable $detail
155      */
156     protected function logActivity(string $type, $detail = ''): void
157     {
158         Activity::add($type, $detail);
159     }
160
161     /**
162      * Get the validation rules for image files.
163      */
164     protected function getImageValidationRules(): array
165     {
166         return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];
167     }
168 }