From b6721c8803eae99b7c23713995d9e94483118853 Mon Sep 17 00:00:00 2001 From: Chris Hills Date: Wed, 30 Apr 2025 17:13:47 +0100 Subject: [PATCH] Add an artisan command to change password --- .../Commands/ChangePasswordCommand.php | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 app/Console/Commands/ChangePasswordCommand.php diff --git a/app/Console/Commands/ChangePasswordCommand.php b/app/Console/Commands/ChangePasswordCommand.php new file mode 100644 index 00000000000..602c674c3cf --- /dev/null +++ b/app/Console/Commands/ChangePasswordCommand.php @@ -0,0 +1,84 @@ +snakeCaseOptions(); + + if (empty($details['email'])) { + $details['email'] = $this->ask('Please specify an email address'); + } + + if (empty($details['password'])) { + $details['password'] = $this->ask('Please specify a password (8 characters minimum)'); + } + + $validator = Validator::make($details, [ + 'email' => ['required', 'email', 'min:5'], + 'password' => [Password::default()], + ]); + + if ($validator->fails()) { + foreach ($validator->errors()->all() as $error) { + $this->error($error); + } + + return 1; + } + + $user = $userRepo->getByEmail($details['email']); + + if (empty($user)) { + $this->error("Could not find user!"); + return 1; + } + + $user->password = Hash::make($details['password']); + $user->save(); + + $this->info("Password for account with email \"{$user->email}\" successfully updated!"); + + return 0; + } + + protected function snakeCaseOptions(): array + { + $returnOpts = []; + foreach ($this->options() as $key => $value) { + $returnOpts[str_replace('-', '_', $key)] = $value; + } + + return $returnOpts; + } +}