Skip to content

Commit

Permalink
Add force option while make super-admin [Ref: 7e21da4]
Browse files Browse the repository at this point in the history
  • Loading branch information
cklei-carly committed Jul 31, 2023
1 parent 8783d66 commit fc8b394
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Commands/MakeSuperAdminUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
Expand Down

0 comments on commit fc8b394

Please sign in to comment.