Skip to content

Commit

Permalink
fix duplicates on sync (#2574)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Dec 9, 2023
1 parent b6b9f4f commit c24211a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,10 @@ private function collectPermissions(...$permissions): array
return $array;
}

$this->ensureModelSharesGuard($permission);

$array[] = $permission->getKey();
if (! in_array($permission->getKey(), $array)) {
$this->ensureModelSharesGuard($permission);
$array[] = $permission->getKey();
}

return $array;
}, []);
Expand Down
7 changes: 4 additions & 3 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ private function collectRoles(...$roles): array
return $array;
}

$this->ensureModelSharesGuard($role);

$array[] = $role->getKey();
if (! in_array($role->getKey(), $array)) {
$this->ensureModelSharesGuard($role);
$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 c24211a

Please sign in to comment.