Skip to content

Commit

Permalink
Merge pull request #6 from ahmadshah/feature/user-model-helper
Browse files Browse the repository at this point in the history
Add role helper for user model
  • Loading branch information
crynobone committed Jan 9, 2014
2 parents 77ea1d3 + 9f93772 commit 851f38f
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/Orchestra/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,122 @@ public function detachRole($roles)
{
$this->roles()->detach((array) $roles);
}

/**
* Determine if current user has the given role.
*
* @param string $roles
* @return boolean
*/
public function is($roles)
{
$userRoles = $this->resolveRolesAsArray();

// For a pre-caution, we should return false in events where user
// roles not an array.
if (! is_array($userRoles)) {
return false;
}

// We should ensure that all given roles match the current user,
// consider it as a AND condition instead of OR.
foreach ((array) $roles as $role) {
if (! in_array($role, $userRoles)) {
return false;
}
}

return true;
}

/**
* Determine if current user has any of the given role.
*
* @param array $roles
* @return boolean
*/
public function isAny(array $roles)
{
$userRoles = $this->resolveRolesAsArray();

// For a pre-caution, we should return false in events where user
// roles not an array.
if (! is_array($userRoles)) {
return false;
}

// We should ensure that any given roles match the current user,
// consider it as OR condition.
foreach ($roles as $role) {
if (in_array($role, $userRoles)) {
return true;
}
}

return false;
}

/**
* Determine if current user does not has any of the given role.
*
* @param string $roles
* @return boolean
*/
public function isNot($roles)
{
$userRoles = $this->resolveRolesAsArray();

// For a pre-caution, we should return false in events where user
// roles not an array.
if (! is_array($userRoles)) {
return false;
}

// We should ensure that any given roles does not match the current
// user, consider it as OR condition.
foreach ((array) $roles as $role) {
if (in_array($role, $userRoles)) {
return false;
}
}

return true;
}

/**
* Determine if current user does not has any of the given role.
*
* @param array $roles
* @return boolean
*/
public function isNotAny(array $roles)
{
$userRoles = $this->resolveRolesAsArray();

// For a pre-caution, we should return false in events where user
// roles not an array.
if (! is_array($userRoles)) {
return false;
}

// We should ensure that any given roles does not match the current user,
// consider it as OR condition.
foreach ($roles as $role) {
if (! in_array($role, $userRoles)) {
return true;
}
}

return false;
}

/**
* Resolve roles into array for checking
*
* @return array
*/
protected function resolveRolesAsArray()
{
return $this->roles()->lists('name');
}
}
152 changes: 152 additions & 0 deletions tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,158 @@ public function testDetachRoleMethod()
$model->detachRole(2);
}

/**
* Test Orchestra\Model\User::is() method
*
* @test
*/
public function testIsMethod()
{
$model = m::mock('\Orchestra\Model\User[roles]');
$relationship = m::mock('\Illuminate\Database\Eloquent\Relations\BelongsToMany[lists]');

$model->shouldReceive('roles')->times(4)->andReturn($relationship);
$relationship->shouldReceive('lists')->times(4)->andReturn(array('admin', 'editor'));

$this->assertTrue($model->is('admin'));
$this->assertFalse($model->is('user'));

$this->assertTrue($model->is(array('admin', 'editor')));
$this->assertFalse($model->is(array('admin', 'user')));
}

/**
* Test Orchestra\Support\Auth::is() method when invalid roles is
* returned.
*
* @test
*/
public function testIsMethodWhenInvalidRolesIsReturned()
{
$model = m::mock('\Orchestra\Model\User[roles]');
$relationship = m::mock('\Illuminate\Database\Eloquent\Relations\BelongsToMany[lists]');

$model->shouldReceive('roles')->times(4)->andReturn($relationship);
$relationship->shouldReceive('lists')->times(4)->andReturn('foo');

$this->assertFalse($model->is('admin'));
$this->assertFalse($model->is('user'));

$this->assertFalse($model->is(array('admin', 'editor')));
$this->assertFalse($model->is(array('admin', 'user')));
}

