MakeUserCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Classes\Pterodactyl;
  4. use App\Models\User;
  5. use App\Traits\Referral;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Hash;
  8. use Illuminate\Support\Facades\Validator;
  9. use Illuminate\Support\Str;
  10. class MakeUserCommand extends Command
  11. {
  12. use Referral;
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'make:user {--ptero_id=} {--password=}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Create an admin account with the Artisan Console';
  25. private Pterodactyl $pterodactyl;
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(Pterodactyl $pterodactyl)
  32. {
  33. parent::__construct();
  34. $this->pterodactyl = $pterodactyl;
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. $ptero_id = $this->option('ptero_id') ?? $this->ask('Please specify your Pterodactyl ID.');
  44. $password = $this->secret('password') ?? $this->ask('Please specify your password.');
  45. // Validate user input
  46. $validator = Validator::make([
  47. 'ptero_id' => $ptero_id,
  48. 'password' => $password,
  49. ], [
  50. 'ptero_id' => 'required|numeric|integer|min:1|max:2147483647',
  51. 'password' => 'required|string|min:8|max:60',
  52. ]);
  53. if ($validator->fails()) {
  54. $this->error($validator->errors()->first());
  55. return 0;
  56. }
  57. //TODO: Do something with response (check for status code and give hints based upon that)
  58. $response = $this->pterodactyl->getUser($ptero_id);
  59. if (isset($response['errors'])) {
  60. if (isset($response['errors'][0]['code'])) {
  61. $this->error("code: {$response['errors'][0]['code']}");
  62. }
  63. if (isset($response['errors'][0]['status'])) {
  64. $this->error("status: {$response['errors'][0]['status']}");
  65. }
  66. if (isset($response['errors'][0]['detail'])) {
  67. $this->error("detail: {$response['errors'][0]['detail']}");
  68. }
  69. return 0;
  70. }
  71. $user = User::create([
  72. 'name' => $response['first_name'],
  73. 'email' => $response['email'],
  74. 'role' => 'admin',
  75. 'password' => Hash::make($password),
  76. 'referral_code' => $this->createReferralCode(),
  77. 'pterodactyl_id' => $response['id'],
  78. ]);
  79. $this->table(['Field', 'Value'], [
  80. ['ID', $user->id],
  81. ['Email', $user->email],
  82. ['Username', $user->name],
  83. ['Ptero-ID', $user->pterodactyl_id],
  84. ['Admin', $user->role],
  85. ['Referral code', $user->referral_code],
  86. ]);
  87. return 1;
  88. }
  89. }