117 lines
3 KiB
PHP
117 lines
3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\User;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* Creates a new user
|
|
*/
|
|
class CreateAdmin extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'user:create';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Create a backend admin user';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
$email = $this->getInput(
|
|
'Type the wanted user email',
|
|
'email',
|
|
'required|ends_with:@ldc.fr|unique:users,email|email:rfc,dns',
|
|
'equipe.web@ldc.fr'
|
|
);
|
|
|
|
$firstname = $this->getInput(
|
|
'Type the wanted user firstname',
|
|
'firstname',
|
|
'required|min:5,max:255',
|
|
'Saitama'
|
|
);
|
|
|
|
$lastname = $this->getInput(
|
|
'Type the wanted user lastname',
|
|
'lastname',
|
|
'required|min:5,max:255',
|
|
'Hagemanto'
|
|
);
|
|
|
|
$phone = $this->getInput(
|
|
'Type the wanted user phone',
|
|
'phone',
|
|
'required|phone:FR|unique:users,phone|min:5,max:255'
|
|
);
|
|
|
|
$password = null;
|
|
while (
|
|
($password = $this->secret(
|
|
(!\is_null($password) ? 'Failed to confirm, ' : '') . 'Type the wanted user password'
|
|
)) !== $this->secret('Confirm the password')
|
|
) {
|
|
continue;
|
|
};
|
|
|
|
$admin = new User([
|
|
'firstname' => $firstname,
|
|
'lastname' => $lastname,
|
|
'email' => $email,
|
|
'phone' => $phone,
|
|
'password' => $password,
|
|
'role' => $this->confirm('Is the user an member of equipe web ?')
|
|
]);
|
|
$admin->saveOrFail();
|
|
|
|
$this->info('User created.');
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Get User Input
|
|
*
|
|
* @param string $message
|
|
* @param string $inputName
|
|
* @param string|array $rules
|
|
* @param string|null $defaultValue
|
|
* @return mixed
|
|
*/
|
|
private function getInput(string $message, string $inputName, $rules, ?string $defaultValue = null)
|
|
{
|
|
$out = null;
|
|
while (is_null($out)) {
|
|
try {
|
|
$tmp = $this->ask($message, $defaultValue);
|
|
$validator = Validator::make([$inputName => $tmp], [
|
|
$inputName => $rules
|
|
]);
|
|
$validator->validate();
|
|
$out = $tmp;
|
|
} catch (ValidationException $e) {
|
|
$this->error(sprintf(
|
|
"'Please provide a valid $inputName %s'",
|
|
\implode(',', collect($e->errors())->flatten()->all())
|
|
));
|
|
continue;
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
}
|