forked from anonaddy/anonaddy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request anonaddy#80 from slurdge/master
Add a command to create a user from command line
- Loading branch information
Showing
1 changed file
with
97 additions
and
0 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
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; | ||
} | ||
} |