-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands to create hosts for MRES.
- Loading branch information
Showing
5 changed files
with
220 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |