2021-07-01 12:33:52 +00:00
< ? php
namespace App\Console\Commands ;
use App\Models\User ;
use Illuminate\Console\Command ;
use Illuminate\Support\Facades\Storage ;
class ImportUsersFromPteroCommand extends Command
{
/**
* @ var string
*/
private $importFileName = 'users.json' ;
2023-01-05 17:01:42 +00:00
2021-07-01 12:33:52 +00:00
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = 'import:users {--initial_credits=} {--initial_server_limit=} {--confirm=}' ;
/**
* The console command description .
*
* @ var string
*/
protected $description = 'Command description' ;
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
2023-01-05 17:01:42 +00:00
* @ return bool
2021-07-01 12:33:52 +00:00
*/
public function handle ()
{
//check if json file exists
2023-01-05 17:01:42 +00:00
if ( ! Storage :: disk ( 'local' ) -> exists ( 'users.json' )) {
$this -> error ( '[ERROR] ' . storage_path ( 'app' ) . '/' . $this -> importFileName . ' is missing' );
2021-07-01 12:33:52 +00:00
return false ;
}
//check if json file is valid
$json = json_decode ( Storage :: disk ( 'local' ) -> get ( 'users.json' ));
2023-01-05 17:01:42 +00:00
if ( ! array_key_exists ( 2 , $json )) {
2021-07-01 12:33:52 +00:00
$this -> error ( '[ERROR] Invalid json file' );
2023-01-05 17:01:42 +00:00
2021-07-01 12:33:52 +00:00
return false ;
}
2023-01-05 17:01:42 +00:00
if ( ! $json [ 2 ] -> data ) {
2021-07-01 12:33:52 +00:00
$this -> error ( '[ERROR] Invalid json file / No users found!' );
2023-01-05 17:01:42 +00:00
2021-07-01 12:33:52 +00:00
return false ;
}
//ask questions :)
$initial_credits = $this -> option ( 'initial_credits' ) ? ? $this -> ask ( 'Please specify the amount of starting credits users should get. ' );
$initial_server_limit = $this -> option ( 'initial_server_limit' ) ? ? $this -> ask ( 'Please specify the initial server limit users should get.' );
$confirm = strtolower ( $this -> option ( 'confirm' ) ? ? $this -> ask ( '[y/n] Are you sure you want to remove all existing users from the database continue importing?' ));
//cancel
if ( $confirm !== 'y' ) {
$this -> error ( '[ERROR] Stopped import script!' );
2023-01-05 17:01:42 +00:00
2021-07-01 12:33:52 +00:00
return false ;
}
//import users
$this -> deleteCurrentUserBase ();
$this -> importUsingJsonFile ( $json , $initial_credits , $initial_server_limit );
2023-01-05 17:01:42 +00:00
2021-07-01 12:33:52 +00:00
return true ;
}
/**
* @ return void
*/
private function deleteCurrentUserBase ()
{
$currentUserCount = User :: count ();
2023-01-05 17:01:42 +00:00
if ( $currentUserCount == 0 ) {
return ;
}
2021-07-01 12:33:52 +00:00
$this -> line ( " Deleting ( { $currentUserCount } ) users.. " );
foreach ( User :: all () as $user ) {
$user -> delete ();
}
}
/**
* @ param $json
* @ param $initial_credits
* @ param $initial_server_limit
* @ return void
*/
private function importUsingJsonFile ( $json , $initial_credits , $initial_server_limit )
{
$this -> withProgressBar ( $json [ 2 ] -> data , function ( $user ) use ( $initial_server_limit , $initial_credits ) {
$role = $user -> root_admin == '0' ? 'member' : 'admin' ;
User :: create ([
2023-01-05 17:01:42 +00:00
'pterodactyl_id' => $user -> id ,
'name' => $user -> name_first ,
'email' => $user -> email ,
'password' => $user -> password ,
'role' => $role ,
'credits' => $initial_credits ,
'server_limit' => $initial_server_limit ,
'created_at' => $user -> created_at ,
'updated_at' => $user -> updated_at ,
2021-07-01 12:33:52 +00:00
]);
});
$this -> newLine ();
2023-01-05 17:01:42 +00:00
$this -> line ( 'Done importing, you can now login using your pterodactyl credentials.' );
2021-07-01 12:33:52 +00:00
$this -> newLine ();
}
}