Skip to content

Commit

Permalink
Add sync without detaching to roles and permissions
Browse files Browse the repository at this point in the history
Fix #177
  • Loading branch information
santigarcor committed Aug 5, 2017
1 parent bfc5d45 commit 079b673
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 16 deletions.
41 changes: 34 additions & 7 deletions src/Laratrust/Traits/LaratrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private function detachModel($relationship, $objectType, $object, $team)
return $this;
}

private function syncModels($relationship, $objectType, $objects, $team)
private function syncModels($relationship, $objectType, $objects, $team, $detaching)
{
$mappedObjects = [];

Expand All @@ -400,7 +400,7 @@ private function syncModels($relationship, $objectType, $objects, $team)
$mappedObjects = $objects;
}

$this->$relationship()->sync($mappedObjects);
$this->$relationship()->sync($mappedObjects, $detaching);
$this->flushCache();

return $this;
Expand Down Expand Up @@ -471,11 +471,24 @@ public function detachRoles($roles = [], $team = null)
*
* @param array $roles
* @param mixed $team
* @param boolean $detaching
* @return static
*/
public function syncRoles($roles = [], $team = null)
public function syncRoles($roles = [], $team = null, $detaching = true)
{
return $this->syncModels('roles', 'role', $roles, $team);
return $this->syncModels('roles', 'role', $roles, $team, $detaching);
}

/**
* Sync roles to the user without detaching.
*
* @param array $roles
* @param mixed $team
* @return static
*/
public function syncRolesWithoutDetaching($roles = [], $team = null)
{
return $this->syncRoles($roles, $team, false);
}

/**
Expand Down Expand Up @@ -539,14 +552,28 @@ public function detachPermissions($permissions = [], $team = null)
}

/**
* Sync roles to the user.
* Sync permissions to the user.
*
* @param array $permissions
* @param mixed $team
* @param boolean $detaching
* @return static
*/
public function syncPermissions($permissions = [], $team = null, $detaching = true)
{
return $this->syncModels('permissions', 'permission', $permissions, $team, $detaching);
}

/**
* Sync permissions to the user without detaching.
*
* @param array $permissions
* @param mixed $team
* @return static
*/
public function syncPermissions($permissions = [], $team = null)
public function syncPermissionsWithoutDetaching($permissions = [], $team = null)
{
return $this->syncModels('permissions', 'permission', $permissions, $team);
return $this->syncModels('permissions', 'permission', $permissions, $team, false);
}

