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