forked from laravel-json-api/laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormRequest.php
330 lines (291 loc) · 8 KB
/
FormRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/*
* Copyright 2024 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace LaravelJsonApi\Laravel\Http\Requests;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Access\Response;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
use Illuminate\Support\Str;
use LaravelJsonApi\Contracts\Schema\Schema;
use LaravelJsonApi\Core\JsonApiService;
use LaravelJsonApi\Validation\Factory as ValidationFactory;
class FormRequest extends BaseFormRequest
{
/**
* @var string
*/
public const JSON_API_MEDIA_TYPE = 'application/vnd.api+json';
/**
* @return bool
*/
public function wantsJsonApi(): bool
{
$acceptable = $this->getAcceptableContentTypes();
return isset($acceptable[0]) && self::JSON_API_MEDIA_TYPE === $acceptable[0];
}
/**
* @return bool
*/
public function acceptsJsonApi(): bool
{
return $this->accepts(self::JSON_API_MEDIA_TYPE);
}
/**
* Determine if the request is sending JSON API content.
*
* @return bool
*/
public function isJsonApi(): bool
{
return $this->matchesType(self::JSON_API_MEDIA_TYPE, $this->header('CONTENT_TYPE'));
}
/**
* Is this a request to view any resource? (Index action.)
*
* @return bool
*/
public function isViewingAny(): bool
{
return $this->isMethod('GET') && $this->doesntHaveResourceId() && $this->isNotRelationship();
}
/**
* Is this a request to view a specific resource? (Read action.)
*
* @return bool
*/
public function isViewingOne(): bool
{
return $this->isMethod('GET') && $this->hasResourceId() && $this->isNotRelationship();
}
/**
* Is this a request to view related resources in a relationship? (Show-related action.)
*
* @return bool
*/
public function isViewingRelated(): bool
{
return $this->isMethod('GET') && $this->isRelationship() && !$this->urlHasRelationships();
}
/**
* Is this a request to view resource identifiers in a relationship? (Show-relationship action.)
*
* @return bool
*/
public function isViewingRelationship(): bool
{
return $this->isMethod('GET') && $this->isRelationship() && $this->urlHasRelationships();
}
/**
* Is this a request to create a resource?
*
* @return bool
*/
public function isCreating(): bool
{
return $this->isMethod('POST') && $this->isNotRelationship();
}
/**
* Is this a request to update a resource?
*
* @return bool
*/
public function isUpdating(): bool
{
return $this->isMethod('PATCH') && $this->isNotRelationship();
}
/**
* Is this a request to create or update a resource?
*
* @return bool
*/
public function isCreatingOrUpdating(): bool
{
return $this->isCreating() || $this->isUpdating();
}
/**
* Is this a request to replace a resource relationship?
*
* @return bool
*/
public function isUpdatingRelationship(): bool
{
return $this->isMethod('PATCH') && $this->isRelationship();
}
/**
* Is this a request to attach records to a resource relationship?
*
* @return bool
*/
public function isAttachingRelationship(): bool
{
return $this->isMethod('POST') && $this->isRelationship();
}
/**
* Is this a request to detach records from a resource relationship?
*
* @return bool
*/
public function isDetachingRelationship(): bool
{
return $this->isMethod('DELETE') && $this->isRelationship();
}
/**
* Is this a request to modify a resource relationship?
*
* @return bool
*/
public function isModifyingRelationship(): bool
{
return $this->isUpdatingRelationship() ||
$this->isAttachingRelationship() ||
$this->isDetachingRelationship();
}
/**
* @return bool
*/
public function isDeleting(): bool
{
return $this->isMethod('DELETE') && $this->isNotRelationship();
}
/**
* Is this a request to view or modify a relationship?
*
* @return bool
*/
public function isRelationship(): bool
{
return $this->jsonApi()->route()->hasRelation();
}
/**
* Is this a request to not view a relationship?
*
* @return bool
*/
public function isNotRelationship(): bool
{
return !$this->isRelationship();
}
/**
* Get the field name for a relationship request.
*
* @return string|null
*/
public function getFieldName(): ?string
{
$route = $this->jsonApi()->route();
if ($route->hasRelation()) {
return $route->fieldName();
}
return null;
}
/**
* Get the JSON API schema for the request.
*
* @return Schema
*/
public function schema(): Schema
{
return $this->jsonApi()->route()->schema();
}
/**
* @return bool
*/
protected function passesAuthorization()
{
try {
/**
* If the developer has implemented the `authorize` method, we
* will return the result if it is a boolean. This allows
* the developer to return a null value to indicate they want
* the default authorization to run.
*/
if (method_exists($this, 'authorize')) {
$result = $this->container->call([$this, 'authorize']);
if ($result !== null) {
return $result instanceof Response ? $result->authorize() : $result;
}
}
/**
* If the developer has not authorized the request themselves,
* we run our default authorization as long as authorization is
* enabled for both the server and the schema (checked via the
* `mustAuthorize()` method).
*/
if (method_exists($this, 'authorizeResource')) {
$result = $this->container->call([$this, 'authorizeResource']);
return $result instanceof Response ? $result->authorize() : $result;
}
} catch (AuthorizationException $ex) {
if (!$ex->hasStatus()) {
$this->failIfUnauthenticated();
}
throw $ex;
}
return true;
}
protected function failIfUnauthenticated()
{
/** @var Guard $auth */
$auth = $this->container->make(Guard::class);
if ($auth->guest()) {
throw new AuthenticationException();
}
}
/**
* @inheritDoc
*/
protected function failedAuthorization()
{
$this->failIfUnauthenticated();
parent::failedAuthorization();
}
/**
* @return ValidationFactory
*/
final protected function validationErrors(): ValidationFactory
{
return $this->container->make(ValidationFactory::class);
}
/**
* @return JsonApiService
*/
final protected function jsonApi(): JsonApiService
{
return $this->container->make(JsonApiService::class);
}
/**
* Is there a resource id?
*
* @return bool
*/
private function hasResourceId(): bool
{
return $this->jsonApi()->route()->hasResourceId();
}
/**
* Is the request not for a specific resource?
*
* @return bool
*/
private function doesntHaveResourceId(): bool
{
return !$this->hasResourceId();
}
/**
* Does the URL contain the keyword "relationships".
*
* @return bool
*/
private function urlHasRelationships(): bool
{
return Str::of($this->url())->contains('relationships');
}
}