/**
Expand Down
109 changes: 100 additions & 9 deletions tests/LaratrustUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,14 @@ public function testSyncRoles()
1 => ['team_id' => null],
2 => ['team_id' => null],
3 => ['team_id' => null]
])->once()->ordered();
], true)->once()->ordered();
$user->shouldReceive('sync')->with([
1 => ['team_id' => 'TeamA'],
2 => ['team_id' => 'TeamA'],
3 => ['team_id' => 'TeamA']
])->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3])->twice()->ordered();
], true)->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3], true)->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3], false)->once()->ordered();
Cache::shouldReceive('forget')->times(8);

/*
Expand All @@ -578,8 +579,53 @@ public function testSyncRoles()
$this->assertInstanceOf('HasRoleUser', $user->syncRoles($rolesIds));
$this->assertInstanceOf('HasRoleUser', $user->syncRoles($rolesIds, 'TeamA'));
// Not using teams
$this->assertInstanceOf('HasRoleUser', $user->syncRoles($rolesIds));
$this->assertInstanceOf('HasRoleUser', $user->syncRoles($rolesIds, 'TeamA'));
$this->assertInstanceOf('HasRoleUser', $user->syncRoles($rolesIds, null));
$this->assertInstanceOf('HasRoleUser', $user->syncRoles($rolesIds, 'TeamA', false));
}

public function testSyncRolesWithoutDetaching()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$rolesIds = [1, 2, 3];
$user = m::mock('HasRoleUser')->makePartial();

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
Config::shouldReceive('get')->with('laratrust.use_teams')->andReturn(true)->twice()->ordered();
Config::shouldReceive('get')->with('laratrust.use_teams')->andReturn(false)->twice()->ordered();
Config::shouldReceive('get')->with('laratrust.foreign_keys.team')->andReturn('team_id')->times(6);
$user->shouldReceive('roles')->andReturn($user)->times(4);
$user->shouldReceive('sync')->with([
1 => ['team_id' => null],
2 => ['team_id' => null],
3 => ['team_id' => null]
], false)->once()->ordered();
$user->shouldReceive('sync')->with([
1 => ['team_id' => 'TeamA'],
2 => ['team_id' => 'TeamA'],
3 => ['team_id' => 'TeamA']
], false)->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3], false)->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3], false)->once()->ordered();
Cache::shouldReceive('forget')->times(8);

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertInstanceOf('HasRoleUser', $user->syncRolesWithoutDetaching($rolesIds));
$this->assertInstanceOf('HasRoleUser', $user->syncRolesWithoutDetaching($rolesIds, 'TeamA'));
// Not using teams
$this->assertInstanceOf('HasRoleUser', $user->syncRolesWithoutDetaching($rolesIds, null));
$this->assertInstanceOf('HasRoleUser', $user->syncRolesWithoutDetaching($rolesIds, 'TeamA'));
}

public function testAttachPermission()
Expand Down Expand Up @@ -770,13 +816,14 @@ public function testSyncPermissions()
1 => ['team_id' => null],
2 => ['team_id' => null],
3 => ['team_id' => null]
])->once()->ordered();
], true)->once()->ordered();
$user->shouldReceive('sync')->with([
1 => ['team_id' => 'TeamA'],
2 => ['team_id' => 'TeamA'],
3 => ['team_id' => 'TeamA']
])->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3])->twice()->ordered();
], true)->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3], true)->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3], false)->once()->ordered();
Cache::shouldReceive('forget')->times(8);

/*
Expand All @@ -788,7 +835,51 @@ public function testSyncPermissions()
$this->assertInstanceOf('HasRoleUser', $user->syncPermissions($permissionsIds, 'TeamA'));
// Not using teams
$this->assertInstanceOf('HasRoleUser', $user->syncPermissions($permissionsIds));
$this->assertInstanceOf('HasRoleUser', $user->syncPermissions($permissionsIds, 'TeamA'));
$this->assertInstanceOf('HasRoleUser', $user->syncPermissions($permissionsIds, 'TeamA', false));
}

public function testSyncPermissionsWithoutDetaching()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$permissionsIds = [1, 2, 3];
$user = m::mock('HasRoleUser')->makePartial();

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
Config::shouldReceive('get')->with('laratrust.use_teams')->andReturn(true)->twice()->ordered();
Config::shouldReceive('get')->with('laratrust.use_teams')->andReturn(false)->twice()->ordered();
Config::shouldReceive('get')->with('laratrust.foreign_keys.team')->andReturn('team_id')->times(6);
$user->shouldReceive('permissions')->andReturn($user)->times(4);
$user->shouldReceive('sync')->with([
1 => ['team_id' => null],
2 => ['team_id' => null],
3 => ['team_id' => null]
], false)->once()->ordered();
$user->shouldReceive('sync')->with([
1 => ['team_id' => 'TeamA'],
2 => ['team_id' => 'TeamA'],
3 => ['team_id' => 'TeamA']
], false)->once()->ordered();
$user->shouldReceive('sync')->with([1, 2, 3], false)->twice()->ordered();
Cache::shouldReceive('forget')->times(8);

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertInstanceOf('HasRoleUser', $user->syncPermissionsWithoutDetaching($permissionsIds));
$this->assertInstanceOf('HasRoleUser', $user->syncPermissionsWithoutDetaching($permissionsIds, 'TeamA'));
// Not using teams
$this->assertInstanceOf('HasRoleUser', $user->syncPermissionsWithoutDetaching($permissionsIds));
$this->assertInstanceOf('HasRoleUser', $user->syncPermissionsWithoutDetaching($permissionsIds, 'TeamA'));
}

public function testUserOwnsaPostModel()
Expand Down

0 comments on commit 079b673

Please sign in to comment.