Skip to content

Commit adc2dc4

Browse files
[10.x] Can set custom Response for denial within Gate@inspect() (#47436)
* can set denial response on Gate * fix docblocks for __construct * formatting * Update Gate.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent dda3f75 commit adc2dc4

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/Illuminate/Auth/Access/Gate.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ class Gate implements GateContract
6868
*/
6969
protected $stringCallbacks = [];
7070

71+
/**
72+
* The default denial response for gates and policies.
73+
*
74+
* @var Illuminate\Auth\Access\Response|null
75+
*/
76+
protected $defaultDenialResponse;
77+
7178
/**
7279
* The callback to be used to guess policy names.
7380
*
@@ -87,9 +94,13 @@ class Gate implements GateContract
8794
* @param callable|null $guessPolicyNamesUsingCallback
8895
* @return void
8996
*/
90-
public function __construct(Container $container, callable $userResolver, array $abilities = [],
91-
array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [],
92-
callable $guessPolicyNamesUsingCallback = null)
97+
public function __construct(Container $container,
98+
callable $userResolver,
99+
array $abilities = [],
100+
array $policies = [],
101+
array $beforeCallbacks = [],
102+
array $afterCallbacks = [],
103+
callable $guessPolicyNamesUsingCallback = null)
93104
{
94105
$this->policies = $policies;
95106
$this->container = $container;
@@ -398,7 +409,9 @@ public function inspect($ability, $arguments = [])
398409
return $result;
399410
}
400411

401-
return $result ? Response::allow() : Response::deny();
412+
return $result
413+
? Response::allow()
414+
: ($this->defaultDenialResponse ?? Response::deny());
402415
} catch (AuthorizationException $e) {
403416
return $e->toResponse();
404417
}
@@ -857,6 +870,19 @@ public function policies()
857870
return $this->policies;
858871
}
859872

873+
/**
874+
* Set the default denial response for gates and policies.
875+
*
876+
* @param \Illuminate\Auth\Access\Response $response
877+
* @return $this
878+
*/
879+
public function defaultDenialResponse(Response $response)
880+
{
881+
$this->defaultDenialResponse = $response;
882+
883+
return $this;
884+
}
885+
860886
/**
861887
* Set the container instance used by the gate.
862888
*

tests/Auth/AuthAccessGateTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,42 @@ public function testClassesCanBeDefinedAsCallbacksUsingAtNotationForGuests()
11091109

11101110
$this->assertFalse($gate->check('absent_invokable'));
11111111
}
1112+
1113+
public function testCanSetDenialResponseInConstructor()
1114+
{
1115+
$gate = new Gate(container: new Container, userResolver: function () {
1116+
//
1117+
});
1118+
1119+
$gate->defaultDenialResponse(Response::denyWithStatus(999, 'my_message', 'abc'));
1120+
1121+
$gate->define('foo', function() { return false; });
1122+
1123+
$response = $gate->inspect('foo', new AccessGateTestDummy);
1124+
1125+
$this->assertTrue($response->denied());
1126+
$this->assertFalse($response->allowed());
1127+
$this->assertSame('my_message', $response->message());
1128+
$this->assertSame('abc', $response->code());
1129+
$this->assertSame(999, $response->status());
1130+
}
1131+
1132+
public function testCanSetDenialResponse()
1133+
{
1134+
$gate = new Gate(container: new Container, userResolver: function () {
1135+
//
1136+
});
1137+
1138+
$gate->define('foo', function() { return false; });
1139+
$gate->defaultDenialResponse(Response::denyWithStatus(404, 'not_found', 'xyz'));
1140+
1141+
$response = $gate->inspect('foo', new AccessGateTestDummy);
1142+
$this->assertTrue($response->denied());
1143+
$this->assertFalse($response->allowed());
1144+
$this->assertSame('not_found', $response->message());
1145+
$this->assertSame('xyz', $response->code());
1146+
$this->assertSame(404, $response->status());
1147+
}
11121148
}
11131149

11141150
class AccessGateTestClassForGuest

0 commit comments

Comments
 (0)