Files
isop-mirror/backend/app/Console/Commands/CreateGarant.php
Andrej f55b8bc580 Create CreateGarant.php
Vytvorenie CLi príkazu na pridanie Granata (ADMINA) do databázy.
2025-11-03 12:10:34 +01:00

60 lines
1.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class CreateGarant extends Command
{
/**
* Názov CLI príkazu.
*/
protected $signature = 'user:create-garant';
/**
* Popis príkazu.
*/
protected $description = 'Interaktívne vytvorí nového používateľa s rolou admin (garant)';
/**
* Spustenie príkazu.
* php artisan user:create-garant
*/
public function handle()
{
$this->info('=== Vytvorenie garanta (admin) ===');
// Načítanie údajov interaktívne
$firstName = $this->ask('Zadaj krstné meno');
$lastName = $this->ask('Zadaj priezvisko');
$email = $this->ask('Zadaj email');
// Kontrola duplicity emailu
if (User::where('email', $email)->exists()) {
$this->error('Používateľ s týmto emailom už existuje.');
return 1;
}
$password = $this->secret('Zadaj heslo (nebude sa zobrazovať)');
$phone = $this->ask('Zadaj telefón ');
// Vytvorenie používateľa
$user = User::create([
'name' => $firstName . ' ' . $lastName,
'first_name' => $firstName,
'last_name' => $lastName,
'email' => $email,
'phone' => $phone,
'role' => 'ADMIN',
'password' => Hash::make($password),
]);
$this->info("\n Garant {$user->first_name} {$user->last_name} bol úspešne vytvorený s rolou ADMIN.");
$this->info("Email: {$user->email}");
return 0;
}
}