91 lines
2.4 KiB
PHP
91 lines
2.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use App\Models\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 = 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;
|
||
|
|
};
|
||
|
|
|
||
|
|
$admin = new User([
|
||
|
|
'name' => $name,
|
||
|
|
'email' => $email,
|
||
|
|
'password' => $password
|
||
|
|
]);
|
||
|
|
$admin->saveOrFail();
|
||
|
|
|
||
|
|
$this->info('User created.');
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|