Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Adds Response authorization to Form Requests #38489

Merged
merged 6 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation\Http;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Access\Response;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
Expand Down Expand Up @@ -163,11 +164,15 @@ protected function getRedirectUrl()
* Determine if the request passes the authorization check.
*
* @return bool
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
protected function passesAuthorization()
{
if (method_exists($this, 'authorize')) {
return $this->container->call([$this, 'authorize']);
$result = $this->container->call([$this, 'authorize']);

return $result instanceof Response ? $result->authorize() : $result;
}

return true;
Expand Down
35 changes: 35 additions & 0 deletions tests/Foundation/FoundationFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Access\Response;
use Illuminate\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory as ValidationFactoryContract;
Expand Down Expand Up @@ -101,6 +102,19 @@ public function testValidateMethodThrowsWhenAuthorizationFails()
$this->createRequest([], FoundationTestFormRequestForbiddenStub::class)->validateResolved();
}

public function testValidateThrowsExceptionFromAuthorizationResponse()
{
$this->expectException(AuthorizationException::class);
$this->expectExceptionMessage('foo');

$this->createRequest([], FoundationTestFormRequestForbiddenWithResponseStub::class)->validateResolved();
}

public function testValidateDoesntThrowExceptionFromResponseAllowed()
{
$this->createRequest([], FoundationTestFormRequestPassesWithResponseStub::class)->validateResolved();
}

public function testPrepareForValidationRunsBeforeValidation()
{
$this->createRequest([], FoundationTestFormRequestHooks::class)->validateResolved();
Expand Down Expand Up @@ -322,3 +336,24 @@ public function passedValidation()
$this->replace(['name' => 'Adam']);
}
}

class FoundationTestFormRequestForbiddenWithResponseStub extends FormRequest
{
public function authorize()
{
return Response::deny('foo');
}
}

class FoundationTestFormRequestPassesWithResponseStub extends FormRequest
{
public function rules()
{
return [];
}

public function authorize()
{
return Response::allow('baz');
}
}