231 lines
6.1 KiB
PHP
231 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\Users\RoleEnum;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class UserCreate extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'uwil:user:create';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Create a backend admin user';
|
|
|
|
/**
|
|
* The first name of the new user.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $first_name;
|
|
|
|
/**
|
|
* The last name of the new user.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $last_name;
|
|
|
|
/**
|
|
* The email of the new user.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $email;
|
|
|
|
/**
|
|
* The password of the new user.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $password;
|
|
|
|
/**
|
|
* The role of the new user.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $role;
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
$this->first_name = null;
|
|
$this->addFirstName();
|
|
|
|
$this->last_name = null;
|
|
$this->addLastName();
|
|
|
|
$this->email = null;
|
|
$this->addEmail();
|
|
|
|
$this->password = null;
|
|
$this->addPassword();
|
|
|
|
$this->role = null;
|
|
$this->addRole();
|
|
|
|
$user = new User([
|
|
'first_name' => $this->first_name,
|
|
'last_name' => $this->last_name,
|
|
'email' => $this->email,
|
|
'password' => $this->password,
|
|
'role' => $this->role,
|
|
'published' => true,
|
|
'published_at' => Carbon::now(),
|
|
]);
|
|
$user->saveOrFail();
|
|
|
|
$this->info('User created 👌');
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Add a first name to the user.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function addFirstName(): void
|
|
{
|
|
while (is_null($this->first_name)) {
|
|
try {
|
|
$tmp = $this->ask('Type the wanted user first name', 'Admin');
|
|
$validator = Validator::make(['first_name' => $tmp], [
|
|
'first_name' => 'required|min:3|max:255'
|
|
]);
|
|
$validator->validate();
|
|
$this->first_name = $tmp;
|
|
} catch (ValidationException $e) {
|
|
$this->error(sprintf(
|
|
'Please provide a valid first name %s',
|
|
\implode(',', collect($e->errors())->flatten()->all())
|
|
));
|
|
continue;
|
|
} //end try
|
|
}; //end while
|
|
}
|
|
|
|
/**
|
|
* Add a last name to the user.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function addLastName(): void
|
|
{
|
|
while (is_null($this->last_name)) {
|
|
try {
|
|
$tmp = $this->ask('Type the wanted user last name', 'Saïtama');
|
|
$validator = Validator::make(['last_name' => $tmp], [
|
|
'last_name' => 'required|min:3|max:255'
|
|
]);
|
|
$validator->validate();
|
|
$this->last_name = $tmp;
|
|
} catch (ValidationException $e) {
|
|
$this->error(sprintf(
|
|
'Please provide a valid last_name %s',
|
|
\implode(',', collect($e->errors())->flatten()->all())
|
|
));
|
|
continue;
|
|
} //end try
|
|
}; //end while
|
|
}
|
|
|
|
/**
|
|
* Add an email to the user.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function addEmail(): void
|
|
{
|
|
while (is_null($this->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();
|
|
$this->email = $tmp;
|
|
} catch (ValidationException $e) {
|
|
$this->error(sprintf(
|
|
'Please provide a valid email %s',
|
|
\implode(',', collect($e->errors())->flatten()->all())
|
|
));
|
|
continue;
|
|
} //end try
|
|
}; //end while
|
|
}
|
|
|
|
/**
|
|
* Add a password to the user.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function addPassword(): void
|
|
{
|
|
while (
|
|
($this->password = $this->secret(
|
|
(!\is_null($this->password) ? 'Failed to confirm, ' : '') . 'Type the wanted user password'
|
|
)) !== $this->secret('Confirm the password')
|
|
) {
|
|
continue;
|
|
}; //end while
|
|
}
|
|
|
|
/**
|
|
* Add a role to the user.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function addRole(): void
|
|
{
|
|
while (is_null($this->role)) {
|
|
try {
|
|
$tmp = $this->choice(
|
|
'Select his role',
|
|
[RoleEnum::conceptor->label(), RoleEnum::admin->label(), RoleEnum::visitor->label()],
|
|
RoleEnum::visitor->label(),
|
|
$maxAttempts = null,
|
|
$allowMultipleSelections = false
|
|
);
|
|
if ($tmp === RoleEnum::conceptor->label()) {
|
|
$tmp = RoleEnum::conceptor;
|
|
}
|
|
if ($tmp === RoleEnum::admin->label()) {
|
|
$tmp = RoleEnum::admin;
|
|
}
|
|
if ($tmp === RoleEnum::visitor->label()) {
|
|
$tmp = RoleEnum::visitor;
|
|
}
|
|
$validator = Validator::make(['role' => $tmp], [
|
|
'role' => 'required'
|
|
]);
|
|
$validator->validate();
|
|
$this->role = $tmp;
|
|
} catch (ValidationException $e) {
|
|
$this->error(sprintf(
|
|
'Please choose from the selection',
|
|
\implode(',', collect($e->errors())->flatten()->all())
|
|
));
|
|
continue;
|
|
} //end try
|
|
}; //end while
|
|
}
|
|
}
|