MakeUserCommand.php 2.7 KB

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