/**
* Test Orchestra\Model\User::isNot() method
*
* @test
*/
public function testIsNotMethod()
{
$model = m::mock('\Orchestra\Model\User[roles]');
$relationship = m::mock('\Illuminate\Database\Eloquent\Relations\BelongsToMany[lists]');

$model->shouldReceive('roles')->times(4)->andReturn($relationship);
$relationship->shouldReceive('lists')->times(4)->andReturn(array('admin', 'editor'));

$this->assertTrue($model->isNot('user'));
$this->assertFalse($model->isNot('admin'));

$this->assertTrue($model->isNot(array('superadmin', 'user')));
$this->assertFalse($model->isNot(array('admin', 'editor')));
}

/**
* Test Orchestra\Support\Auth::isNot() method when invalid roles is
* returned.
*
* @test
*/
public function testIsNotMethodWhenInvalidRolesIsReturned()
{
$model = m::mock('\Orchestra\Model\User[roles]');
$relationship = m::mock('\Illuminate\Database\Eloquent\Relations\BelongsToMany[lists]');

$model->shouldReceive('roles')->times(4)->andReturn($relationship);
$relationship->shouldReceive('lists')->times(4)->andReturn('foo');

$this->assertFalse($model->isNot('admin'));
$this->assertFalse($model->isNot('user'));

$this->assertFalse($model->isNot(array('admin', 'editor')));
$this->assertFalse($model->isNot(array('admin', 'user')));
}

/**
* Test Orchestra\Model\User::isAny() method
*
* @test
*/
public function testIsAnyMethod()
{
$model = m::mock('\Orchestra\Model\User[roles]');
$relationship = m::mock('\Illuminate\Database\Eloquent\Relations\BelongsToMany[lists]');

$model->shouldReceive('roles')->twice()->andReturn($relationship);
$relationship->shouldReceive('lists')->twice()->andReturn(array('admin', 'editor'));

$this->assertTrue($model->isAny(array('admin', 'user')));
$this->assertFalse($model->isAny(array('superadmin', 'user')));
}

/**
* Test Orchestra\Support\Auth::isAny() method when invalid roles is
* returned.
*
* @test
*/
public function testIsAnyMethodWhenInvalidRolesIsReturned()
{
$model = m::mock('\Orchestra\Model\User[roles]');
$relationship = m::mock('\Illuminate\Database\Eloquent\Relations\BelongsToMany[lists]');

$model->shouldReceive('roles')->twice()->andReturn($relationship);
$relationship->shouldReceive('lists')->times(2)->andReturn('foo');

$this->assertFalse($model->isAny(array('admin', 'editor')));
$this->assertFalse($model->isAny(array('admin', 'user')));
}

/**
* Test Orchestra\Model\User::isNotAny() method
*
* @test
*/
public function testIsNotAnyMethod()
{
$model = m::mock('\Orchestra\Model\User[roles]');
$relationship = m::mock('\Illuminate\Database\Eloquent\Relations\BelongsToMany[lists]');

$model->shouldReceive('roles')->twice()->andReturn($relationship);
$relationship->shouldReceive('lists')->twice()->andReturn(array('admin', 'editor'));

$this->assertTrue($model->isNotAny(array('admin', 'user')));
$this->assertFalse($model->isNotAny(array('admin', 'editor')));
}

/**
* Test Orchestra\Support\Auth::isNotAny() method when invalid roles is
* returned.
*
* @test
*/
public function testIsNotAnyMethodWhenInvalidRolesIsReturned()
{
$model = m::mock('\Orchestra\Model\User[roles]');
$relationship = m::mock('\Illuminate\Database\Eloquent\Relations\BelongsToMany[lists]');

$model->shouldReceive('roles')->twice()->andReturn($relationship);
$relationship->shouldReceive('lists')->twice()->andReturn('foo');

$this->assertFalse($model->isNotAny(array('admin', 'editor')));
$this->assertFalse($model->isNotAny(array('admin', 'user')));
}

/**
* Test Orchestra\Model\User::scopeSearch() method.
*
Expand Down

0 comments on commit 851f38f

Please sign in to comment.