Skip to content

Commit b5300a1

Browse files
[8.x] Adds Response authorization to Form Requests (#38489)
* Adds Response authorization to Form Requests. * Style changes * Removes string check to denying responses. * Fixes tests by removing string check. * Removed string authorization to denying response.
1 parent 30e1fd3 commit b5300a1

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Illuminate/Foundation/Http/FormRequest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Foundation\Http;
44

55
use Illuminate\Auth\Access\AuthorizationException;
6+
use Illuminate\Auth\Access\Response;
67
use Illuminate\Contracts\Container\Container;
78
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
89
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
@@ -163,11 +164,15 @@ protected function getRedirectUrl()
163164
* Determine if the request passes the authorization check.
164165
*
165166
* @return bool
167+
*
168+
* @throws \Illuminate\Auth\Access\AuthorizationException
166169
*/
167170
protected function passesAuthorization()
168171
{
169172
if (method_exists($this, 'authorize')) {
170-
return $this->container->call([$this, 'authorize']);
173+
$result = $this->container->call([$this, 'authorize']);
174+
175+
return $result instanceof Response ? $result->authorize() : $result;
171176
}
172177

173178
return true;

tests/Foundation/FoundationFormRequestTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Illuminate\Auth\Access\AuthorizationException;
7+
use Illuminate\Auth\Access\Response;
78
use Illuminate\Container\Container;
89
use Illuminate\Contracts\Translation\Translator;
910
use Illuminate\Contracts\Validation\Factory as ValidationFactoryContract;
@@ -101,6 +102,19 @@ public function testValidateMethodThrowsWhenAuthorizationFails()
101102
$this->createRequest([], FoundationTestFormRequestForbiddenStub::class)->validateResolved();
102103
}
103104

105+
public function testValidateThrowsExceptionFromAuthorizationResponse()
106+
{
107+
$this->expectException(AuthorizationException::class);
108+
$this->expectExceptionMessage('foo');
109+
110+
$this->createRequest([], FoundationTestFormRequestForbiddenWithResponseStub::class)->validateResolved();
111+
}
112+
113+
public function testValidateDoesntThrowExceptionFromResponseAllowed()
114+
{
115+
$this->createRequest([], FoundationTestFormRequestPassesWithResponseStub::class)->validateResolved();
116+
}
117+
104118
public function testPrepareForValidationRunsBeforeValidation()
105119
{
106120
$this->createRequest([], FoundationTestFormRequestHooks::class)->validateResolved();
@@ -322,3 +336,24 @@ public function passedValidation()
322336
$this->replace(['name' => 'Adam']);
323337
}
324338
}
339+
340+
class FoundationTestFormRequestForbiddenWithResponseStub extends FormRequest
341+
{
342+
public function authorize()
343+
{
344+
return Response::deny('foo');
345+
}
346+
}
347+
348+
class FoundationTestFormRequestPassesWithResponseStub extends FormRequest
349+
{
350+
public function rules()
351+
{
352+
return [];
353+
}
354+
355+
public function authorize()
356+
{
357+
return Response::allow('baz');
358+
}
359+
}

0 commit comments

Comments
 (0)