Skip to content

Commit

Permalink
Allow array callables to be passed to Gate::before()
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Sep 28, 2018
1 parent 59a0453 commit b56c91e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public function raw($ability, $arguments = [])
* Determine whether the callback/method can be called with the given user.
*
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
* @param \Closure|string $class
* @param \Closure|string|array $class
* @param string|null $method
* @return bool
*/
Expand All @@ -346,6 +346,15 @@ protected function canBeCalledWithUser($user, $class, $method = null)
return $this->methodAllowsGuests($class, $method);
}

// If the "class" is actually a callable array, it may be either
// two strings (when using a static method), or it could be a
// concrete instance of an object, plus the method's name.
if (is_array($class)) {
$className = is_string($class[0]) ? $class[0] : get_class($class[0]);

return $this->methodAllowsGuests($className, $class[1]);
}

return $this->callbackAllowsGuests($class);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Auth/AuthAccessGateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ public function test_basic_closures_can_be_defined()
$this->assertFalse($gate->check('bar'));
}

public function test_before_can_take_an_array_callback()
{
$gate = new Gate(new Container, function () {
return null;
});

$gate->before([new AccessGateTestBeforeCallback, 'allowEverything']);

$this->assertTrue($gate->check('anything'));
}

public function test_before_can_take_an_array_callback_with_static_method()
{
$gate = new Gate(new Container, function () {
return null;
});

$gate->before([AccessGateTestBeforeCallback::class, 'allowEverything']);

$this->assertTrue($gate->check('anything'));
}

public function test_before_can_allow_guests()
{
$gate = new Gate(new Container, function () {
Expand Down Expand Up @@ -799,3 +821,16 @@ public function update($user, AccessGateTestDummy $dummy)
return true;
}
}

class AccessGateTestBeforeCallback
{
public static function allowEverything($user = null)
{
return true;
}

public static function allowEverythingStatically($user = null)
{
return true;
}
}

0 comments on commit b56c91e

Please sign in to comment.