2021-06-06 18:17:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2023-02-03 18:03:57 +00:00
|
|
|
use App\Classes\PterodactylClient;
|
2021-06-06 18:17:30 +00:00
|
|
|
use App\Models\User;
|
2023-02-06 20:16:54 +00:00
|
|
|
use App\Settings\PterodactylSettings;
|
2023-01-29 22:12:34 +00:00
|
|
|
use App\Traits\Referral;
|
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;
|
2021-07-28 08:05:01 +00:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2021-06-06 18:17:30 +00:00
|
|
|
|
2021-06-06 22:52:58 +00:00
|
|
|
class MakeUserCommand extends Command
|
2021-06-06 18:17:30 +00:00
|
|
|
{
|
2023-01-29 22:12:34 +00:00
|
|
|
use Referral;
|
|
|
|
|
2023-02-06 20:16:54 +00:00
|
|
|
private $pterodactyl;
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-04-20 21:07:01 +00:00
|
|
|
public function __construct()
|
2021-06-06 18:17:30 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2023-01-29 20:01:14 +00:00
|
|
|
|
2021-06-06 18:17:30 +00:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2023-04-20 21:07:01 +00:00
|
|
|
public function handle(PterodactylSettings $ptero_settings)
|
2021-06-06 21:04:04 +00:00
|
|
|
{
|
2023-04-20 21:07:01 +00:00
|
|
|
$this->pterodactyl = new PterodactylClient($ptero_settings);
|
2021-06-06 18:17:30 +00:00
|
|
|
$ptero_id = $this->option('ptero_id') ?? $this->ask('Please specify your Pterodactyl ID.');
|
2021-08-02 15:52:26 +00:00
|
|
|
$password = $this->secret('password') ?? $this->ask('Please specify your password.');
|
2021-06-06 21:04:04 +00:00
|
|
|
|
2021-07-28 08:05:01 +00:00
|
|
|
// Validate user input
|
|
|
|
$validator = Validator::make([
|
|
|
|
'ptero_id' => $ptero_id,
|
|
|
|
'password' => $password,
|
|
|
|
], [
|
|
|
|
'ptero_id' => 'required|numeric|integer|min:1|max:2147483647',
|
|
|
|
'password' => 'required|string|min:8|max:60',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
$this->error($validator->errors()->first());
|
2023-01-05 17:01:42 +00:00
|
|
|
|
2021-06-09 18:53:58 +00:00
|
|
|
return 0;
|
2021-06-06 21:04:04 +00:00
|
|
|
}
|
2021-06-06 18:17:30 +00:00
|
|
|
|
2021-06-09 18:53:58 +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
|
|
|
|
2021-07-28 08:05:01 +00:00
|
|
|
if (isset($response['errors'])) {
|
2023-01-05 17:01:42 +00:00
|
|
|
if (isset($response['errors'][0]['code'])) {
|
|
|
|
$this->error("code: {$response['errors'][0]['code']}");
|
|
|
|
}
|
|
|
|
if (isset($response['errors'][0]['status'])) {
|
|
|
|
$this->error("status: {$response['errors'][0]['status']}");
|
|
|
|
}
|
|
|
|
if (isset($response['errors'][0]['detail'])) {
|
|
|
|
$this->error("detail: {$response['errors'][0]['detail']}");
|
|
|
|
}
|
|
|
|
|
2021-06-09 18:53:58 +00:00
|
|
|
return 0;
|
2021-06-06 21:04:04 +00:00
|
|
|
}
|
2021-06-06 18:17:30 +00:00
|
|
|
$user = User::create([
|
2021-07-28 08:05:01 +00:00
|
|
|
'name' => $response['first_name'],
|
|
|
|
'email' => $response['email'],
|
|
|
|
'role' => 'admin',
|
|
|
|
'password' => Hash::make($password),
|
2023-01-29 22:12:34 +00:00
|
|
|
'referral_code' => $this->createReferralCode(),
|
2023-01-05 17:01:42 +00:00
|
|
|
'pterodactyl_id' => $response['id'],
|
2021-06-06 18:17:30 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->table(['Field', 'Value'], [
|
|
|
|
['ID', $user->id],
|
|
|
|
['Email', $user->email],
|
|
|
|
['Username', $user->name],
|
|
|
|
['Ptero-ID', $user->pterodactyl_id],
|
|
|
|
['Admin', $user->role],
|
2023-01-29 20:01:14 +00:00
|
|
|
['Referral code', $user->referral_code],
|
2021-06-06 18:17:30 +00:00
|
|
|
]);
|
2021-06-06 21:04:04 +00:00
|
|
|
|
2021-06-09 18:53:58 +00:00
|
|
|
return 1;
|
2021-06-06 18:17:30 +00:00
|
|
|
}
|
|
|
|
}
|