]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Merge branch 'v0.30.x'
[bookstack] / app / Http / Controllers / Controller.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Facades\Activity;
6 use BookStack\Interfaces\Loggable;
7 use BookStack\Ownable;
8 use Illuminate\Foundation\Bus\DispatchesJobs;
9 use Illuminate\Foundation\Validation\ValidatesRequests;
10 use Illuminate\Http\Exceptions\HttpResponseException;
11 use Illuminate\Http\JsonResponse;
12 use Illuminate\Http\Response;
13 use Illuminate\Routing\Controller as BaseController;
14
15 abstract class Controller extends BaseController
16 {
17     use DispatchesJobs, 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     protected function showPermissionError()
51     {
52         if (request()->wantsJson()) {
53             $response = response()->json(['error' => trans('errors.permissionJson')], 403);
54         } else {
55             $response = redirect('/');
56             $this->showErrorNotification(trans('errors.permission'));
57         }
58
59         throw new HttpResponseException($response);
60     }
61
62     /**
63      * Checks that the current user has the given permission otherwise throw an exception.
64      */
65     protected function checkPermission(string $permission): void
66     {
67         if (!user() || !user()->can($permission)) {
68             $this->showPermissionError();
69         }
70     }
71
72     /**
73      * Check the current user's permissions against an ownable item otherwise throw an exception.
74      */
75     protected function checkOwnablePermission(string $permission, Ownable $ownable): void
76     {
77         if (!userCan($permission, $ownable)) {
78             $this->showPermissionError();
79         }
80     }
81
82     /**
83      * Check if a user has a permission or bypass the permission
84      * check if the given callback resolves true.
85      */
86     protected function checkPermissionOr(string $permission, callable $callback): void
87     {
88         if ($callback() !== true) {
89             $this->checkPermission($permission);
90         }
91     }
92
93     /**
94      * Check if the current user has a permission or bypass if the provided user
95      * id matches the current user.
96      */
97     protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
98     {
99         $this->checkPermissionOr($permission, function () use ($userId) {
100             return $userId === user()->id;
101         });
102     }
103
104     /**
105      * Send back a json error message.
106      */
107     protected function jsonError(string $messageText = "", int $statusCode = 500): JsonResponse
108     {
109         return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
110     }
111
112     /**
113      * Create a response that forces a download in the browser.
114      */
115     protected function downloadResponse(string $content, string $fileName): Response
116     {
117         return response()->make($content, 200, [
118             'Content-Type'        => 'application/octet-stream',
119             'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
120         ]);
121     }
122
123     /**
124      * Show a positive, successful notification to the user on next view load.
125      */
126     protected function showSuccessNotification(string $message): void
127     {
128         session()->flash('success', $message);
129     }
130
131     /**
132      * Show a warning notification to the user on next view load.
133      */
134     protected function showWarningNotification(string $message): void
135     {
136         session()->flash('warning', $message);
137     }
138
139     /**
140      * Show an error notification to the user on next view load.
141      */
142     protected function showErrorNotification(string $message): void
143     {
144         session()->flash('error', $message);
145     }
146
147     /**
148      * Log an activity in the system.
149      * @param string|Loggable
150      */
151     protected function logActivity(string $type, $detail = ''): void
152     {
153         Activity::add($type, $detail);
154     }
155
156     /**
157      * Get the validation rules for image files.
158      */
159     protected function getImageValidationRules(): string
160     {
161         return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';
162     }
163 }