diff --git a/src/Models/Role.php b/src/Models/Role.php index 6bd47b75..5bab4878 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -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()); } } diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php index cc69d096..4293b5c6 100644 --- a/src/Traits/HasPermissions.php +++ b/src/Traits/HasPermissions.php @@ -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()); } /** diff --git a/tests/HasPermissionsTest.php b/tests/HasPermissionsTest.php index ce197b16..044eac6f 100644 --- a/tests/HasPermissionsTest.php +++ b/tests/HasPermissionsTest.php @@ -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()); + } + } } diff --git a/tests/HasRolesTest.php b/tests/HasRolesTest.php index 93ee3574..22cd45f7 100644 --- a/tests/HasRolesTest.php +++ b/tests/HasRolesTest.php @@ -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; @@ -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()); + } + } + }