Skip to content

Commit

Permalink
Added the possibility to attach/detach roles by using the string name
Browse files Browse the repository at this point in the history
Fix #102
  • Loading branch information
santigarcor committed May 16, 2017
1 parent c9f96d5 commit 2a5e582
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 27 deletions.
16 changes: 8 additions & 8 deletions docs/usage/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ Assignment

.. code-block:: php
$user->attachRole($admin); // parameter can be an Role object, array, or id
$user->attachRole($admin); // parameter can be an Role object, array, id or string
// equivalent to $user->roles()->attach([$admin->id]);
$user->attachRoles([$admin, $owner]); // parameter can be an Role object, array, or id
$user->attachRoles([$admin, $owner]); // parameter can be an Role object, array, id or string
// equivalent to $user->roles()->attach([$admin->id, $owner->id]);
$user->syncRoles([$admin->id, $owner->id]);
Expand All @@ -92,10 +92,10 @@ Removal

.. code-block:: php
$user->detachRole($admin); // parameter can be an Role object, array, or id
$user->detachRole($admin); // parameter can be an Role object, array, id or string
// equivalent to $user->roles()->detach([$admin->id]);
$user->detachRoles([$admin, $owner]); // parameter can be an Role object, array, or id
$user->detachRoles([$admin, $owner]); // parameter can be an Role object, array, id or string
// equivalent to $user->roles()->detach([$admin->id, $owner->id]);
User Permissions Assignment & Removal
Expand All @@ -108,10 +108,10 @@ Assignment

.. code-block:: php
$user->attachPermission($editUser); // parameter can be an Permission object, array, or id
$user->attachPermission($editUser); // parameter can be an Permission object, array, id or string
// equivalent to $user->permissions()->attach([$editUser->id]);
$user->attachPermissions([$editUser, $createPost]); // parameter can be an Permission object, array, or id
$user->attachPermissions([$editUser, $createPost]); // parameter can be an Permission object, array, id or string
// equivalent to $user->permissions()->attach([$editUser->id, $createPost->id]);
$user->syncPermissions([$editUser->id, $createPost->id]);
Expand All @@ -122,10 +122,10 @@ Removal

