Skip to content

Commit

Permalink
fix duplicates on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Dec 7, 2023
1 parent b6b9f4f commit af544ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, []);
Expand Down
4 changes: 3 additions & 1 deletion src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, []);
Expand Down
10 changes: 10 additions & 0 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
10 changes: 10 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit af544ac

Please sign in to comment.