Skip to content

Commit

Permalink
Progress on making teams feature optional
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Jun 15, 2017
1 parent 4faefce commit eaacf90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/Laratrust/Traits/LaratrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ private function teamForeignKey()
*/
private function isInSameTeam($rolePermission, $team)
{
if (!Config::get('laratrust.use_teams')) {
return true;
}

$teamForeignKey = $this->teamForeignKey();
return $rolePermission->pivot->$teamForeignKey == $team;
}
Expand All @@ -160,8 +164,8 @@ private function isInSameTeam($rolePermission, $team)
*/
private function fetchTeam($team = null)
{
if (is_null($team)) {
return $team;
if (is_null($team) || !Config::get('laratrust.use_teams')) {
return null;
}

$team = call_user_func_array(
Expand Down
24 changes: 17 additions & 7 deletions tests/LaratrustUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ public function testHasRole()
| Expectation
|------------------------------------------------------------
*/
Config::shouldReceive('get')->with('cache.ttl', 60)->times(14)->andReturn('1440');
Cache::shouldReceive('remember')->times(14)->andReturn($user->roles);
Config::shouldReceive('get')->with('laratrust.use_teams')->times(18)->andReturn(true)->ordered();
Config::shouldReceive('get')->with('laratrust.use_teams')->times(3)->andReturn(false)->ordered();
Config::shouldReceive('get')->with('cache.ttl', 60)->times(17)->andReturn('1440');
Cache::shouldReceive('remember')->times(17)->andReturn($user->roles);
Config::shouldReceive('get')->with('laratrust.team_foreign_key')->times(13)->andReturn('team_id');
Config::shouldReceive('get')->with('laratrust.team')->times(5)->andReturn($team);
$team->shouldReceive('where')->with('name', 'TeamA')->times(5)->andReturn($team);
Expand All @@ -138,6 +140,9 @@ public function testHasRole()
$this->assertFalse($user->hasRole(['RoleA', 'RoleC'], 'TeamA', true));
$this->assertFalse($user->hasRole(['RoleA', 'RoleC'], true));
$this->assertFalse($user->hasRole(['RoleC', 'RoleD']));
// Not using teams
$this->assertTrue($user->hasRole(['RoleA', 'RoleC'], 'TeamA'));
$this->assertFalse($user->hasRole(['RoleC', 'RoleD'], true));
}

public function testHasPermission()
Expand Down Expand Up @@ -169,9 +174,11 @@ public function testHasPermission()
| Expectation
|------------------------------------------------------------
*/
$roleA->shouldReceive('cachedPermissions')->times(10)->andReturn($roleA->perms);
$roleB->shouldReceive('cachedPermissions')->twice()->andReturn($roleB->perms);
Config::shouldReceive('get')->with('cache.ttl', 60)->times(27)->andReturn('1440');
Config::shouldReceive('get')->with('laratrust.use_teams')->times(51)->andReturn(true)->ordered();
Config::shouldReceive('get')->with('laratrust.use_teams')->times()->andReturn(false)->ordered();
$roleA->shouldReceive('cachedPermissions')->times(12)->andReturn($roleA->perms);
$roleB->shouldReceive('cachedPermissions')->times(3 )->andReturn($roleB->perms);
Config::shouldReceive('get')->with('cache.ttl', 60)->times(32)->andReturn('1440');
Config::shouldReceive('get')->with('laratrust.team_foreign_key')->times(48)->andReturn('team_id');
Config::shouldReceive('get')->with('laratrust.team')->times(3)->andReturn($team);
$team->shouldReceive('where')->with('name', 'TeamA')->times(3)->andReturn($team);
Expand All @@ -183,13 +190,13 @@ public function testHasPermission()
"laratrust_permissions_for_user_{$user->getKey()}",
1440,
m::any()
)->times(15)->andReturn($user->permissions);
)->times(18)->andReturn($user->permissions);
Cache::shouldReceive('remember')
->with(
"laratrust_roles_for_user_{$user->getKey()}",
1440,
m::any()
)->times(12)->andReturn($user->roles);
)->times(14)->andReturn($user->roles);

/*
|------------------------------------------------------------
Expand All @@ -209,6 +216,8 @@ public function testHasPermission()
$this->assertFalse($user->hasPermission(['manage_a', 'manage_b', 'manage_d'], 'TeamA', true));
$this->assertFalse($user->hasPermission(['manage_a', 'manage_b', 'manage_e'], true));
$this->assertFalse($user->hasPermission(['manage_e', 'manage_f']));
// Not using teams
$this->assertTrue($user->hasPermission(['manage_a', 'manage_b', 'manage_d'], 'TeamA', true));
}

public function testCan()
Expand Down Expand Up @@ -286,6 +295,7 @@ public function testHasPermissionWithPlaceholderSupport()
| Expectation
|------------------------------------------------------------
*/
Config::shouldReceive('get')->with('laratrust.use_teams')->times(17)->andReturn(true)->ordered();
$role->shouldReceive('cachedPermissions')->times(6)->andReturn($role->perms);
Config::shouldReceive('get')->with('cache.ttl', 60)->times(15)->andReturn('1440');
Config::shouldReceive('get')->with('laratrust.team_foreign_key')->times(15)->andReturn('team_id');
Expand Down

0 comments on commit eaacf90

Please sign in to comment.