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

[11.X] Introduces UsePolicy attribute #54409

Open
wants to merge 3 commits into
base: 11.x
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/Illuminate/Auth/Access/Attributes/UsePolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Auth\Access\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class UsePolicy
{
/**
* Create a new attribute instance.
*
* @param class-string $policyClass
* @return void
*/
public function __construct(public string $policyClass)
{
}
}
29 changes: 29 additions & 0 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Exception;
use Illuminate\Auth\Access\Attributes\UsePolicy;
use Illuminate\Auth\Access\Events\GateEvaluated;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Contracts\Container\Container;
Expand Down Expand Up @@ -670,6 +671,10 @@ public function getPolicyFor($class)
return $this->resolvePolicy($this->policies[$class]);
}

if ($policy = $this->tryResolvePolicyViaUsePolicyAttribute($class)) {
return $policy;
}

foreach ($this->guessPolicyName($class) as $guessedPolicy) {
if (class_exists($guessedPolicy)) {
return $this->resolvePolicy($guessedPolicy);
Expand Down Expand Up @@ -708,6 +713,30 @@ protected function guessPolicyName($class)
}) ?: [$classDirname.'\\Policies\\'.class_basename($class).'Policy']);
}

/**
* Try to resolve the policy via the UsePolicy attribute.
*
* @param class-string $class
* @return mixed
*/
protected function tryResolvePolicyViaUsePolicyAttribute($class)
{
try {
$attributes = (new \ReflectionClass($class))
->getAttributes(UsePolicy::class);
} catch (Exception) {
return null;
}

if ($attributes !== []) {
$usePolicy = $attributes[0]->newInstance();

return $this->resolvePolicy($usePolicy->policyClass);
}

return null;
}

/**
* Specify a callback to be used to guess policy names.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Auth/AuthAccessGateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Auth;

use Illuminate\Auth\Access\Attributes\UsePolicy;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Access\Gate;
use Illuminate\Auth\Access\HandlesAuthorization;
Expand Down Expand Up @@ -612,6 +613,18 @@ public function testForUserMethodAttachesANewUserToANewGateInstance()
$this->assertTrue($gate->forUser((object) ['id' => 2])->check('foo'));
}

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

$policy = $gate->getPolicyFor(AccessGateTestDummyWithUsePolicyAttribute::class);

$this->assertTrue(
$policy instanceof AccessGateTestPolicy &&
get_class($policy) === AccessGateTestPolicy::class
);
}

public function testForUserMethodAttachesANewUserToANewGateInstanceWithGuessCallback()
{
$gate = $this->getBasicGate();
Expand Down Expand Up @@ -1342,6 +1355,12 @@ class AccessGateTestDummy implements AccessGateTestDummyInterface
//
}

#[UsePolicy(AccessGateTestPolicy::class)]
class AccessGateTestDummyWithUsePolicyAttribute implements AccessGateTestDummyInterface
{
//
}

class AccessGateTestSubDummy extends AccessGateTestDummy
{
//
Expand Down