Skip to content

Commit

Permalink
Merge pull request anonaddy#80 from slurdge/master
Browse files Browse the repository at this point in the history
Add a command to create a user from command line
  • Loading branch information
willbrowningme authored Sep 27, 2020
2 parents e3ac41d + ec0cdd4 commit a39cc98
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions app/Console/Commands/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Ramsey\Uuid\Uuid;
use App\Models\Recipient;
use App\Models\User;
use App\Rules\NotDeletedUsername;
use App\Rules\NotLocalRecipient;
use App\Rules\RegisterUniqueRecipient;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

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

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

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

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$validator = Validator::make([
'username' => $this->argument('username'),
'email' => $this->argument('email')], [
'username' => [
'required',
'regex:/^[a-zA-Z0-9]*$/',
'max:20',
'unique:users,username',
'unique:additional_usernames,username',
new NotDeletedUsername
],
'email' => [
'required',
'email:rfc,dns',
'max:254',
new RegisterUniqueRecipient,
new NotLocalRecipient
],
]);

if ($validator->fails()) {
$errors = $validator->errors();
foreach ($errors->all() as $message) {
$this->error($message);
}
return 1;
}
$userId = Uuid::uuid4();

$recipient = Recipient::create([
'email' => $this->argument('email'),
'user_id' => $userId
]);

$twoFactor = app('pragmarx.google2fa');

$user = User::create([
'id' => $userId,
'username' => $this->argument('username'),
'default_recipient_id' => $recipient->id,
'password' => Hash::make($userId),
'two_factor_secret' => $twoFactor->generateSecretKey()
]);

$this->info('Created user: '.$user->username.' with userid: '.$user->id);
$this->info('This user can now reset their password (default password is userid)');

return 0;
}
}

0 comments on commit a39cc98

Please sign in to comment.