ctrlpanel/app/Console/Commands/MakeUserCommand.php

81 lines
2.1 KiB
PHP
Raw Normal View History

2021-06-06 18:17:30 +00:00
<?php
namespace App\Console\Commands;
use App\Classes\Pterodactyl;
use App\Models\User;
2021-06-06 21:04:04 +00:00
use Illuminate\Console\Command;
2021-06-06 18:17:30 +00:00
use Illuminate\Support\Facades\Hash;
class MakeUserCommand extends Command
2021-06-06 18:17:30 +00:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:user {--ptero_id=} {--password=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create an admin account with the Artisan Console';
private Pterodactyl $pterodactyl;
2021-06-06 18:17:30 +00:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Pterodactyl $pterodactyl)
2021-06-06 18:17:30 +00:00
{
parent::__construct();
$this->pterodactyl = $pterodactyl;
2021-06-06 18:17:30 +00:00
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
2021-06-06 21:04:04 +00:00
{
2021-06-06 18:17:30 +00:00
$ptero_id = $this->option('ptero_id') ?? $this->ask('Please specify your Pterodactyl ID.');
$password = $this->option('password') ?? $this->ask('Please specify your password.');
2021-06-06 21:04:04 +00:00
if (strlen($password) < 8) {
$this->alert('Your password need to be at least 8 characters long');
return 0;
2021-06-06 21:04:04 +00:00
}
2021-06-06 18:17:30 +00:00
//TODO: Do something with response (check for status code and give hints based upon that)
$response = $this->pterodactyl->getUser($ptero_id);
2021-06-06 18:17:30 +00:00
if ($response === []) {
$this->alert('It seems that your Pterodactyl ID is not correct. Rerun the command and input an correct ID');
return 0;
2021-06-06 21:04:04 +00:00
}
2021-06-06 18:17:30 +00:00
$user = User::create([
2021-06-06 21:04:04 +00:00
'name' => $response['first_name'],
'email' => $response['email'],
'role' => 'admin',
'password' => Hash::make($password),
2021-06-06 18:17:30 +00:00
'pterodactyl_id' => $response['id']
]);
$this->table(['Field', 'Value'], [
['ID', $user->id],
['Email', $user->email],
['Username', $user->name],
['Ptero-ID', $user->pterodactyl_id],
['Admin', $user->role],
]);
2021-06-06 21:04:04 +00:00
return 1;
2021-06-06 18:17:30 +00:00
}
}