diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 7995c57cd..d52114b66 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -373,11 +373,19 @@ public function postAdminEdit(Request $request) $user = User::find($user_id); + $oldRole = $user->role; + // Set role for User $user->update([ - 'role' => $request->input('user_role'), + 'role' => $request->input('user_role'), ]); + // If we are demoting from NetworkCoordinator, remove them from the list of coordinators for + // any networks they are currently coordinating. + if ($oldRole == Role::NETWORK_COORDINATOR && ($user->role == Role::HOST || $user->role == Role::RESTARTER)) { + $user->networks()->detach(); + } + // The user may have previously been removed from the group, which will mean they have an entry in // users_groups with deleted_at set. Zap that if present so that sync() then works. sync() doesn't // handle soft deletes itself. diff --git a/tests/Feature/Events/AddRemoveVolunteerTest.php b/tests/Feature/Events/AddRemoveVolunteerTest.php index 88d34800f..5a047e91b 100644 --- a/tests/Feature/Events/AddRemoveVolunteerTest.php +++ b/tests/Feature/Events/AddRemoveVolunteerTest.php @@ -9,6 +9,7 @@ use App\Notifications\AdminModerationEvent; use App\Notifications\NotifyRestartersOfNewEvent; use App\Party; +use App\Role; use App\User; use DB; use Faker\Generator as Faker; @@ -198,7 +199,7 @@ public function testAdminRemoveReaddHost() { $response = $this->post('/profile/edit-admin-settings', [ '_token' => $tokenValue, 'id' => $host->id, - 'user_role' => 2, + 'user_role' => Role::ADMINISTRATOR, 'assigned_groups' => [ $idgroups ], diff --git a/tests/Feature/Networks/NetworkTest.php b/tests/Feature/Networks/NetworkTest.php index f563f4cd9..9b3109292 100644 --- a/tests/Feature/Networks/NetworkTest.php +++ b/tests/Feature/Networks/NetworkTest.php @@ -19,11 +19,6 @@ class NetworkTest extends TestCase protected function setUp(): void { parent::setUp(); - DB::statement('SET foreign_key_checks=0'); - Network::truncate(); - DB::delete('delete from user_network'); - DB::statement('SET foreign_key_checks=1'); - $this->networkService = new RepairNetworkService(); } @@ -305,10 +300,7 @@ public function admins_can_edit() $admin = User::factory()->administrator()->create(); $this->actingAs($admin); - $network = new Network(); - $network->name = 'Restarters'; - $network->shortname = 'restarters'; - $network->save(); + $network = Network::where('shortname', 'restarters')->first(); $response = $this->get('/networks/' . $network->id . '/edit', $network->attributesToArray()); $response->assertSuccessful(); @@ -330,4 +322,39 @@ public function admins_can_edit() $this->assertTrue($network->containsGroup($group)); $this->assertTrue($group->isMemberOf($network)); } + + public function testRemoveNetworkCoordinatorByRole() { + $this->withoutExceptionHandling(); + + $network = Network::factory()->create(); + + $admin = User::factory()->administrator()->create(); + $coordinator = User::factory()->networkCoordinator()->create(); + $network->addCoordinator($coordinator); + + $this->actingAs($admin); + + $response = $this->get('/user/edit/' . $coordinator->id); + $response->assertStatus(200); + + $crawler = new Crawler($response->getContent()); + + $tokens = $crawler->filter('input[name=_token]')->each(function (Crawler $node, $i) { + return $node; + }); + + $tokenValue = $tokens[0]->attr('value'); + + $response = $this->post('/profile/edit-admin-settings', [ + '_token' => $tokenValue, + 'id' => $coordinator->id, + 'assigned_groups' => [], + 'user_role' => Role::HOST, + ]); + $response->assertSessionHas('message'); + $this->assertTrue($response->isRedirection()); + + // Demoting to host should remove as a network coordinator. + $this->assertFalse($network->coordinators->contains($coordinator)); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index cb82ed365..4fe386f7f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -94,6 +94,8 @@ protected function setUp(): void $network->name = 'Restarters'; $network->shortname = 'restarters'; $network->save(); + } else { + error_log("Got network"); } $this->withoutExceptionHandling();