Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add role helper for user model #6

Merged
merged 2 commits into from
Jan 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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