8000 Request updates - throw validation/authorization exceptions. · josh-taylor/laravel-json-api@10db401 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10db401

Browse files
committed
Request updates - throw validation/authorization exceptions.
1 parent 232be1f commit 10db401

File tree

7 files changed

+63
-42
lines changed

7 files changed

+63
-42
lines changed

src/Contracts/Http/Requests/RequestHandlerInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,11 @@ public function getEncodingParameters();
7171
*/
7272
public function getHttpRequest();
7373

74+
/**
75+
* Did validation complete successfully?
76+
*
77+
* @return bool
78+
*/
79+
public function isValid();
80+
7481
}

src/Exceptions/HandlesErrors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function renderJsonApi(Request $request, Exception $e)
5757
/** @var JsonApiService $service */
5858
$service = app(JsonApiService::class);
5959

60-
if (!$service->api()->hasEncoder()) {
60+
if (!$service->getApi()->hasEncoder()) {
6161
return $this->renderWithoutEncoder($e);
6262
}
6363

src/Http/Requests/AbstractRequest.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,15 @@
2323
use CloudCreativity\JsonApi\Contracts\Store\StoreInterface;
2424
use CloudCreativity\JsonApi\Contracts\Validators\DocumentValidatorInterface;
2525
use CloudCreativity\JsonApi\Contracts\Validators\ValidatorProviderInterface;
26-
use CloudCreativity\JsonApi\Document\Error;
26+
use CloudCreativity\JsonApi\Exceptions\AuthorizationException;
27+
use CloudCreativity\JsonApi\Exceptions\ValidationException;
2728
use CloudCreativity\JsonApi\Object\Document;
2829
use CloudCreativity\JsonApi\Object\ResourceIdentifier;
2930
use CloudCreativity\LaravelJsonApi\Contracts\Http\Requests\RequestHandlerInterface;
3031
use CloudCreativity\LaravelJsonApi\Contracts\Pagination\PageParameterHandlerInterface;
3132
use CloudCreativity\LaravelJsonApi\Exceptions\RequestException;
32-
use Exception;
3333
use Illuminate\Http\Request as HttpRequest;
34-
use Illuminate\Http\Response;
3534
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
36-
use Neomerx\JsonApi\Exceptions\JsonApiException;
37-
use RuntimeException;
3835
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
3936

4037
/**
@@ -129,6 +126,11 @@ abstract class AbstractRequest implements RequestHandlerInterface
129126
*/
130127
private $record;
131128

129+
/**
130+
* @var bool|null
131+
*/
132+
private $validated;
133+
132134
/**
133135
* AbstractRequest constructor.
134136
* @param ValidatorProviderInterface $validators
@@ -148,7 +150,7 @@ public function __construct(
148150
public function getResourceType()
149151
{
150152
if (empty($this->resourceType)) {
151-
throw new RuntimeException('The resourceType property must be set on: ' . static::class);
153+
throw new RequestException('The resourceType property must be set on: ' . static::class);
152154
}
153155

154156
return $this->resourceType;
@@ -158,10 +160,13 @@ public function getResourceType()
158160
* Validate the given class instance.
159161
*
160162
* @return void
161-
* @throws Exception
163+
* @throws NotFoundHttpException|ValidationException|AuthorizationException
162164
*/
163165
public function validate()
164166
{
167+
/** Register the current request in the container. */
168+
app()->instance(RequestHandlerInterface::class, $this);
169+
165170
/** Check the URI is valid */
166171
$this->record = !empty($this->getResourceId()) ? $this->findRecord() : null;
167172
$this->validateRelationshipUrl();
@@ -185,8 +190,7 @@ public function validate()
185190
throw $this->denied();
186191
}
187192

188-
/** Register the current request in the container. */
189-
app()->instance(RequestHandlerInterface::class, $this);
193+
$this->validated = true;
190194
}
191195

192196
/**
@@ -229,6 +233,14 @@ public function getEncodingParameters()
229233
return $this->encodingParameters;
230234
}
231235

236+
/**
237+
* @return bool
238+
*/
239+
public function isValid()
240+
{
241+
return (bool) $this->validated;
242+
}
243+
232244
/**
233245
* @return bool
234246
*/
@@ -317,6 +329,7 @@ protected function validateRelationshipUrl()
317329

318330
/**
319331
* @return EncodingParametersInterface
332+
* @throws ValidationException
320333
*/
321334
protected function validateParameters()
322335
{
@@ -328,8 +341,7 @@ protected function validateParameters()
328341
$this->validators->filterResources() : null;
329342

330343
if ($validator && !$validator->isValid((array) $parameters->getFilteringParameters())) {
331-
$errors = $validator->errors();
332-
throw new JsonApiException($errors, Error::getErrorStatus($errors));
344+
throw new ValidationException($validator->errors());
333345
}
334346

335347
return $parameters;
@@ -360,15 +372,14 @@ protected function validator()
360372

361373
/**
362374
* @return void
363-
* @throws JsonApiException
375+
* @throws ValidationException
364376
*/
365377
protected function validateDocument()
366378
{
367379
$validator = $this->validator();
368380

369381
if ($validator && !$validator->isValid($this->getDocument())) {
370-
$errors = $validator->errors();
371-
throw new JsonApiException($errors, Error::getErrorStatus($errors));
382+
throw new ValidationException($validator->errors());
372383
}
373384
}
374385

@@ -424,13 +435,12 @@ protected function allowedPagingParameters()
424435
}
425436

