MakeUserCommand.php 2.7 KB

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