Skip to content

Commit

Permalink
Merge pull request #77 from httpoz/prevent-duplicates
Browse files Browse the repository at this point in the history
Prevent duplicates
  • Loading branch information
httpoz authored Apr 23, 2021
2 parents c944bb4 + fb0f653 commit 0edbbca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/Contracts/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace HttpOz\Roles\Contracts;

use HttpOz\Roles\Models\Role;

interface HasRole
{
/**
Expand Down Expand Up @@ -54,23 +56,23 @@ public function hasRole($role);
/**
* Attach role to a user.
*
* @param int|\HttpOz\Roles\Models\Role $role
* @return null|bool
* @param int|Role $role
* @return bool
*/
public function attachRole($role);
public function attachRole(int|Role $role): bool;

/**
* Detach role from a user.
*
* @param int|\HttpOz\Roles\Models\Role $role
* @param int|Role $role
* @return int
*/
public function detachRole($role);

/**
* Sync roles for a user.
*
* @param array|\HttpOz\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles
* @param array|Role[]|\Illuminate\Database\Eloquent\Collection $roles
* @return array
*/
public function syncRoles($roles);
Expand Down
24 changes: 13 additions & 11 deletions src/Traits/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace HttpOz\Roles\Traits;

use HttpOz\Roles\Models\Role;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Cache;

Expand Down Expand Up @@ -109,15 +110,16 @@ public function hasRole( $role ) {
}
}

/**
* Attach role to a user.
*
* @param int|\HttpOz\Roles\Models\Role $role
*
* @return bool
*/
public function attachRole( $role ) {
if ( ! collect( $this->getRoles() )->contains( $role ) ) {
/**
* Attach role to a user.
*
* @param int|Role $role
*
* @return bool
*/
public function attachRole(int|Role $role): bool
{
if ( ! $this->getRoles()->contains( $role ) ) {
$this->clearCached();
$this->roles()->attach( $role );
}
Expand All @@ -128,7 +130,7 @@ public function attachRole( $role ) {
/**
* Detach role from a user.
*
* @param int|\HttpOz\Roles\Models\Role $role
* @param int|Role $role
*
* @return int
*/
Expand All @@ -153,7 +155,7 @@ public function detachAllRoles() {
/**
* Sync roles for a user.
*
* @param array|\HttpOz\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles
* @param array|Role[]|\Illuminate\Database\Eloquent\Collection $roles
*
* @return array
*/
Expand Down
12 changes: 11 additions & 1 deletion tests/Feature/UserRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ public function testUserHasRoleOnAttach()
$role = Role::factory()->create();

$adminRole = Role::findBySlug('admin');
$user = User::find(1);
$admin->detachAllRoles();
$admin->attachRole($adminRole);

$this->assertEquals($role->slug, $adminRole->slug);
}

public function testUserHasOneRoleOnAttachDuplicate()
{
$admin = User::factory()->create();
$role = Role::factory()->create();

$admin->attachRole($role);
$admin->attachRole($role);

$this->assertCount(1, $admin->roles);
}
}

0 comments on commit 0edbbca

Please sign in to comment.