2022-05-04 16:03:13 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Auth;
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Access\GroupSyncService;
|
|
|
|
use BookStack\Users\Models\Role;
|
|
|
|
use BookStack\Users\Models\User;
|
2022-05-04 16:03:13 -04:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class GroupSyncServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
public function test_user_is_assigned_to_matching_roles()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->viewer();
|
2022-05-04 16:03:13 -04:00
|
|
|
|
|
|
|
$roleA = Role::factory()->create(['display_name' => 'Wizards']);
|
|
|
|
$roleB = Role::factory()->create(['display_name' => 'Gremlins']);
|
|
|
|
$roleC = Role::factory()->create(['display_name' => 'ABC123', 'external_auth_id' => 'sales']);
|
|
|
|
$roleD = Role::factory()->create(['display_name' => 'DEF456', 'external_auth_id' => 'admin-team']);
|
|
|
|
|
2022-06-08 13:00:30 -04:00
|
|
|
foreach ([$roleA, $roleB, $roleC, $roleD] as $role) {
|
2022-05-04 16:03:13 -04:00
|
|
|
$this->assertFalse($user->hasRole($role->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
(new GroupSyncService())->syncUserWithFoundGroups($user, ['Wizards', 'Gremlinz', 'Sales', 'Admin Team'], false);
|
|
|
|
|
|
|
|
$user = User::query()->find($user->id);
|
|
|
|
$this->assertTrue($user->hasRole($roleA->id));
|
|
|
|
$this->assertFalse($user->hasRole($roleB->id));
|
|
|
|
$this->assertTrue($user->hasRole($roleC->id));
|
|
|
|
$this->assertTrue($user->hasRole($roleD->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_multiple_values_in_role_external_auth_id_handled()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->viewer();
|
2022-05-04 16:03:13 -04:00
|
|
|
$role = Role::factory()->create(['display_name' => 'ABC123', 'external_auth_id' => 'sales, engineering, developers, marketers']);
|
|
|
|
$this->assertFalse($user->hasRole($role->id));
|
|
|
|
|
|
|
|
(new GroupSyncService())->syncUserWithFoundGroups($user, ['Developers'], false);
|
|
|
|
|
|
|
|
$user = User::query()->find($user->id);
|
|
|
|
$this->assertTrue($user->hasRole($role->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_commas_can_be_used_in_external_auth_id_if_escaped()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->viewer();
|
2022-05-04 16:03:13 -04:00
|
|
|
$role = Role::factory()->create(['display_name' => 'ABC123', 'external_auth_id' => 'sales\,-developers, marketers']);
|
|
|
|
$this->assertFalse($user->hasRole($role->id));
|
|
|
|
|
|
|
|
(new GroupSyncService())->syncUserWithFoundGroups($user, ['Sales, Developers'], false);
|
|
|
|
|
|
|
|
$user = User::query()->find($user->id);
|
|
|
|
$this->assertTrue($user->hasRole($role->id));
|
|
|
|
}
|
2022-06-27 09:17:42 -04:00
|
|
|
|
|
|
|
public function test_external_auth_id_matches_ignoring_case()
|
|
|
|
{
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->viewer();
|
2022-06-27 09:17:42 -04:00
|
|
|
$role = Role::factory()->create(['display_name' => 'ABC123', 'external_auth_id' => 'WaRRioRs']);
|
|
|
|
$this->assertFalse($user->hasRole($role->id));
|
|
|
|
|
|
|
|
(new GroupSyncService())->syncUserWithFoundGroups($user, ['wArriors', 'penguiNs'], false);
|
|
|
|
|
|
|
|
$user = User::query()->find($user->id);
|
|
|
|
$this->assertTrue($user->hasRole($role->id));
|
|
|
|
}
|
2022-06-08 13:00:30 -04:00
|
|
|
}
|