Bump create user command

This commit is contained in:
Jérémy Munsch 2022-08-11 09:36:40 +02:00
parent 9dc1aa728e
commit f25b458c97
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC

View file

@ -6,6 +6,8 @@ use App\Enums\Role;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class CreateUser extends Command
{
@ -30,19 +32,57 @@ class CreateUser extends Command
*/
public function handle()
{
$name = $this->ask('What is the name?');
$email = $this->ask('What is the email?');
$pwd = $this->secret('What is the password?');
$user = new User();
$user->name = $name;
$user->email = $email;
if ($this->confirm('Is the user an admin?')) {
$user->role = Role::admin();
} else {
$user->role = Role::client();
$email = null;
while (is_null($email)) {
try {
$tmp = $this->ask('Type the wanted user email', 'equipe.web@ldc.fr');
$validator = Validator::make(['email' => $tmp], [
'email' => 'required|unique:users,email|email:rfc,dns'
]);
$validator->validate();
$email = $tmp;
} catch (ValidationException $e) {
$this->error(sprintf(
'Please provide a valid email %s',
\implode(',', collect($e->errors())->flatten()->all())
));
continue;
}
}
$user->password = Hash::make($pwd);
$user->save();
$name = null;
while (is_null($name)) {
try {
$tmp = $this->ask('Type the wanted user name', 'Admin');
$validator = Validator::make(['name' => $tmp], [
'name' => 'required|min:5,max:255'
]);
$validator->validate();
$name = $tmp;
} catch (ValidationException $e) {
$this->error(sprintf(
'Please provide a valid name %s',
\implode(',', collect($e->errors())->flatten()->all())
));
continue;
}
}
$password = null;
while (
($password = $this->secret(
(!\is_null($password) ? 'Failed to confirm, ' : '') . 'Type the wanted user password'
)) !== $this->secret('Confirm the password')
) {
continue;
};
$user = new User();
$user->name = $name;
$user->email = $email;
$user->role = $this->confirm('Is the user an admin?') ? Role::admin() : Role::client();
$user->password = Hash::make($password);
$user->saveOrFail();
$this->info('User created.');
return 0;