61 lines
1.5 KiB
PHP
61 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\UserRole;
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class CreateUser extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'user:create';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'maak een nieuwe gebruiker';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
$name = $this->ask('Naam van de gebruiker');
|
|
$email = $this->ask('E-mailadres');
|
|
$password = $this->secret('Wachtwoord');
|
|
|
|
$choices = collect(UserRole::cases())->map(fn($role) => $role->label())->toArray();
|
|
$names = collect(UserRole::cases())->map(fn($role) => $role->name)->toArray();
|
|
|
|
$index = $this->choice('Welke rol?', $choices); // string zoals 'Beheerder'
|
|
$name = $names[array_search($index, $choices)];
|
|
|
|
|
|
$role = UserRole::tryFromName($name);
|
|
|
|
if (!$role) {
|
|
$this->error("Ongeldige rol opgegeven.");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$user = User::create([
|
|
'name' => $name,
|
|
'email' => $email,
|
|
'password' => Hash::make($password),
|
|
'role' => $role,
|
|
]);
|
|
|
|
$this->info("Gebruiker {$user->email} aangemaakt met rol {$role->name}");
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|