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

[5.8] Improvements regarding arguments passed to the Gate callbacks #28079

Merged
merged 2 commits into from
Apr 1, 2019
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
4 changes: 1 addition & 3 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,12 @@ protected function callAuthCallback($user, $ability, array $arguments)
*/
protected function callBeforeCallbacks($user, $ability, array $arguments)
{
$arguments = array_merge([$user, $ability], [$arguments]);

foreach ($this->beforeCallbacks as $before) {
if (! $this->canBeCalledWithUser($user, $before)) {
continue;
}

if (! is_null($result = $before(...$arguments))) {
if (! is_null($result = $before($user, $ability, $arguments))) {
return $result;
}
}
Expand Down
26 changes: 23 additions & 3 deletions tests/Auth/AuthAccessGateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,22 @@ public function test_a_single_argument_can_be_passed_when_checking_abilities()

$dummy = new AccessGateTestDummy;

$gate->before(function ($user, $ability, array $arguments) use ($dummy) {
$this->assertCount(1, $arguments);
$this->assertSame($dummy, $arguments[0]);
});

$gate->define('foo', function ($user, $x) use ($dummy) {
$this->assertEquals($dummy, $x);
$this->assertSame($dummy, $x);

return true;
});

$gate->after(function ($user, $ability, $result, array $arguments) use ($dummy) {
$this->assertCount(1, $arguments);
$this->assertSame($dummy, $arguments[0]);
});

$this->assertTrue($gate->check('foo', $dummy));
}

Expand All @@ -358,13 +368,23 @@ public function test_multiple_arguments_can_be_passed_when_checking_abilities()
$dummy1 = new AccessGateTestDummy;
$dummy2 = new AccessGateTestDummy;

$gate->before(function ($user, $ability, array $arguments) use ($dummy1, $dummy2) {
$this->assertCount(2, $arguments);
$this->assertSame([$dummy1, $dummy2], $arguments);
});

$gate->define('foo', function ($user, $x, $y) use ($dummy1, $dummy2) {
$this->assertEquals($dummy1, $x);
$this->assertEquals($dummy2, $y);
$this->assertSame($dummy1, $x);
$this->assertSame($dummy2, $y);

return true;
});

$gate->after(function ($user, $ability, $result, array $arguments) use ($dummy1, $dummy2) {
$this->assertCount(2, $arguments);
$this->assertSame([$dummy1, $dummy2], $arguments);
});

$this->assertTrue($gate->check('foo', [$dummy1, $dummy2]));
}

Expand Down