Skip to content

[12.x] feat: create UsePolicy attribute #55882

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

Merged
merged 2 commits into from
Jun 4, 2025
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
26 changes: 26 additions & 0 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Attributes\UsePolicy;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -669,6 +670,12 @@ public function getPolicyFor($class)
return $this->resolvePolicy($this->policies[$class]);
}

$policy = $this->getPolicyFromAttribute($class);

if (! is_null($policy)) {
return $this->resolvePolicy($policy);
}

foreach ($this->guessPolicyName($class) as $guessedPolicy) {
if (class_exists($guessedPolicy)) {
return $this->resolvePolicy($guessedPolicy);
Expand All @@ -682,6 +689,25 @@ public function getPolicyFor($class)
}
}

/**
* Get the policy class from the class attribute.
*
* @param class-string<*> $class
* @return class-string<*>|null
*/
protected function getPolicyFromAttribute(string $class): ?string
{
if (! class_exists($class)) {
return null;
}

$attributes = (new ReflectionClass($class))->getAttributes(UsePolicy::class);

return $attributes !== []
? $attributes[0]->newInstance()->class
: null;
}

/**
* Guess the policy name for the given class.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Eloquent/Attributes/UsePolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class UsePolicy
{
/**
* Create a new attribute instance.
*
* @param class-string<*> $class
*/
public function __construct(public string $class)
{
}
}
18 changes: 18 additions & 0 deletions tests/Integration/Auth/GatePolicyResolutionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Integration\Auth;

use Illuminate\Auth\Access\Events\GateEvaluated;
use Illuminate\Database\Eloquent\Attributes\UsePolicy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
Expand Down Expand Up @@ -78,4 +80,20 @@ public function testPolicyCanBeGuessedMultipleTimes()
Gate::getPolicyFor(AuthenticationTestUser::class)
);
}

public function testPolicyCanBeGivenByAttribute(): void
{
Gate::guessPolicyNamesUsing(fn () => [AuthenticationTestUserPolicy::class]);

$this->assertInstanceOf(PostPolicy::class, Gate::getPolicyFor(Post::class));
}
}

#[UsePolicy(PostPolicy::class)]
class Post extends Model
{
}

class PostPolicy
{
}