MakeUserCommand.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. class MakeUserCommand extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'make:user {--ptero_id=} {--password=}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Create an admin account with the Artisan Console';
  21. private Pterodactyl $pterodactyl;
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct(Pterodactyl $pterodactyl)
  28. {
  29. parent::__construct();
  30. $this->pterodactyl = $pterodactyl;
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. $ptero_id = $this->option('ptero_id') ?? $this->ask('Please specify your Pterodactyl ID.');
  40. $password = $this->option('password') ?? $this->ask('Please specify your password.');
  41. if (strlen($password) < 8) {
  42. $this->alert('Your password need to be at least 8 characters long');
  43. return 0;
  44. }
  45. //TODO: Do something with response (check for status code and give hints based upon that)
  46. $response = $this->pterodactyl->getUser($ptero_id);
  47. if ($response === []) {
  48. $this->alert('It seems that your Pterodactyl ID is not correct. Rerun the command and input an correct ID');
  49. return 0;
  50. }
  51. $user = User::create([
  52. 'name' => $response['first_name'],
  53. 'email' => $response['email'],
  54. 'role' => 'admin',
  55. 'password' => Hash::make($password),
  56. 'pterodactyl_id' => $response['id']
  57. ]);
  58. $this->table(['Field', 'Value'], [
  59. ['ID', $user->id],
  60. ['Email', $user->email],
  61. ['Username', $user->name],
  62. ['Ptero-ID', $user->pterodactyl_id],
  63. ['Admin', $user->role],
  64. ]);
  65. return 1;
  66. }
  67. }