90 lines
2.6 KiB
PHP
90 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
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
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'create:user';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Create a user';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function handle()
|
|
{
|
|
$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;
|
|
}
|
|
}
|
|
$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;
|
|
}
|
|
}
|