ImportUsersFromPteroCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\User;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Storage;
  6. class ImportUsersFromPteroCommand extends Command
  7. {
  8. /**
  9. * @var string
  10. */
  11. private $importFileName = 'users.json';
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'import:users {--initial_credits=} {--initial_server_limit=} {--confirm=}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return bool
  37. */
  38. public function handle()
  39. {
  40. //check if json file exists
  41. if (! Storage::disk('local')->exists('users.json')) {
  42. $this->error('[ERROR] '.storage_path('app').'/'.$this->importFileName.' is missing');
  43. return false;
  44. }
  45. //check if json file is valid
  46. $json = json_decode(Storage::disk('local')->get('users.json'));
  47. if (! array_key_exists(2, $json)) {
  48. $this->error('[ERROR] Invalid json file');
  49. return false;
  50. }
  51. if (! $json[2]->data) {
  52. $this->error('[ERROR] Invalid json file / No users found!');
  53. return false;
  54. }
  55. //ask questions :)
  56. $initial_credits = $this->option('initial_credits') ?? $this->ask('Please specify the amount of starting credits users should get. ');
  57. $initial_server_limit = $this->option('initial_server_limit') ?? $this->ask('Please specify the initial server limit users should get.');
  58. $confirm = strtolower($this->option('confirm') ?? $this->ask('[y/n] Are you sure you want to remove all existing users from the database continue importing?'));
  59. //cancel
  60. if ($confirm !== 'y') {
  61. $this->error('[ERROR] Stopped import script!');
  62. return false;
  63. }
  64. //import users
  65. $this->deleteCurrentUserBase();
  66. $this->importUsingJsonFile($json, $initial_credits, $initial_server_limit);
  67. return true;
  68. }
  69. /**
  70. * @return void
  71. */
  72. private function deleteCurrentUserBase()
  73. {
  74. $currentUserCount = User::count();
  75. if ($currentUserCount == 0) {
  76. return;
  77. }
  78. $this->line("Deleting ({$currentUserCount}) users..");
  79. foreach (User::all() as $user) {
  80. $user->delete();
  81. }
  82. }
  83. /**
  84. * @param $json
  85. * @param $initial_credits
  86. * @param $initial_server_limit
  87. * @return void
  88. */
  89. private function importUsingJsonFile($json, $initial_credits, $initial_server_limit)
  90. {
  91. $this->withProgressBar($json[2]->data, function ($user) use ($initial_server_limit, $initial_credits) {
  92. $role = $user->root_admin == '0' ? 'member' : 'admin';
  93. User::create([
  94. 'pterodactyl_id' => $user->id,
  95. 'name' => $user->name_first,
  96. 'email' => $user->email,
  97. 'password' => $user->password,
  98. 'role' => $role,
  99. 'credits' => $initial_credits,
  100. 'server_limit' => $initial_server_limit,
  101. 'created_at' => $user->created_at,
  102. 'updated_at' => $user->updated_at,
  103. ]);
  104. });
  105. $this->newLine();
  106. $this->line('Done importing, you can now login using your pterodactyl credentials.');
  107. $this->newLine();
  108. }
  109. }