Skip to content

Commit

Permalink
Merge pull request #661 from TheRestartProject/RES-1906_remove_networ…
Browse files Browse the repository at this point in the history
…k_coordinator

RES-1906 Demote network coordinator
  • Loading branch information
edwh authored Jun 12, 2023
2 parents 01afde0 + 77120b0 commit 9aab875
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
10 changes: 9 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/Events/AddRemoveVolunteerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
],
Expand Down
45 changes: 36 additions & 9 deletions tests/Feature/Networks/NetworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
Expand All @@ -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));
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ protected function setUp(): void
$network->name = 'Restarters';
$network->shortname = 'restarters';
$network->save();
} else {
error_log("Got network");
}

$this->withoutExceptionHandling();
Expand Down

0 comments on commit 9aab875

Please sign in to comment.