8000 Updates for latest changes in cloudcreativity/json-api dependency. · josh-taylor/laravel-json-api@6ca69a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ca69a4

Browse files
committed
Updates for latest changes in cloudcreativity/json-api dependency.
1 parent ccc9731 commit 6ca69a4

File tree

2 files changed

+ 8000 41
-39
lines changed

2 files changed

+41
-39
lines changed

src/Http/Requests/AbstractRequest.php

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use CloudCreativity\JsonApi\Contracts\Validators\DocumentValidatorInterface;
2525
use CloudCreativity\JsonApi\Contracts\Validators\ValidatorProviderInterface;
2626
use CloudCreativity\JsonApi\Exceptions\AuthorizationException;
27-
use CloudCreativity\JsonApi\Exceptions\ErrorCollection;
2827
use CloudCreativity\JsonApi\Exceptions\ValidationException;
2928
use CloudCreativity\JsonApi\Object\Document;
3029
use CloudCreativity\JsonApi\Object\ResourceIdentifier;
@@ -175,21 +174,17 @@ public function validate()
175174
/** Check request parameters are acceptable. */
176175
$this->encodingParameters = $this->validateParameters();
177176

178-
/** Do any pre-document authorization */
179-
if (!$this->authorizeBeforeValidation($errors = new ErrorCollection())) {
180-
throw new AuthorizationException($errors);
181-
}
177+
/** Do any authorization that can occur before the document is validated. */
178+
$this->authorizeBeforeValidation();
182179

183180
/** If a document is expected from the client, validate it. */
184181
if ($this->isExpectingDocument()) {
185182
$this->document = $this->decodeDocument($this->getHttpRequest());
186183
$this->validateDocument();
187184
}
188185

189-
/** Do any post-document authorization. */
190-
if (!$this->authorizeAfterValidation($errors = new ErrorCollection())) {
191-
throw new AuthorizationException($errors);
192-
}
186+
/** Do any authorization that occurs after the document is validated. */
187+
$this->authorizeAfterValidation();
193188

194189
$this->validated = true;
195190
}
@@ -243,73 +238,80 @@ public function isValid()
243238
}
244239

245240
/**
246-
* @param ErrorCollection $errors
247-
* @return bool
241+
* @return void
242+
* @throws AuthorizationException
248243
*/
249-
protected function authorizeBeforeValidation(ErrorCollection $errors)
244+
protected function authorizeBeforeValidation()
250245
{
251246
if (!$this->authorizer) {
25 8000 2-
return true;
247+
return;
253248
}
254249

255250
$parameters = $this->getEncodingParameters();
251+
$authorized = true;
256252

257253
/** Index */
258254
if ($this->isIndex()) {
259-
return $this->authorizer->canReadMany($parameters, $errors);
255+
$authorized = $this->authorizer->canReadMany($parameters);
260256
} /** Read Resource */
261257
elseif ($this->isReadResource()) {
262-
return $this->authorizer->canRead($this->getRecord(), $parameters, $errors);
258+
$authorized = $this->authorizer->canRead($this->getRecord(), $parameters);
263259
} /** Update Resource */
264260
elseif ($this->isUpdateResource()) {
265-
return $this->authorizer->canUpdate($this->getRecord(), $parameters, $errors);
261+
$authorized = $this->authorizer->canUpdate($this->getRecord(), $parameters);
266262
} /** Delete Resource */
267263
elseif ($this->isDeleteResource()) {
268-
return $this->authorizer->canDelete($this->getRecord(), $parameters, $errors);
264+
$authorized = $this->authorizer->canDelete($this->getRecord(), $parameters);
269265
} /** Read Related Resource */
270266
elseif ($this->isReadRelatedResource()) {
271-
return $this->authorizer->canReadRelatedResource(
267+
$authorized = $this->authorizer->canReadRelatedResource(
272268
$this->getRelationshipName(),
273269
$this->getRecord(),
274-
$parameters,
275-
$errors
270+
$parameters
276271
);
277272
} /** Read Relationship Data */
278273
elseif ($this->isReadRelationship()) {
279-
return $this->authorizer->canReadRelationship(
274+
$authorized = $this->authorizer->canReadRelationship(
280275
$this->getRelationshipName(),
281276
$this->getRecord(),
282-
$parameters,
283-
$errors
277+
$parameters
284278
);
285279
} /** Modify Relationship Data */
286280
elseif ($this->isModifyRelationship()) {
287-
return $this->authorizer->canModifyRelationship(
281+
$authorized = $this->authorizer->canModifyRelationship(
288282
$this->getRelationshipName(),
289283
$this->getRecord(),
290-
$parameters,
291-
$errors
284+
$parameters
292285
);
293286
}
294287

295-
return true;
288+
if (!$authorized) {
289+
throw new AuthorizationException($this->authorizer->getErrors());
290+
}
296291
}
297292

298293
/**
299-
* @param ErrorCollection $errors
300-
* @return bool
294+
* @return void
295+
* @throws AuthorizationException
301296
*/
302-
protected function authorizeAfterValidation(ErrorCollection $errors)
297+
protected function authorizeAfterValidation()
303298
{
304-
if ($this->authorizer && $this->isCreateResource()) {
305-
return $this->authorizer->canCreate(
299+
if (!$this->authorizer) {
300+
return;
301+
}
302+
303+
$authorized = true;
304+
305+
if ($this->isCreateResource()) {
306+
$authorized = $this->authorizer->canCreate(
306307
$this->getDocument()->resource(),
307-
$this->getEncodingParameters(),
308-
$errors
308+
$this->getEncodingParameters()
309309
);
310310
}
311311

312-
return true;
312+
if (!$authorized) {
313+
throw new AuthorizationException($this->authorizer->getErrors());
314+
}
313315
}
314316

315317
/**

src/Utils/AbstractErrorBag.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace CloudCreativity\LaravelJsonApi\Utils;
2020

21-
use CloudCreativity\JsonApi\Exceptions\ErrorCollection;
21+
use CloudCreativity\JsonApi\Exceptions\MutableErrorCollection as Errors;
2222
use Countable;
2323
use Generator;
2424
use Illuminate\Contracts\Support\Arrayable;
@@ -41,7 +41,7 @@ abstract class AbstractErrorBag implements Countable, IteratorAggregate, Message
4141

4242
/**
4343
* Create a JSON API error for the supplied message key and detail.
44-
*
44+
*
4545
* @param string $key
4646
* @param string $detail
4747
* @return ErrorInterface
@@ -94,11 +94,11 @@ public function getMessageBag()
9494
}
9595

9696
/**
97-
* @return ErrorCollection
97+
* @return Errors
9898
*/
9999
public function getErrors()
100100
{
101-
return new ErrorCollection($this->toArray());
101+
return new Errors($this->toArray());
102102
}
103103

104104
/**

0 commit comments

Comments
 (0)
0