MakeUserCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Classes\PterodactylClient;
  4. use App\Models\User;
  5. use App\Settings\PterodactylSettings;
  6. use App\Traits\Referral;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Hash;
  9. use Illuminate\Support\Facades\Validator;
  10. class MakeUserCommand extends Command
  11. {
  12. use Referral;
  13. private $pterodactyl;
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'make:user {--ptero_id=} {--password=}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Create an admin account with the Artisan Console';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle(PterodactylSettings $ptero_settings)
  41. {
  42. $this->pterodactyl = new PterodactylClient($ptero_settings);
  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. $user->syncRoles(1);
  88. return 1;
  89. }
  90. }