Ajout de la commande make:admin
This commit is contained in:
60
app/Console/Commands/CreateAdmin.php
Normal file
60
app/Console/Commands/CreateAdmin.php
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?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 !");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user