Skip to content

Commit

Permalink
Form Request should throw AuthorizationException.
Browse files Browse the repository at this point in the history
Form requests should throw the AuthorizationException when
authorization fails to be more consistent with the exception thrown by
the Gate class. The response / exception can still be overridden by
redefining the method.
  • Loading branch information
taylorotwell committed Dec 16, 2016
1 parent 46f805b commit 1a75409
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
16 changes: 3 additions & 13 deletions src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Exception\HttpResponseException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Validation\ValidatesWhenResolvedTrait;
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
Expand Down Expand Up @@ -136,11 +136,11 @@ protected function passesAuthorization()
*
* @return void
*
* @throws \Illuminate\Http\Exception\HttpResponseException
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
protected function failedAuthorization()
{
throw new HttpResponseException($this->forbiddenResponse());
throw new AuthorizationException('This action is unauthorized.');
}

/**
Expand All @@ -160,16 +160,6 @@ public function response(array $errors)
->withErrors($errors, $this->errorBag);
}

/**
* Get the response for a forbidden operation.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function forbiddenResponse()
{
return new Response('Forbidden', 403);
}

/**
* Format the errors from the given Validator instance.
*
Expand Down
3 changes: 1 addition & 2 deletions tests/Foundation/FoundationFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testValidateFunctionThrowsValidationExceptionIfValidationFails()
}

/**
* @expectedException \Illuminate\Http\Exception\HttpResponseException
* @expectedException \Illuminate\Auth\Access\AuthorizationException
*/
public function testValidateFunctionThrowsHttpResponseExceptionIfAuthorizationFails()
{
Expand All @@ -59,7 +59,6 @@ public function testValidateFunctionThrowsHttpResponseExceptionIfAuthorizationFa
);
$container->instance('Illuminate\Contracts\Validation\Factory', $factory);
$validator->shouldReceive('passes')->never();
$request->shouldReceive('forbiddenResponse')->once()->andReturn(new Illuminate\Http\Response);

$request->validate($factory);
}
Expand Down

0 comments on commit 1a75409

Please sign in to comment.