]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Started widening of activity logging
[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     public function __construct()
20     {
21         //
22     }
23
24     /**
25      * Check if the current user is signed in.
26      */
27     protected function isSignedIn(): bool
28     {
29         return auth()->check();
30     }
31
32     /**
33      * Stops the application and shows a permission error if
34      * the application is in demo mode.
35      */
36     protected function preventAccessInDemoMode()
37     {
38         if (config('app.env') === 'demo') {
39             $this->showPermissionError();
40         }
41     }
42
43     /**
44      * Adds the page title into the view.
45      */
46     public function setPageTitle(string $title)
47     {
48         view()->share('pageTitle', $title);
49     }
50
51     /**
52      * On a permission error redirect to home and display.
53      * the error as a notification.
54      */
55     protected function showPermissionError()
56     {
57         if (request()->wantsJson()) {
58             $response = response()->json(['error' => trans('errors.permissionJson')], 403);
59         } else {
60             $response = redirect('/');
61             $this->showErrorNotification(trans('errors.permission'));
62         }
63
64         throw new HttpResponseException($response);
65     }
66
67     /**
68      * Checks that the current user has the given permission otherwise throw an exception.
69      */
70     protected function checkPermission(string $permission): void
71     {
72         if (!user() || !user()->can($permission)) {
73             $this->showPermissionError();
74         }
75     }
76
77     /**
78      * Check the current user's permissions against an ownable item otherwise throw an exception.
79      */
80     protected function checkOwnablePermission(string $permission, Ownable $ownable): void
81     {
82         if (!userCan($permission, $ownable)) {
83             $this->showPermissionError();
84         }
85     }
86
87     /**
88      * Check if a user has a permission or bypass the permission
89      * check if the given callback resolves true.
90      */
91     protected function checkPermissionOr(string $permission, callable $callback): void
92     {
93         if ($callback() !== true) {
94             $this->checkPermission($permission);
95         }
96     }
97
98     /**
99      * Check if the current user has a permission or bypass if the provided user
100      * id matches the current user.
101      */
102     protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
103     {
104         $this->checkPermissionOr($permission, function () use ($userId) {
105             return $userId === user()->id;
106         });
107     }
108
109     /**
110      * Send back a json error message.
111      */
112     protected function jsonError(string $messageText = "", int $statusCode = 500): JsonResponse
113     {
114         return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
115     }
116
117     /**
118      * Create a response that forces a download in the browser.
119      */
120     protected function downloadResponse(string $content, string $fileName): Response
121     {
122         return response()->make($content, 200, [
123             'Content-Type'        => 'application/octet-stream',
124             'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
125         ]);
126     }
127
128     /**
129      * Show a positive, successful notification to the user on next view load.
130      */
131     protected function showSuccessNotification(string $message): void
132     {
133         session()->flash('success', $message);
134     }
135
136     /**
137      * Show a warning notification to the user on next view load.
138      */
139     protected function showWarningNotification(string $message): void
140     {
141         session()->flash('warning', $message);
142     }
143
144     /**
145      * Show an error notification to the user on next view load.
146      */
147     protected function showErrorNotification(string $message): void
148     {
149         session()->flash('error', $message);
150     }
151
152     /**
153      * Log an activity in the system.
154      * @param string|Loggable
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(): string
165     {
166         return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';
167     }
168 }