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

Gate ability registration #45807

Closed
wants to merge 4 commits into from
Closed
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
29 changes: 29 additions & 0 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,35 @@ public function resource($name, $class, array $abilities = null)
return $this;
}

/**
* Register abilities from a class.
*
* @param string $name
* @param string $class
* @return $this
*
* @throws \ReflectionException
*/
public function register($name, $class)
{
$reflection = new ReflectionClass($class);

$traitMethods = (new Collection($reflection->getTraits()))
->flatMap(fn ($item) => $item->getMethods())
->map(fn ($item) => $item->name);

$abilities = (new Collection($reflection->getMethods()))
->reject(fn ($item) => $item->isConstructor())
->reject(fn ($item) => $item->getDeclaringClass()->name !== $class)
->reject(fn ($item) => in_array($item->name, $traitMethods->toArray()));

foreach ($abilities as $ability) {
$this->define($name.'.'.$ability->name, $class.'@'.$ability->name);
}

return $this;
}

/**
* Create the ability callback for a callback string.
*
Expand Down
67 changes: 67 additions & 0 deletions tests/Auth/AuthAccessGateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ public function testCustomResourceGatesCanBeDefined()
$this->assertTrue($gate->check('test.ability2'));
}

public function testClassGatesCanBeRegistered()
{
$gate = $this->getBasicGate();

$gate->register('test', AccessGateTestAutoRegistersMethods::class);

$this->assertTrue($gate->has('test.gate1'));
$this->assertTrue($gate->has('test.gate2'));
$this->assertFalse($gate->has('test.__construct'));
$this->assertFalse($gate->has('test.parent1'));
$this->assertFalse($gate->has('test.parent2'));
$this->assertFalse($gate->has('test.trait1'));
$this->assertFalse($gate->has('test.trait2'));
$this->assertFalse($gate->has('test.trait3'));
$this->assertFalse($gate->has('test.trait4'));
}

public function testBeforeCallbacksCanOverrideResultIfNecessary()
{
$gate = $this->getBasicGate();
Expand Down Expand Up @@ -1396,3 +1413,53 @@ public function create()
throw new AuthorizationException('Not allowed.', 'some_code');
}
}

class AccessGateTestAutoRegistersMethodsParent
{
public function parent1()
{
}

public function parent2()
{
}
}

trait AccessGateTestAutoRegistersMethodsTraitOne
{
public function trait1()
{
}

public function trait2()
{
}
}

trait AccessGateTestAutoRegistersMethodsTraitTwo
{
public function trait3()
{
}

public function trait4()
{
}
}

class AccessGateTestAutoRegistersMethods extends AccessGateTestAutoRegistersMethodsParent
{
use AccessGateTestAutoRegistersMethodsTraitOne, AccessGateTestAutoRegistersMethodsTraitTwo;

public function __construct()
{
}

public function gate1()
{
}

public function gate2()
{
}
}