8000 [Feature] Use new show related method on the authorizer interface · X-Coder264/laravel@1f12602 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f12602

Browse files
committed
[Feature] Use new show related method on the authorizer interface
1 parent 61f56c7 commit 1f12602

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## Added
7+
8+
- The authorizer now has separate `showRelated()` and `showRelationship()` methods. Previously both these controller
9+
actions were authorized via the single `showRelationship()` method. Adding the new `showRelated` method means
10+
developers can now implement separate authorization logic for these two actions if desired. Our default implementation
11+
remains unchanged - both are authorized using the `view<RelationshipName>` method on the relevant policy.
12+
613
## [1.0.0-beta.4] - 2021-06-02
714

815
### Fixed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"require": {
2626
"php": "^7.4|^8.0",
2727
"ext-json": "*",
28-
"laravel-json-api/core": "^1.0.0-beta.4",
28+
"laravel-json-api/core": "^1.0.0-beta.5",
2929
"laravel-json-api/eloquent": "^1.0.0-beta.5",
3030
"laravel-json-api/encoder-neomerx": "^1.0.0-beta.1",
3131
"laravel-json-api/exceptions": "^1.0.0-beta.2",

src/Http/Requests/FormRequest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Illuminate\Contracts\Auth\Guard;
2424
use Illuminate\Database\Eloquent\Model;
2525
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
26+
use Illuminate\Support\Str;
2627
use LaravelJsonApi\Contracts\Schema\Schema;
2728
use LaravelJsonApi\Core\JsonApiService;
2829
use LaravelJsonApi\Validation\Factory as ValidationFactory;
@@ -85,13 +86,23 @@ public function isViewingOne(): bool
8586
}
8687

8788
/**
88-
* Is this a request to view resources in a relationship (Read related/relationship actions.)
89+
* Is this a request to view related resources in a relationship? (Show-related action.)
90+
*
91+
* @return bool
92+
*/
93+
public function isViewingRelated(): bool
94+
{
95+
return $this->isMethod('GET') && $this->isRelationship() && !$this->urlHasRelationships();
96+
}
97+
98+
/**
99+
* Is this a request to view resource identifiers in a relationship? (Show-relationship action.)
89100
*
90101
* @return bool
91102
*/
92103
public function isViewingRelationship(): bool
93104
{
94-
return $this->isMethod('GET') && $this->isRelationship();
105+
return $this->isMethod('GET') && $this->isRelationship() && $this->urlHasRelationships();
95106
}
96107

97108
/**
@@ -320,4 +331,14 @@ private function doesntHaveResourceId(): bool
320331
{
321332
return !$this->hasResourceId();
322333
}
334+
335+
/**
336+
* Does the URL contain the keyword "relationships".
337+
*
338+
* @return bool
339+
*/
340+
private function urlHasRelationships(): bool
341+
{
342+
return Str::of($this->url())->contains('relationships');
343+
}
323344
}

src/Http/Requests/ResourceQuery.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ public function authorizeResource(Authorizer $authorizer): bool
126126
return $authorizer->show($this, $this->modelOrFail());
127127
}
128128

129+
if ($this->isViewingRelated()) {
130+
return $authorizer->showRelated(
131+
$this,
132+
$this->modelOrFail(),
133+
$this->jsonApi()->route()->fieldName(),
134+
);
135+
}
136+
129137
if ($this->isViewingRelationship()) {
130138
return $authorizer->showRelationship(
131139
$this,

0 commit comments

Comments
 (0)
0