109 lines
3.4 KiB
PHP
109 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\UserRole;
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
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 void
|
|
*/
|
|
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,spoof'
|
|
]);
|
|
$validator->validate();
|
|
$email = $tmp;
|
|
} catch (ValidationException $e) {
|
|
$this->error(sprintf(
|
|
'Please provide a valid email %s',
|
|
\implode(',', collect($e->errors())->flatten()->all())
|
|
));
|
|
continue;
|
|
}
|
|
}
|
|
$firstname = null;
|
|
while (is_null($firstname)) {
|
|
try {
|
|
$tmp = $this->ask('Type the wanted user firstname', 'Admin');
|
|
$validator = Validator::make(['firstname' => $tmp], [
|
|
'firstname' => 'required|min:5,max:255'
|
|
]);
|
|
$validator->validate();
|
|
$firstname = $tmp;
|
|
} catch (ValidationException $e) {
|
|
$this->error(sprintf(
|
|
'Please provide a valid firstname %s',
|
|
\implode(',', collect($e->errors())->flatten()->all())
|
|
));
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$lastname = null;
|
|
while (is_null($lastname)) {
|
|
try {
|
|
$tmp = $this->ask('Type the wanted user lastname', 'Saïtama');
|
|
$validator = Validator::make(['lastname' => $tmp], [
|
|
'lastname' => 'required|min:5,max:255'
|
|
]);
|
|
$validator->validate();
|
|
$lastname = $tmp;
|
|
} catch (ValidationException $e) {
|
|
$this->error(sprintf(
|
|
'Please provide a valid lastname %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;
|
|
}; //end while
|
|
|
|
$user = new User();
|
|
$user->firstname = $firstname;
|
|
$user->lastname = $lastname;
|
|
$user->date_of_birth = now();
|
|
$user->email = $email;
|
|
$user->password = $password;
|
|
$user->role = $this->confirm('Is the user an admin?') ?
|
|
UserRole::admin() : UserRole::user();
|
|
$user->email_verified_at = now();
|
|
$user->saveOrFail();
|
|
|
|
$this->info('User created.');
|
|
}
|
|
}
|