Skip to content

Auto-discover nested policies following conventional, parallel hierarchy #54493

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 3 commits into from
Feb 6, 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
3 changes: 3 additions & 0 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ protected function guessPolicyName($class)
$classDirname = implode('\\', array_slice($classDirnameSegments, 0, $index));

return $classDirname.'\\Policies\\'.class_basename($class).'Policy';
})->when(str_contains($classDirname, '\\Models\\'), function ($collection) use ($class, $classDirname) {
return $collection->concat([str_replace('\\Models\\', '\\Policies\\', $classDirname).'\\'.class_basename($class).'Policy'])
->concat([str_replace('\\Models\\', '\\Models\\Policies\\', $classDirname).'\\'.class_basename($class).'Policy']);
})->reverse()->values()->first(function ($class) {
return class_exists($class);
}) ?: [$classDirname.'\\Policies\\'.class_basename($class).'Policy']);
Expand Down
27 changes: 27 additions & 0 deletions tests/Integration/Auth/Fixtures/Models/Nested/SubTestUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Illuminate\Tests\Integration\Auth\Fixtures\Models\Nested;

use Illuminate\Foundation\Auth\User as Authenticatable;

class SubTestUser extends Authenticatable
{
public $table = 'users';
public $timestamps = false;

/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = [];

/**
* The attributes that should be hidden for arrays.
*
* @var string[]
*/
protected $hidden = [
'password', 'remember_token',
];
}
27 changes: 27 additions & 0 deletions tests/Integration/Auth/Fixtures/Models/Nested/TopTestUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Illuminate\Tests\Integration\Auth\Fixtures\Models\Nested;

use Illuminate\Foundation\Auth\User as Authenticatable;

class TopTestUser extends Authenticatable
{
public $table = 'users';
public $timestamps = false;

/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = [];

/**
* The attributes that should be hidden for arrays.
*
* @var string[]
*/
protected $hidden = [
'password', 'remember_token',
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Tests\Integration\Auth\Fixtures\Models\Policies\Nested;

class SubTestUserPolicy
{
//
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Tests\Integration\Auth\Fixtures\Policies\Nested;

class TopTestUserPolicy
{
//
}
15 changes: 15 additions & 0 deletions tests/Integration/Auth/GatePolicyResolutionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
use Illuminate\Tests\Integration\Auth\Fixtures\Models\Policies\Nested\SubTestUserPolicy;
use Illuminate\Tests\Integration\Auth\Fixtures\Policies\AuthenticationTestUserPolicy;
use Illuminate\Tests\Integration\Auth\Fixtures\Policies\Nested\TopTestUserPolicy;
use Orchestra\Testbench\TestCase;

class GatePolicyResolutionTest extends TestCase
Expand Down Expand Up @@ -37,6 +39,19 @@ public function testPolicyCanBeGuessedUsingClassConventions()
);
}

public function testPolicyCanBeGuessedForParallelClassHierarchies()
{
$this->assertInstanceOf(
TopTestUserPolicy::class,
Gate::getPolicyFor(Fixtures\Models\Nested\TopTestUser::class)
);

$this->assertInstanceOf(
SubTestUserPolicy::class,
Gate::getPolicyFor(Fixtures\Models\Nested\SubTestUser::class)
);
}

public function testPolicyCanBeGuessedUsingCallback()
{
Gate::guessPolicyNamesUsing(function () {
Expand Down