Files
core/app/Console/Commands/NewUser.php

66 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\CMS\Enumerations\UserRoles;
use App\Events\CreateNewUser;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
class NewUser extends Command
{
/**
* The name and signature of the console command.
* @var string
*/
protected $signature = 'user:new {email}';
/**
* The console command description.
* @var string
*/
protected $description = 'Creates new user';
/**
* Execute the console command.
*/
public function handle()
{
do {
$email = $this->argument('email');
} while (!filter_var($email, FILTER_VALIDATE_EMAIL));
// ask for password
do {
$password = $this->secret('Password');
$passwordAgain = $this->secret('Password again');
if ($password !== $passwordAgain) {
$this->error('Passwords does not match');
}
} while ($passwordAgain !== $password || strlen($password) === 0);
// ask for role
$roles = UserRoles::getSimple();
do {
$role = $this->ask('Role (default ' . User::ROLE_ADMIN . '): ', User::ROLE_ADMIN);
// check if given role is valid
if (!in_array($role, $roles)) {
$this->error('Invalid role specified. valid roles are: ' . implode(', ', $roles));
}
} while (!in_array($role, $roles));
// create new user
$user = new User();
$user->username = $user->email = $email;
$user->password = Hash::make($password);
$user->role = $role;
$user->save();
event(new CreateNewUser($user));
$this->info('User ' . $email . ' successfully created.');
}
}