Skip to content

Commit

Permalink
Add a "whereIsNot" query scope to the HasRoles trait
Browse files Browse the repository at this point in the history
  • Loading branch information
soltanipoor authored and JosephSilber committed Aug 3, 2017
1 parent fb1e4ea commit 4f9ee53
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Database/Concerns/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ public function scopeWhereIsAll($query, $role)
);
}

/**
* Constrain the given query by the provided role.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $role
* @return void
*/
public function scopeWhereIsNot($query, $role)
{
call_user_func_array(
[new RolesQuery, 'constrainWhereIsNot'],
func_get_args()
);
}

/**
* Get an instance of the bouncer's clipboard.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Database/Queries/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function constrainWhereIs($query, $role)
});
}


/**
* Constrain the given query by all provided roles.
*
Expand All @@ -39,6 +40,22 @@ public function constrainWhereIsAll($query, $role)
}, '=', count($roles));
}

/**
* Constrain the given query by the provided role.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $role
* @return \Illuminate\Database\Eloquent\Builder
*/
public function constrainWhereIsNot($query, $role)
{
$roles = array_slice(func_get_args(), 1);

return $query->whereHas('roles', function ($query) use ($roles) {
$query->whereIn('name', $roles);
}, '<', 1);
}

/**
* Constrain the given roles query to those that were assigned to the given authorities.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Constraints/UserIsConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,40 @@ public function test_users_can_be_constrained_to_having_all_provided_roles()
$this->assertCount(1, $users);
$this->assertEquals('Joseph', $users->first()->name);
}

public function test_users_can_be_constrained_to_not_having_a_role()
{
$user1 = User::create();
$user2 = User::create();
$user3 = User::create();

$user1->assign('admin');
$user2->assign('editor');
$user3->assign('subscriber');

$users = User::whereIsNot('admin')->get();

$this->assertCount(2, $users);
$this->assertFalse($users->contains($user1));
$this->assertTrue($users->contains($user2));
$this->assertTrue($users->contains($user3));
}

public function test_users_can_be_constrained_to_not_having_any_of_the_given_roles()
{
$user1 = User::create();
$user2 = User::create();
$user3 = User::create();

$user1->assign('admin');
$user2->assign('editor');
$user3->assign('subscriber');

$users = User::whereIsNot('superadmin', 'editor', 'subscriber')->get();

$this->assertCount(1, $users);
$this->assertTrue($users->contains($user1));
$this->assertFalse($users->contains($user2));
$this->assertFalse($users->contains($user3));
}
}

0 comments on commit 4f9ee53

Please sign in to comment.