|
11 | 11 |
|
12 | 12 | namespace LaravelJsonApi\Laravel\Http\Requests;
|
13 | 13 |
|
| 14 | +use Illuminate\Auth\Access\AuthorizationException; |
| 15 | +use Illuminate\Auth\Access\Response; |
14 | 16 | use Illuminate\Auth\AuthenticationException;
|
15 | 17 | use Illuminate\Contracts\Auth\Guard;
|
16 | 18 | use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
|
@@ -226,42 +228,54 @@ public function schema(): Schema
|
226 | 228 | */
|
227 | 229 | protected function passesAuthorization()
|
228 | 230 | {
|
229 |
| - /** |
230 |
| - * If the developer has implemented the `authorize` method, we |
231 |
| - * will return the result if it is a boolean. This allows |
232 |
| - * the developer to return a null value to indicate they want |
233 |
| - * the default authorization to run. |
234 |
| - */ |
235 |
| - if (method_exists($this, 'authorize')) { |
236 |
| - if (is_bool($passes = $this->container->call([$this, 'authorize']))) { |
237 |
| - return $passes; |
| 231 | + try { |
| 232 | + /** |
| 233 | + * If the developer has implemented the `authorize` method, we |
| 234 | + * will return the result if it is a boolean. This allows |
| 235 | + * the developer to return a null value to indicate they want |
| 236 | + * the default authorization to run. |
| 237 | + */ |
| 238 | + if (method_exists($this, 'authorize')) { |
| 239 | + $result = $this->container->call([$this, 'authorize']); |
| 240 | + if ($result !== null) { |
| 241 | + return $result instanceof Response ? $result->authorize() : $result; |
| 242 | + } |
238 | 243 | }
|
239 |
| - } |
240 | 244 |
|
241 |
| - /** |
242 |
| - * If the developer has not authorized the request themselves, |
243 |
| - * we run our default authorization as long as authorization is |
244 |
| - * enabled for both the server and the schema (checked via the |
245 |
| - * `mustAuthorize()` method). |
246 |
| - */ |
247 |
| - if (method_exists($this, 'authorizeResource')) { |
248 |
| - return $this->container->call([$this, 'authorizeResource']); |
249 |
| - } |
| 245 | + /** |
| 246 | + * If the developer has not authorized the request themselves, |
| 247 | + * we run our default authorization as long as authorization is |
| 248 | + * enabled for both the server and the schema (checked via the |
| 249 | + * `mustAuthorize()` method). |
| 250 | + */ |
| 251 | + if (method_exists($this, 'authorizeResource')) { |
| 252 | + $result = $this->container->call([$this, 'authorizeResource']); |
| 253 | + return $result instanceof Response ? $result->authorize() : $result; |
| 254 | + } |
250 | 255 |
|
| 256 | + } catch (AuthorizationException $ex) { |
| 257 | + $this->failIfUnauthenticated(); |
| 258 | + throw $ex; |
| 259 | + } |
251 | 260 | return true;
|
252 | 261 | }
|
253 | 262 |
|
254 |
| - /** |
255 |
| - * @inheritDoc |
256 |
| - */ |
257 |
| - protected function failedAuthorization() |
| 263 | + protected function failIfUnauthenticated() |
258 | 264 | {
|
259 |
| - /** @var Guard $auth */ |
| 265 | + /** @var Guard $auth */ |
260 | 266 | $auth = $this->container->make(Guard::class);
|
261 | 267 |
|
262 | 268 | if ($auth->guest()) {
|
263 | 269 | throw new AuthenticationException();
|
264 | 270 | }
|
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * @inheritDoc |
| 275 | + */ |
| 276 | + protected function failedAuthorization() |
| 277 | + { |
| 278 | + $this->failIfUnauthenticated(); |
265 | 279 |
|
266 | 280 | parent::failedAuthorization();
|
267 | 281 | }
|
|
0 commit comments