.. code-block:: php
$user->detachPermission($createPost); // parameter can be an Permission object, array, or id
$user->detachPermission($createPost); // parameter can be an Permission object, array, id or string
// equivalent to $user->roles()->detach([$createPost->id]);
$user->detachPermissions([$createPost, $editUser]); // parameter can be an Permission object, array, or id
$user->detachPermissions([$createPost, $editUser]); // parameter can be an Permission object, array, id or string
// equivalent to $user->roles()->detach([$createPost->id, $editUser->id]);
Checking for Roles & Permissions
Expand Down
13 changes: 9 additions & 4 deletions src/Laratrust/Traits/LaratrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public function syncRoles($roles = [])
*/
public function attachPermission($permission)
{
$this->permissions()->attach($this->getIdFor($permission));
$this->permissions()->attach($this->getIdFor($permission, 'permission'));
$this->flushCache();

return $this;
Expand All @@ -373,7 +373,7 @@ public function attachPermission($permission)
*/
public function detachPermission($permission)
{
$this->permissions()->detach($this->getIdFor($permission));
$this->permissions()->detach($this->getIdFor($permission, 'permission'));
$this->flushCache();

return $this;
Expand Down Expand Up @@ -526,17 +526,22 @@ public function flushCache()

/**
* Gets the it from an array or object
* @param mixed $object
* @param mixed $object
* @param string $type
* @return int
*/
private function getIdFor($object)
private function getIdFor($object, $type = 'role')
{
if (is_object($object)) {
return $object->getKey();
} elseif (is_array($object)) {
return $object['id'];
} elseif (is_numeric($object)) {
return $object;
} elseif (is_string($object)) {
return call_user_func_array([
Config::get("laratrust.{$type}"), 'where'
], ['name', $object])->firstOrFail()->getKey();
}

throw new InvalidArgumentException(
Expand Down
76 changes: 61 additions & 15 deletions tests/LaratrustUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function testHasRole()
$this->assertFalse($user->hasRole(['RoleC', 'RoleD']));
}

public function testCan()
public function testHasPermission()
{
/*
|------------------------------------------------------------
Expand Down Expand Up @@ -167,20 +167,54 @@ public function testCan()
| Assertion
|------------------------------------------------------------
*/
$this->assertTrue($user->can([]));
$this->assertTrue($user->can('manage_a'));
$this->assertTrue($user->can('manage_b'));
$this->assertTrue($user->can('manage_c'));
$this->assertTrue($user->can('manage_d'));
$this->assertFalse($user->can('manage_e'));
$this->assertTrue($user->hasPermission([]));
$this->assertTrue($user->hasPermission('manage_a'));
$this->assertTrue($user->hasPermission('manage_b'));
$this->assertTrue($user->hasPermission('manage_c'));
$this->assertTrue($user->hasPermission('manage_d'));
$this->assertFalse($user->hasPermission('manage_e'));

$this->assertTrue($user->can(['manage_a', 'manage_b', 'manage_c']));
$this->assertTrue($user->can(['manage_a', 'manage_b', 'manage_d']));
$this->assertTrue($user->can(['manage_a', 'manage_b', 'manage_d'], true));
$this->assertFalse($user->can(['manage_a', 'manage_b', 'manage_e'], true));
$this->assertFalse($user->can(['manage_e', 'manage_f']));
$this->assertTrue($user->hasPermission(['manage_a', 'manage_b', 'manage_c']));
$this->assertTrue($user->hasPermission(['manage_a', 'manage_b', 'manage_d']));
$this->assertTrue($user->hasPermission(['manage_a', 'manage_b', 'manage_d'], true));
$this->assertFalse($user->hasPermission(['manage_a', 'manage_b', 'manage_e'], true));
$this->assertFalse($user->hasPermission(['manage_e', 'manage_f']));
}

public function testIsAbleTo()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$user = m::mock('HasRoleUser')->makePartial();

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$user->shouldReceive('hasPermission')->with('manage_user', false)->andReturn(true);

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertTrue($user->isAbleTo('manage_user'));
// $this->assertTrue($user->isAbleTo('manage_a'));
// $this->assertTrue($user->isAbleTo('manage_b'));
// $this->assertTrue($user->isAbleTo('manage_c'));
// $this->assertTrue($user->isAbleTo('manage_d'));
// $this->assertFalse($user->isAbleTo('manage_e'));

// $this->assertTrue($user->isAbleTo(['manage_a', 'manage_b', 'manage_c']));
// $this->assertTrue($user->isAbleTo(['manage_a', 'manage_b', 'manage_d']));
// $this->assertTrue($user->isAbleTo(['manage_a', 'manage_b', 'manage_d'], true));
// $this->assertFalse($user->isAbleTo(['manage_a', 'manage_b', 'manage_e'], true));
// $this->assertFalse($user->isAbleTo(['manage_e', 'manage_f']));
}

public function testCanWithPlaceholderSupport()
{
Expand Down Expand Up @@ -255,12 +289,16 @@ public function testAttachRole()
| Expectation
|------------------------------------------------------------
*/
$roleObject->shouldReceive('getKey')->andReturn(1);
$roleObject->shouldReceive('getKey')->andReturn(1)->twice();
$user->shouldReceive('roles')->andReturn($user);
$user->shouldReceive('attach')->with(1)->once()->ordered();
$user->shouldReceive('attach')->with(2)->once()->ordered();
$user->shouldReceive('attach')->with(3)->once()->ordered();
Cache::shouldReceive('forget')->times(6);
$user->shouldReceive('attach')->with(1)->once()->ordered();
Cache::shouldReceive('forget')->times(8);
Config::shouldReceive('get')->with('laratrust.role')->andReturn($roleObject)->once();
$roleObject->shouldReceive('where')->with('name', 'admin')->andReturn($roleObject);
$roleObject->shouldReceive('firstOrFail')->andReturn($roleObject);

/*
|------------------------------------------------------------
Expand All @@ -273,6 +311,8 @@ public function testAttachRole()
$this->assertInstanceOf('HasRoleUser', $result);
$result = $user->attachRole(3);
$this->assertInstanceOf('HasRoleUser', $result);
$result = $user->attachRole('admin');
$this->assertInstanceOf('HasRoleUser', $result);
}

public function testDetachRole()
Expand Down Expand Up @@ -451,7 +491,11 @@ public function testAttachPermission()
$user->shouldReceive('attach')->with(1)->once()->ordered();
$user->shouldReceive('attach')->with(2)->once()->ordered();
$user->shouldReceive('attach')->with(3)->once()->ordered();
Cache::shouldReceive('forget')->times(6);
$user->shouldReceive('attach')->with(1)->once()->ordered();
Cache::shouldReceive('forget')->times(8);
Config::shouldReceive('get')->with('laratrust.permission')->andReturn($permissionObject)->once();
$permissionObject->shouldReceive('where')->with('name', 'edit-post')->andReturn($permissionObject);
$permissionObject->shouldReceive('firstOrFail')->andReturn($permissionObject);

/*
|------------------------------------------------------------
Expand All @@ -464,6 +508,8 @@ public function testAttachPermission()
$this->assertInstanceOf('HasRoleUser', $result);
$result = $user->attachPermission(3);
$this->assertInstanceOf('HasRoleUser', $result);
$result = $user->attachPermission('edit-post');
$this->assertInstanceOf('HasRoleUser', $result);
$this->setExpectedException(InvalidArgumentException::class);
$user->attachPermission(true);
}
Expand Down

0 comments on commit 2a5e582

Please sign in to comment.