426437
/**
427-
* @return JsonApiException
438+
* @return AuthorizationException
428439
*/
429440
protected function denied()
430441
{
431-
$error = $this->authorizer ?
432-
$this->authorizer->denied() : new Error(null, null, Response::HTTP_FORBIDDEN);
442+
$error = $this->authorizer ? $this->authorizer->denied() : [];
433443

434-
return new JsonApiException($error, $error->getStatus());
444+
return new AuthorizationException($error);
435445
}
436446
}

src/Http/Requests/DecodesDocuments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function decodeDocument(HttpRequest $request)
4141
$service = app(JsonApiService::class);
4242

4343
$object = $service
44-
->api()
44+
->getApi()
4545
->getCodecMatcher()
4646
->getDecoder()
4747
->decode($request->getContent());

src/Http/Responses/Responses.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ protected function createResponse($content, $statusCode, array $headers)
6565
*/
6666
protected function getEncoder()
6767
{
68-
return $this->service->api()->getEncoder();
68+
return $this->service->getApi()->getEncoder();
6969
}
7070

7171 F987
/**
7272
* @return null|string
7373
*/
7474
protected function getUrlPrefix()
7575
{
76-
return $this->service->api()->getUrlPrefix();
76+
return $this->service->getApi()->getUrlPrefix();
7777
}
7878

7979

@@ -86,23 +86,23 @@ protected function getEncodingParameters()
8686
return null;
8787
}
8888

89-
return $this->service->request()->getEncodingParameters();
89+
return $this->service->getRequest()->getEncodingParameters();
9090
}
9191

9292
/**
9393
* @return ContainerInterface
9494
*/
9595
protected function getSchemaContainer()
9696
{
97-
return $this->service->api()->getSchemas();
97+
return $this->service->getApi()->getSchemas();
9898
}
9999

100100
/**
101101
* @return SupportedExtensionsInterface|null
102102
*/
103103
protected function getSupportedExtensions()
104104
{
105-
return $this->service->api()->getSupportedExts();
105+
return $this->service->getApi()->getSupportedExts();
106106
}
107107

108108
/**
@@ -112,7 +112,7 @@ protected function getMediaType()
112112
{
113113
$type = $this
114114
->service
115-
->api()
115+
->getApi()
116116
->getCodecMatcher()
117117
->getEncoderRegisteredMatchedType();
118118

src/Pagination/PageParameterHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected function getParams()
137137
{
138138
return (array) $this
139139
->service
140-
->request()
140+
->getRequest()
141141
->getEncodingParameters()
142142
->getPaginationParameters();
143143
}

src/Services/JsonApiService.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,35 @@ public function __construct(ResourceRegistrar $registrar)
4949
}
5050

5151
/**
52-
* @param $resourceType
53-
* @param $controller
52+
* Register a resource type with the router.
53+
*
54+
* @param string $resourceType
55+
* @param string $controller
5456
* @param array $options
5557
*/
5658
public function resource($resourceType, $controller, array $options = [])
5759
{
5860
$this->registrar->resource($resourceType, $controller, $options);
5961
}
6062

63+
/**
64+
* Has JSON API support been started?
65+
*
66+
* @return bool
67+
*/
68+
public function isActive()
69+
{
70+
return app()->bound(ApiInterface::class);
71+
}
72+
6173
/**
6274
* Get the active API.
6375
*
6476
* An active API will be available once the JSON API middleware has been run.
6577
*
6678
* @return ApiInterface
6779
*/
68-
public function api()
80+
public function getApi()
6981
{
7082
if (!$this->isActive()) {
7183
throw new RuntimeException('No active API. The JSON API middleware has not been run.');
@@ -77,12 +89,12 @@ public function api()
7789
/**
7890
* Get the handler for the current HTTP Request.
7991
*
80-
* A request handler will be registered if a request has completed validation
81-
* upon resolution from the service container.
92+
* A request handler will be registered if a request object has been created via the
93+
* service container (which starts validation).
8294
*
8395
* @return RequestHandlerInterface
8496
*/
85-
public function request()
97+
public function getRequest()
8698
{
8799
if (!app()->bound(RequestHandlerInterface::class)) {
88100
throw new RuntimeException('No active JSON API request.');
@@ -92,18 +104,10 @@ public function request()
92104
}
93105

94106
/**
95-
* Has JSON API support been started?
107+
* Has a request handler been registered?
96108
*
97109
* @return bool
98110
*/
99-
public function isActive()
100-
{
101-
return app()->bound(ApiInterface::class);
102-
}
103-
104-
/**
105-
* @return bool
106-
*/
107111
public function hasRequest()
108112
{
109113
return app()->bound(RequestHandlerInterface::class);

0 commit comments

Comments
 (0)
0