]> BookStack Code Mirror - bookstack/blob - app/Http/Controller.php
Cleaned up namespacing in routes
[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      * Check the current user's permissions against an ownable item otherwise throw an exception.
71      */
72     protected function checkOwnablePermission(string $permission, Model $ownable): void
73     {
74         if (!userCan($permission, $ownable)) {
75             $this->showPermissionError();
76         }
77     }
78
79     /**
80      * Check if a user has a permission or bypass the permission
81      * check if the given callback resolves true.
82      */
83     protected function checkPermissionOr(string $permission, callable $callback): void
84     {
85         if ($callback() !== true) {
86             $this->checkPermission($permission);
87         }
88     }
89
90     /**
91      * Check if the current user has a permission or bypass if the provided user
92      * id matches the current user.
93      */
94     protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
95     {
96         $this->checkPermissionOr($permission, function () use ($userId) {
97             return $userId === user()->id;
98         });
99     }
100
101     /**
102      * Send back a json error message.
103      */
104     protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
105     {
106         return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
107     }
108
109     /**
110      * Create and return a new download response factory using the current request.
111      */
112     protected function download(): DownloadResponseFactory
113     {
114         return new DownloadResponseFactory(request());
115     }
116
117     /**
118      * Show a positive, successful notification to the user on next view load.
119      */
120     protected function showSuccessNotification(string $message): void
121     {
122         session()->flash('success', $message);
123     }
124
125     /**
126      * Show a warning notification to the user on next view load.
127      */
128     protected function showWarningNotification(string $message): void
129     {
130         session()->flash('warning', $message);
131     }
132
133     /**
134      * Show an error notification to the user on next view load.
135      */
136     protected function showErrorNotification(string $message): void
137     {
138         session()->flash('error', $message);
139     }
140
141     /**
142      * Log an activity in the system.
143      *
144      * @param string|Loggable $detail
145      */
146     protected function logActivity(string $type, $detail = ''): void
147     {
148         Activity::add($type, $detail);
149     }
150
151     /**
152      * Get the validation rules for image files.
153      */
154     protected function getImageValidationRules(): array
155     {
156         return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];
157     }
158 }