2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
namespace Modules\Importer\Services\Importers;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
use App\Facades\Utils;
|
|
|
|
use App\Models\Enums\UserState;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\UserService;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Str;
|
2019-12-13 04:07:35 +08:00
|
|
|
use Modules\Importer\Services\BaseImporter;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
class UserImport extends BaseImporter
|
|
|
|
{
|
2019-12-02 22:57:35 +08:00
|
|
|
protected $table = 'pilots';
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
/**
|
|
|
|
* @var UserService
|
|
|
|
*/
|
|
|
|
private $userSvc;
|
|
|
|
|
|
|
|
public function run($start = 0)
|
|
|
|
{
|
|
|
|
$this->comment('--- USER IMPORT ---');
|
|
|
|
|
|
|
|
$this->userSvc = app(UserService::class);
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$first_row = true;
|
2019-12-02 22:57:35 +08:00
|
|
|
foreach ($this->db->readRows($this->table, $start) as $row) {
|
2019-11-27 22:19:20 +08:00
|
|
|
$pilot_id = $row->pilotid; // This isn't their actual ID
|
|
|
|
$name = $row->firstname.' '.$row->lastname;
|
|
|
|
|
|
|
|
// Figure out which airline, etc, they belong to
|
|
|
|
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
|
|
|
|
// Log::info('User airline from '.$row->code.' to ID '.$airline_id);
|
|
|
|
|
|
|
|
$rank_id = $this->idMapper->getMapping('ranks', $row->rank);
|
|
|
|
$state = $this->getUserState($row->retired);
|
|
|
|
|
|
|
|
if ($first_row) {
|
|
|
|
$new_password = 'admin';
|
|
|
|
$first_row = false;
|
|
|
|
} else {
|
|
|
|
$new_password = Str::random(60);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for a user with that pilot ID already. If someone has it
|
|
|
|
if ($this->userSvc->isPilotIdAlreadyUsed($pilot_id)) {
|
|
|
|
Log::info('User with pilot id '.$pilot_id.' exists, reassigning');
|
|
|
|
$pilot_id = $this->userSvc->getNextAvailablePilotId();
|
|
|
|
}
|
|
|
|
|
|
|
|
$attrs = [
|
|
|
|
'pilot_id' => $pilot_id,
|
|
|
|
'name' => $name,
|
|
|
|
'password' => Hash::make($new_password),
|
|
|
|
'api_key' => Utils::generateApiKey(),
|
|
|
|
'airline_id' => $airline_id,
|
|
|
|
'rank_id' => $rank_id,
|
|
|
|
'home_airport_id' => $row->hub,
|
|
|
|
'curr_airport_id' => $row->hub,
|
|
|
|
'flights' => (int) $row->totalflights,
|
|
|
|
'flight_time' => Utils::hoursToMinutes($row->totalhours),
|
|
|
|
'state' => $state,
|
|
|
|
'created_at' => $this->parseDate($row->joindate),
|
|
|
|
];
|
|
|
|
|
|
|
|
$user = User::updateOrCreate(['email' => $row->email], $attrs);
|
|
|
|
$this->idMapper->addMapping('users', $row->pilotid, $user->id);
|
|
|
|
|
|
|
|
$this->updateUserRoles($user, $row->pilotid);
|
|
|
|
|
|
|
|
if ($user->wasRecentlyCreated) {
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Imported '.$count.' users');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a user's roles and add them to the proper ones
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param string $old_pilot_id
|
|
|
|
*/
|
|
|
|
protected function updateUserRoles(User $user, $old_pilot_id)
|
|
|
|
{
|
|
|
|
// Be default add them to the user role, and then determine if they
|
|
|
|
// belong to any other groups, and add them to that
|
|
|
|
$this->userSvc->addUserToRole($user, 'user');
|
|
|
|
|
|
|
|
// Figure out what other groups they belong to
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user's new state from their original state
|
|
|
|
*
|
|
|
|
* @param $state
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getUserState($state)
|
|
|
|
{
|
|
|
|
// Return active for now, let the stats/cron determine the status later
|
|
|
|
return UserState::ACTIVE;
|
|
|
|
/*$state = (int) $state;
|
|
|
|
|
|
|
|
// Declare array of classic states
|
|
|
|
$phpvms_classic_states = [
|
|
|
|
'ACTIVE' => 0,
|
|
|
|
'INACTIVE' => 1,
|
|
|
|
'BANNED' => 2,
|
|
|
|
'ON_LEAVE' => 3,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Decide which state they will be in accordance with v7
|
|
|
|
if ($state === $phpvms_classic_states['ACTIVE']) {
|
|
|
|
return UserState::ACTIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($state === $phpvms_classic_states['INACTIVE']) {
|
|
|
|
// TODO: Make an inactive state?
|
|
|
|
return UserState::REJECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($state === $phpvms_classic_states['BANNED']) {
|
|
|
|
return UserState::SUSPENDED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($state === $phpvms_classic_states['ON_LEAVE']) {
|
|
|
|
return UserState::ON_LEAVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->error('Unknown status: '.$state);
|
|
|
|
|
|
|
|
return UserState::ACTIVE;*/
|
|
|
|
}
|
|
|
|
}
|