Skip to content

Commit

Permalink
Fix #2771
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Dec 13, 2024
1 parent 0c5789d commit 30649f5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public function hasPermissionTo($permission, ?string $guardName = null): bool
throw GuardDoesNotMatch::create($permission->guard_name, $guardName ? collect([$guardName]) : $this->getGuardNames());
}

return $this->permissions->contains($permission->getKeyName(), $permission->getKey());
return $this->loadMissing('permissions')->permissions
->contains($permission->getKeyName(), $permission->getKey());
}
}
3 changes: 2 additions & 1 deletion src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ public function hasDirectPermission($permission): bool
{
$permission = $this->filterPermission($permission);

return $this->permissions->contains($permission->getKeyName(), $permission->getKey());
return $this->loadMissing('permissions')->permissions
->contains($permission->getKeyName(), $permission->getKey());
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,33 @@ public function it_can_reject_permission_based_on_logged_in_user_guard()
'status' => false,
]);
}

/** @test */
public function it_can_be_given_a_permission_on_role_when_lazy_loading_is_restricted()
{
try {
$testRole = app(Role::class)->with('permissions')->get()->first();

$testRole->givePermissionTo('edit-articles');

$this->assertTrue($testRole->hasPermissionTo('edit-articles'));
} catch (Exception $e) {
$this->fail('Lazy loading detected in the givePermissionTo method: ' . $e->getMessage());
}
}

/** @test */
public function it_can_be_given_a_permission_on_user_when_lazy_loading_is_restricted()
{
try {
User::create(['email' => 'other@user.com']);
$testUser = User::with('permissions')->get()->first();

$testUser->givePermissionTo('edit-articles');

$this->assertTrue($testUser->hasPermissionTo('edit-articles'));
} catch (Exception $e) {
$this->fail('Lazy loading detected in the givePermissionTo method: ' . $e->getMessage());
}
}
}
30 changes: 30 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Permission\Tests;

use Illuminate\Support\Facades\DB;
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
use Spatie\Permission\Exceptions\RoleDoesNotExist;
Expand Down Expand Up @@ -856,4 +857,33 @@ public function it_does_not_detach_roles_when_user_soft_deleting()

$this->assertTrue($user->hasRole('testRole'));
}

/** @test */
public function it_can_be_given_a_role_on_permission_when_lazy_loading_is_restricted()
{
try {
$testPermission = app(Permission::class)->with('roles')->get()->first();

$testPermission->assignRole('testRole');

$this->assertTrue($testPermission->hasRole('testRole'));
} catch (Exception $e) {
$this->fail('Lazy loading detected in the givePermissionTo method: ' . $e->getMessage());
}
}

/** @test */
public function it_can_be_given_a_role_on_user_when_lazy_loading_is_restricted()
{
try {
User::create(['email' => 'other@user.com']);
$user = User::with('roles')->get()->first();
$user->assignRole('testRole');

$this->assertTrue($user->hasRole('testRole'));
} catch (Exception $e) {
$this->fail('Lazy loading detected in the givePermissionTo method: ' . $e->getMessage());
}
}

}

0 comments on commit 30649f5

Please sign in to comment.