Files
RecruIT/app/Console/Commands/CreateAdmin.php
2026-03-20 09:15:25 +01:00

61 lines
1.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
class CreateAdmin extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:admin {email?} {--password=}';
/**
* The description of the console command.
*
* @var string
*/
protected $description = 'Créer un utilisateur administrateur';
/**
* Execute the console command.
*/
public function handle()
{
$email = $this->argument('email');
if (!$email) {
$email = $this->ask('Email de l\'administrateur ?');
}
if (User::where('email', $email)->exists()) {
$this->error('Cet email est déjà utilisé !');
return;
}
$name = $this->ask('Nom de l\'administrateur ?', 'Administrateur');
$password = $this->option('password');
if (!$password) {
$password = $this->secret('Mot de passe de l\'administrateur ?');
if (!$password) {
$this->error('Le mot de passe ne peut pas être vide.');
return;
}
}
User::create([
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
'role' => 'admin',
]);
$this->info("L'administrateur $name ($email) a été créé avec succès !");
}
}