File tree Expand file tree Collapse file tree 4 files changed +39
-3
lines changed Expand file tree Collapse file tree 4 files changed +39
-3
lines changed Original file line number Diff line number Diff line change 3
3
All notable changes to this project will be documented in this file. This project adheres to
4
4
[ Semantic Versioning] ( http://semver.org/ ) and [ this changelog format] ( http://keepachangelog.com/ ) .
5
5
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
+
6
13
## [ 1.0.0-beta.4] - 2021-06-02
7
14
8
15
### Fixed
Original file line number Diff line number Diff line change 25
25
"require" : {
26
26
"php" : " ^7.4|^8.0" ,
27
27
"ext-json" : " *" ,
28
- "laravel-json-api/core" : " ^1.0.0-beta.4 " ,
28
+ "laravel-json-api/core" : " ^1.0.0-beta.5 " ,
29
29
"laravel-json-api/eloquent" : " ^1.0.0-beta.5" ,
30
30
"laravel-json-api/encoder-neomerx" : " ^1.0.0-beta.1" ,
31
31
"laravel-json-api/exceptions" : " ^1.0.0-beta.2" ,
Original file line number Diff line number Diff line change 23
23
use Illuminate \Contracts \Auth \Guard ;
24
24
use Illuminate \Database \Eloquent \Model ;
25
25
use Illuminate \Foundation \Http \FormRequest as BaseFormRequest ;
26
+ use Illuminate \Support \Str ;
26
27
use LaravelJsonApi \Contracts \Schema \Schema ;
27
28
use LaravelJsonApi \Core \JsonApiService ;
28
29
use LaravelJsonApi \Validation \Factory as ValidationFactory ;
@@ -85,13 +86,23 @@ public function isViewingOne(): bool
85
86
}
86
87
87
88
/**
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.)
89
100
*
90
101
* @return bool
91
102
*/
92
103
public function isViewingRelationship (): bool
93
104
{
94
- return $ this ->isMethod ('GET ' ) && $ this ->isRelationship ();
105
+ return $ this ->isMethod ('GET ' ) && $ this ->isRelationship () && $ this -> urlHasRelationships () ;
95
106
}
96
107
97
108
/**
@@ -320,4 +331,14 @@ private function doesntHaveResourceId(): bool
320
331
{
321
332
return !$ this ->hasResourceId ();
322
333
}
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
+ }
323
344
}
Original file line number Diff line number Diff line change @@ -126,6 +126,14 @@ public function authorizeResource(Authorizer $authorizer): bool
126
126
return $ authorizer ->show ($ this , $ this ->modelOrFail ());
127
127
}
128
128
129
+ if ($ this ->isViewingRelated ()) {
130
+ return $ authorizer ->showRelated (
131
+ $ this ,
132
+ $ this ->modelOrFail (),
133
+ $ this ->jsonApi ()->route ()->fieldName (),
134
+ );
135
+ }
136
+
129
137
if ($ this ->isViewingRelationship ()) {
130
138
return $ authorizer ->showRelationship (
131
139
$ this ,
You can’t perform that action at this time.
0 commit comments