Skip to content

Commit

Permalink
Add commands to create hosts for MRES.
Browse files Browse the repository at this point in the history
  • Loading branch information
edwh committed Oct 7, 2022
1 parent 73bb49f commit defd6ea
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 3 deletions.
33 changes: 31 additions & 2 deletions app/Console/Commands/ImportMRES.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

use function Symfony\Component\VarDumper\Dumper\esc;

class ImportMRES extends Command
{
Expand All @@ -12,15 +15,15 @@ class ImportMRES extends Command
*
* @var string
*/
protected $signature = 'import:mres {input} {output} {--networks=CSV list of ids}';
protected $signature = 'import:mres {input} {output} {commands} {--networks=CSV list of ids}';


/**
* The console command description.
*
* @var string
*/
protected $description = 'One-off script to import MRES groups. This takes a CSV of the groups in MRES format and outputs a CSV in our standard import format. You can convert from the ODS file to CSV; make sure you select "Western Europe (ISO08859-15/EURO)" as the Character set.';
protected $description = 'One-off script to import MRES groups. This takes a CSV of the groups in MRES format and outputs a CSV in our standard import format. You can convert from the ODS file to CSV; make sure you select "Western Europe (ISO08859-15/EURO)" as the Character set. It also outputs a command script with user creation commands.';

/**
* Create a new command instance.
Expand All @@ -41,10 +44,12 @@ public function handle()
{
$input = $this->argument('input');
$output = $this->argument('output');
$commands = $this->argument('commands');
$networks = $this->option('networks');

$inputFile = fopen($input, 'r');
$outputFile = fopen($output, 'w');
$commandsFile = fopen($commands, 'w');

// First three lines are headers.
fgetcsv($inputFile);
Expand All @@ -54,6 +59,8 @@ public function handle()
// Write headers to output.
fputcsv($outputFile, ['Name', 'Location', 'Postcode', 'Area', 'Country', 'Latitude', 'Longitude', 'Website', 'Phone', 'Networks', 'Description']);

$creating = [];

while (!feof($inputFile))
{
$fields = fgetcsv($inputFile);
Expand Down Expand Up @@ -153,6 +160,28 @@ public function handle()
$networks,
$description,
]);

// Now the host, if any.
if (!$email) {
// We default to Enzo.
$email = "e.mandrin@mres-asso.fr";
$hostname = "Enzo Mandrin";
}

if (!$hostname) {
// We use the LHS of the email.
$hostname = explode("@", $email)[0];
}

// Random password.
$password = Str::random(32);

if (User::where('email', '=', $email)->count() == 0 && !array_key_exists($email, $creating)) {
// User doesn't exist, create it.
$creating[$email] = true;
fwrite($commandsFile, "php artisan user:create " . escapeshellarg($hostname) . " " . escapeshellarg($email) . " " . escapeshellarg($password) . "\n");
fwrite($commandsFile, "php artisan user:makehost " . escapeshellarg($email) . " " . escapeshellarg(utf8_encode($groupname)) . "\n");
}
}
}
}
Expand Down
91 changes: 91 additions & 0 deletions app/Console/Commands/UserCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace App\Console\Commands;

use App\Role;
use App\User;
use App\WikiSyncStatus;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class UserCreate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:create {name} {email} {password}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a user.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$name = $this->argument('name');
$email = $this->argument('email');
$password = $this->argument('password');

if (User::where('email', $email)->count() > 0) {
$this->info("User $email already exists - leaving unmodified");
return;
}

$rules = [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
];

$validator = Validator::make([
'name' => $name,
'email' => $email,
'password' => $password,
], $rules);

if ($validator->fails()) {
$this->error("Invalid parameters " . $validator->messages()->toJson());
} else {
$user = User::create([
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
'role' => Role::RESTARTER,
'recovery' => substr(bin2hex(openssl_random_pseudo_bytes(32)), 0, 24),
'recovery_expires' => strftime('%Y-%m-%d %X', time() + (24 * 60 * 60)),
'calendar_hash' => Str::random(15),
'username' => '',
'wiki_sync_status' => WikiSyncStatus::CreateAtLogin
]);

if ($user) {
$this->info("User created #" . $user->id);
} else {
$this->error("User creation failed");
}

$user->generateAndSetUsername();
}
}
}
69 changes: 69 additions & 0 deletions app/Console/Commands/UserMakeHost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Console\Commands;

use App\Role;
use App\User;
use App\WikiSyncStatus;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class UserMakeHost extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:makehost {email} {groupname}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Make a user a host of a group.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$email = $this->argument('email');
$groupname = $this->argument('groupname');

$user = User::where('email', $email)->first();
if (!$user) {
$this->error("User $email not found.");
return;
}

$group = \App\Group::where('name', $groupname)->first();
if (!$group) {
$this->error("Group $groupname not found.");
return;
}

if ($group->isVolunteer($user->id)) {
$this->info("User $email is already a volunteer for group $groupname.");
} else {
$group->addVolunteer($user);
$group->makeMemberAHost($user);
$this->info("User $email is now a host of group $groupname.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertTrue;

class NetworkCommands extends TestCase {
class NetworkCommandsTest extends TestCase {
public function testCreate() {
$this->artisan('network:create testname testshortname "test description" --website="https://therestartproject.org" --language=fr --timezone="Asia/Samarkand" --wordpress --zapier --drip --auto-approve-events')->assertExitCode(0);
$network = Network::orderBy('id', 'desc')->first();
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/Users/UserCommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Commands;

use App\Group;
use App\Helpers\Fixometer;
use App\User;
use DB;
use Tests\TestCase;

use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertTrue;

class UserCommandsTest extends TestCase {
public function testCreate() {
$this->artisan('user:create testname test@test.com 1234567890')->assertExitCode(0);
$user = User::where('email', 'test@test.com')->first();
self::assertEquals('testname', $user->name);
}

public function testMakeHost() {
$host = factory(User::class)->states('Host')->create();
$group = factory(Group::class)->create();
assertFalse(Fixometer::userIsHostOfGroup($group->idgroups, $host->id));
$this->artisan('user:makehost ' . escapeshellarg($host->email) . ' ' . escapeshellarg($group->name))->assertExitCode(0);
assertTrue(Fixometer::userIsHostOfGroup($group->idgroups, $host->id));
}
}

0 comments on commit defd6ea

Please sign in to comment.