Skip to content

Commit f3ecaf6

Browse files
committed
feat: update resource authorizer to handle auth responses
1 parent 37bb500 commit f3ecaf6

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

Diff for: src/Core/Auth/Authorizer.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function show(?Request $request, object $model): bool|Response
8787
/**
8888
* @inheritDoc
8989
*/
90-
public function update(Request $request, object $model): bool|Response
90+
public function update(?Request $request, object $model): bool|Response
9191
{
9292
if ($this->mustAuthorize()) {
9393
return $this->gate->inspect(
@@ -102,7 +102,7 @@ public function update(Request $request, object $model): bool|Response
102102
/**
103103
* @inheritDoc
104104
*/
105-
public function destroy(Request $request, object $model): bool|Response
105+
public function destroy(?Request $request, object $model): bool|Response
106106
{
107107
if ($this->mustAuthorize()) {
108108
return $this->gate->inspect(
@@ -117,7 +117,7 @@ public function destroy(Request $request, object $model): bool|Response
117117
/**
118118
* @inheritDoc
119119
*/
120-
public function showRelated(Request $request, object $model, string $fieldName): bool|Response
120+
public function showRelated(?Request $request, object $model, string $fieldName): bool|Response
121121
{
122122
if ($this->mustAuthorize()) {
123123
return $this->gate->inspect(
@@ -132,15 +132,15 @@ public function showRelated(Request $request, object $model, string $fieldName):
132132
/**
133133
* @inheritDoc
134134
*/
135-
public function showRelationship(Request $request, object $model, string $fieldName): bool|Response
135+
public function showRelationship(?Request $request, object $model, string $fieldName): bool|Response
136136
{
137137
return $this->showRelated($request, $model, $fieldName);
138138
}
139139

140140
/**
141141
* @inheritDoc
142142
*/
143-
public function updateRelationship(Request $request, object $model, string $fieldName): bool|Response
143+
public function updateRelationship(?Request $request, object $model, string $fieldName): bool|Response
144144
{
145145
if ($this->mustAuthorize()) {
146146
return $this->gate->inspect(
@@ -155,7 +155,7 @@ public function updateRelationship(Request $request, object $model, string $fiel
155155
/**
156156
* @inheritDoc
157157
*/
158-
public function attachRelationship(Request $request, object $model, string $fieldName): bool|Response
158+
public function attachRelationship(?Request $request, object $model, string $fieldName): bool|Response
159159
{
160160
if ($this->mustAuthorize()) {
161161
return $this->gate->inspect(
@@ -170,7 +170,7 @@ public function attachRelationship(Request $request, object $model, string $fiel
170170
/**
171171
* @inheritDoc
172172
*/
173-
public function detachRelationship(Request $request, object $model, string $fieldName): bool|Response
173+
public function detachRelationship(?Request $request, object $model, string $fieldName): bool|Response
174174
{
175175
if ($this->mustAuthorize()) {
176176
return $this->gate->inspect(
@@ -197,16 +197,16 @@ public function failed(): never
197197
/**
198198
* Create a lazy relation object.
199199
*
200-
* @param Request $request
200+
* @param Request|null $request
201201
* @param string $fieldName
202202
* @return LazyRelation
203203
*/
204-
private function createRelation(Request $request, string $fieldName): LazyRelation
204+
private function createRelation(?Request $request, string $fieldName): LazyRelation
205205
{
206206
return new LazyRelation(
207207
$this->service->server(),
208208
$this->schema()->relationship($fieldName),
209-
$request->json()->all()
209+
$request?->json()->all() ?? [],
210210
);
211211
}
212212

Diff for: src/Core/Auth/ResourceAuthorizer.php

+28-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace LaravelJsonApi\Core\Auth;
1313

1414
use Illuminate\Auth\Access\AuthorizationException;
15+
use Illuminate\Auth\Access\Response;
1516
use Illuminate\Auth\AuthenticationException;
1617
use Illuminate\Http\Request;
1718
use LaravelJsonApi\Contracts\Auth\Authorizer as AuthorizerContract;
@@ -44,7 +45,7 @@ public function index(?Request $request): ?ErrorList
4445
$this->modelClass,
4546
);
4647

47-
return $passes ? null : $this->failed();
48+
return $this->parse($passes);
4849
}
4950

5051
/**
@@ -67,7 +68,7 @@ public function store(?Request $request): ?ErrorList
6768
$this->modelClass,
6869
);
6970

70-
return $passes ? null : $this->failed();
71+
return $this->parse($passes);
7172
}
7273

7374
/**
@@ -90,7 +91,7 @@ public function show(?Request $request, object $model): ?ErrorList
9091
$model,
9192
);
9293

93-
return $passes ? null : $this->failed();
94+
return $this->parse($passes);
9495
}
9596

9697
/**
@@ -113,7 +114,7 @@ public function update(?Request $request, object $model): ?ErrorList
113114
$model,
114115
);
115116

116-
return $passes ? null : $this->failed();
117+
return $this->parse($passes);
117118
}
118119

119120
/**
@@ -136,7 +137,7 @@ public function destroy(?Request $request, object $model): ?ErrorList
136137
$model,
137138
);
138139

139-
return $passes ? null : $this->failed();
140+
return $this->parse($passes);
140141
}
141142

142143
/**
@@ -160,7 +161,7 @@ public function showRelated(?Request $request, object $model, string $fieldName)
160161
$fieldName,
161162
);
162163

163-
return $passes ? null : $this->failed();
164+
return $this->parse($passes);
164165
}
165166

166167
/**
@@ -184,7 +185,7 @@ public function showRelationship(?Request $request, object $model, string $field
184185
$fieldName,
185186
);
186187

187-
return $passes ? null : $this->failed();
188+
return $this->parse($passes);
188189
}
189190

190191
/**
@@ -208,7 +209,7 @@ public function updateRelationship(?Request $request, object $model, string $fie
208209
$fieldName,
209210
);
210211

211-
return $passes ? null : $this->failed();
212+
return $this->parse($passes);
212213
}
213214

214215
/**
@@ -232,7 +233,7 @@ public function attachRelationship(?Request $request, object $model, string $fie
232233
$fieldName,
233234
);
234235

235-
return $passes ? null : $this->failed();
236+
return $this->parse($passes);
236237
}
237238

238239
/**
@@ -256,7 +257,7 @@ public function detachRelationship(?Request $request, object $model, string $fie
256257
$fieldName,
257258
);
258259

259-
return $passes ? null : $this->failed();
260+
return $this->parse($passes);
260261
}
261262

262263
/**
@@ -269,6 +270,23 @@ public function detachRelationshipOrFail(?Request $request, object $model, strin
269270
}
270271
}
271272

273+
/**
274+
* @param bool|Response $result
275+
* @return ErrorList|null
276+
* @throws AuthenticationException
277+
* @throws AuthorizationException
278+
* @throws HttpExceptionInterface
279+
*/
280+
private function parse(bool|Response $result): ?ErrorList
281+
{
282+
if ($result instanceof Response) {
283+
$result->authorize();
284+
return null;
285+
}
286+
287+
return $result ? null : $this->failed();
288+
}
289+
272290
/**
273291
* @return ErrorList
274292
* @throws AuthorizationException

0 commit comments

Comments
 (0)