diff --git a/src/Commands/MakeSuperAdminUser.php b/src/Commands/MakeSuperAdminUser.php index 183306c..798c25a 100644 --- a/src/Commands/MakeSuperAdminUser.php +++ b/src/Commands/MakeSuperAdminUser.php @@ -16,7 +16,8 @@ class MakeSuperAdminUser extends Command protected $signature = 'make:super-admin-user {--name= : The name of the user} {--email= : A valid and unique email address} - {--password= : The password for the user (min. 8 characters)}'; + {--password= : The password for the user (min. 8 characters)} + {--force : Force updating existing user}'; public $description = 'Create a super admin user'; @@ -35,18 +36,37 @@ public function handle(): int return static::SUCCESS; } + protected function getUserCheckKeys(): array { + return [ + 'email' => $this->validateInput(fn () => + $this->options['email'] ?? + $this->ask('Email address'), 'email', array_merge(['required', 'email'], $this->option('force') ? [] : ['unique:'.$this->getUserModel()]), + fn () => $this->options['email'] = null), + ]; + } + protected function getUserData(): array { return [ 'name' => $this->validateInput(fn () => $this->options['name'] ?? $this->ask('Name'), 'name', ['required'], fn () => $this->options['name'] = null), - 'email' => $this->validateInput(fn () => $this->options['email'] ?? $this->ask('Email address'), 'email', ['required', 'email', 'unique:'.$this->getUserModel()], fn () => $this->options['email'] = null), 'password' => Hash::make($this->validateInput(fn () => $this->options['password'] ?? $this->secret('Password'), 'password', ['required', 'min:8'], fn () => $this->options['password'] = null)), ]; } protected function createUser(): Authenticatable { - return static::getUserModel()::create($this->getUserData()); + $checkKeys = $this->getUserCheckKeys(); + + if ($this->option('force')) { + $query = static::getUserModel()::query(); + foreach ($checkKeys as $key => $value) { + $query = $query->where($key, $value); + } + return $query->first(); + } + + $data = array_merge($checkKeys, $this->getUserData()); + return static::getUserModel()::create($data); } protected function assignRole(Authenticatable $user): void