diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php index 2618b380..3ec39df9 100644 --- a/src/Traits/HasPermissions.php +++ b/src/Traits/HasPermissions.php @@ -372,7 +372,9 @@ private function collectPermissions(...$permissions): array $this->ensureModelSharesGuard($permission); - $array[] = $permission->getKey(); + if (! in_array($permission->getKey(), $array)) { + $array[] = $permission->getKey(); + } return $array; }, []); diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index f02f7c16..11c26f1f 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -131,7 +131,9 @@ private function collectRoles(...$roles): array $this->ensureModelSharesGuard($role); - $array[] = $role->getKey(); + if (! in_array($role->getKey(), $array)) { + $array[] = $role->getKey(); + } return $array; }, []); diff --git a/tests/HasPermissionsTest.php b/tests/HasPermissionsTest.php index 0eeb961a..bc7b84f9 100644 --- a/tests/HasPermissionsTest.php +++ b/tests/HasPermissionsTest.php @@ -541,6 +541,16 @@ public function it_can_sync_multiple_permissions() $this->assertFalse($this->testUser->hasDirectPermission('edit-news')); } + /** @test */ + public function it_can_avoid_sync_duplicated_permissions() + { + $this->testUser->syncPermissions('edit-articles', 'edit-blog', 'edit-blog'); + + $this->assertTrue($this->testUser->hasDirectPermission('edit-articles')); + + $this->assertTrue($this->testUser->hasDirectPermission('edit-blog')); + } + /** @test */ public function it_can_sync_multiple_permissions_by_id() { diff --git a/tests/HasRolesTest.php b/tests/HasRolesTest.php index ec7bccc7..7b0e54e3 100644 --- a/tests/HasRolesTest.php +++ b/tests/HasRolesTest.php @@ -259,6 +259,16 @@ public function it_can_sync_roles_from_a_string_on_a_permission() $this->assertTrue($this->testUserPermission->hasRole('testRole2')); } + /** @test */ + public function it_can_avoid_sync_duplicated_roles() + { + $this->testUser->syncRoles('testRole', 'testRole', 'testRole2'); + + $this->assertTrue($this->testUser->hasRole('testRole')); + + $this->assertTrue($this->testUser->hasRole('testRole2')); + } + /** @test */ public function it_can_sync_multiple_roles() {