From e61a61c9631f98eb4f61ef941a1321891d947558 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 6 Apr 2021 00:26:50 -0700 Subject: [PATCH] Add a user create console command --- app/Console/Commands/CreateUser.php | 86 +++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 app/Console/Commands/CreateUser.php diff --git a/app/Console/Commands/CreateUser.php b/app/Console/Commands/CreateUser.php new file mode 100644 index 0000000..613ac19 --- /dev/null +++ b/app/Console/Commands/CreateUser.php @@ -0,0 +1,86 @@ +options(); + if (!$options['email']) { + $options['email'] = $this->ask('Enter an email address for the user'); + } + if (!$options['name']) { + $options['name'] = $this->ask('Enter a name for the user'); + } + if (!$options['pass']) { + $options['pass'] = $this->secret('Enter a password for the user'); + $password_confirm = $this->secret('Re-type the password to confirm'); + if ($options['pass'] !== $password_confirm) { + $this->error('Passwords did not match.'); + return 1; + } + } + + if (empty($options['email']) || empty($options['name']) || empty($options['pass'])) { + $this->error('All options (name, email, and password) are required.'); + return 1; + } + + // Validate email. + $validator = Validator::make(['email' => $options['email']], [ + 'email' => 'email', + ]); + if ($validator->fails()) { + $this->error("Invalid email address {$options['email']}."); + return 1; + } + + // Check for an existing user. + if (User::whereEmail($options['email'])->exists()) { + $this->error("User with email address {$options['email']} already exists."); + return 1; + } + + User::create([ + 'name' => $options['name'], + 'email' => $options['email'], + 'password' => Hash::make($options['pass']), + 'remember_token' => Str::random(10), + ])->save(); + + $this->info("User {$options['email']} created!"); + + return 0; + } +}