diff --git a/app/Console/Commands/CreateUser.php b/app/Console/Commands/CreateUser.php new file mode 100644 index 0000000..aeecc3f --- /dev/null +++ b/app/Console/Commands/CreateUser.php @@ -0,0 +1,97 @@ + $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; + } +}