Fixed a few field entries (#116)

* Stopped inheritance errors popping up

* Added fillable fields

These would not save otherwise.

* Added country fillable field

Wouldn’t save when importing from phpvms classic.

* Added more to classic importer

Change arguments to ask in terminal
Fixed table_prefix names in Importer.php

Added the ability to import users from phpvms classic (tested on
simpilot’s 5.5.x) and when importing, it will then reset the user’s
password to a temporary hash and then email it to the user to then
change when they first log in.

* Changes to ImporterService
This commit is contained in:
web541 2018-01-04 08:41:21 +11:00 committed by Nabeel Shahzad
parent be6e5e8dec
commit 08df20de19
7 changed files with 157 additions and 9 deletions

View File

@ -7,7 +7,7 @@ use App\Console\BaseCommand;
class Importer extends BaseCommand
{
protected $signature = 'phpvms:importer {db_host} {db_name} {db_user} {db_pass?}';
protected $signature = 'phpvms:importer {db_host} {db_name} {db_user} {db_pass?} {table_prefix=phpvms_}';
protected $description = 'Import from an older version of phpVMS';
/**
@ -19,7 +19,8 @@ class Importer extends BaseCommand
'host' => $this->argument('db_host'),
'name' => $this->argument('db_name'),
'user' => $this->argument('db_user'),
'pass' => $this->argument('db_pass')
'pass' => $this->argument('db_pass'),
'table_prefix' => $this->argument('table_prefix')
];
$importerSvc = new \App\Console\Services\Importer($db_creds);

View File

@ -13,6 +13,11 @@ use App\Models\Airline;
use App\Models\Airport;
use App\Models\Rank;
use App\Models\Subfleet;
use App\Models\User;
use App\Models\Enums\UserState;
use App\Facades\Utils;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Hash;
/**
* Class Importer
@ -68,6 +73,7 @@ class Importer
'name' => '',
'user' => '',
'pass' => '',
'table_prefix' => 'phpvms_'
], $db_creds);
}
@ -149,7 +155,11 @@ class Importer
*/
protected function tableName($table)
{
return 'phpvms_'.$table;
if($this->creds['table_prefix'] !== false) {
return $this->creds['table_prefix'].$table;
}
return $table;
}
/**
@ -163,8 +173,8 @@ class Importer
$model->save();
return true;
} catch (QueryException $e) {
#$this->error($e->getMessage());
return false;
# return false;
return $this->error($e->getMessage());
}
}
@ -180,7 +190,7 @@ class Importer
$rows = $this->conn->query($sql)->fetchColumn();
$this->info('Found '.$rows.' rows in '.$table);
return $rows;
return (int) $rows;
}
/**
@ -190,6 +200,9 @@ class Importer
*/
protected function readRows($table)
{
// Set the table prefix if it has been entered
$this->tableName($table);
$offset = 0;
$total_rows = $this->getTotalRows($table);
@ -405,15 +418,41 @@ class Importer
protected function importUsers()
{
/*$this->comment('--- USER IMPORT ---');
$this->comment('--- USER IMPORT ---');
$count = 0;
foreach ($this->readRows('pilots') as $row)
{
# TODO: What to do about pilot ids
$name = $row->firstname.' '.$row->lastname;
$airline_id = $this->airlines[$row->code];
$rank_id = $this->ranks[$row->rank];
$state = $this->getUserState($row->retired);
$attrs = [
'name' => $name,
'email' => $row->email,
'password' => "",
'api_key' => "",
'airline_id' => $airline_id->id,
'rank_id' => $rank_id->rankid,
'home_airport_id' => $row->hub,
'curr_airport_id' => $row->hub,
'flights' => (int) $row->totalflights,
'flight_time' => Utils::hoursToMinutes($row->totalhours),
'state' => $state,
];
if($this->saveModel(new User($attrs))) {
++$count;
}
}
$this->info('Imported ' . $count . ' users');*/
$this->info('Imported ' . $count . ' users');
// Reset Passwords & Generate API Keys
$this->setupUsers();
}
/**
@ -423,4 +462,59 @@ class Importer
{
/*$this->comment('--- RECALCULATING RANKS ---');*/
}
/**
* Generate user's API Key and email them their new password
*/
protected function setupUsers()
{
$allusers = User::all();
foreach($allusers as $user)
{
# Generate New User Password
$newpw = substr(md5(date('mdYhs')), 0, 10);
# Generate API Key
$api_key = Utils::generateApiKey();
# Update Info in DB
$user->password = Hash::make($newpw);
$user->api_key = $api_key;
$user->save();
}
# TODO: Think about how to deliver new password to user, email in batch at the end?
# TODO: How to reset password upon first login only for reset users
}
/**
* Get the user's new state from their original state
*/
protected function getUserState($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'])
{
# Active
return UserState::ACTIVE;
} elseif ($state == $phpvms_classic_states['INACTIVE']) {
# Rejected
# TODO: Make an inactive state?
return UserState::REJECTED;
} elseif ($state == $phpvms_classic_states['BANNED']) {
# Suspended
return UserState::SUSPENDED;
} elseif ($state == $phpvms_classic_states['ON_LEAVE']) {
# On Leave
return UserState::ON_LEAVE;
}
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Mail\Mailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
class NewLoginDetails extends Mailable
{
use Queueable, SerializesModels;
public $subject, $user, $newpw;
public function __construct(User $user, $newpw=null, $subject=null)
{
$this->subject = $subject ?: 'New Login Details';
$this->newpw = $newpw ?: 'N/A';
$this->user = $user;
}
public function build()
{
return $this->markdown('emails.user.newlogindetails')
->subject($this->subject)
->with(['user' => $this->user, 'newpw' => $this->newpw]);
}
}

View File

@ -18,6 +18,7 @@ class Airport extends BaseModel
'icao',
'name',
'location',
'country',
'lat',
'lon',
'hub',

View File

@ -35,11 +35,14 @@ class User extends Authenticatable
'email',
'password',
'airline_id',
'rank_id',
'api_key',
'home_airport_id',
'curr_airport_id',
'last_pirep_id',
'rank_id',
'flights',
'flight_time',
'balance',
'timezone',
'state',
'status',

View File

@ -2,6 +2,7 @@
namespace App\Services;
use Log;
use App\Facades\Utils;
use App\Models\User;
use App\Models\Rank;
@ -70,6 +71,8 @@ class UserService extends BaseService
. UserState::label($user->state));
event(new UserStateChanged($user, $old_state));
return $user;
}
/**

View File

@ -0,0 +1,17 @@
@component('mail::message')
Your new login details for {{ config('app.name') }} follow:
Do not share this information with anyone else! <br />
<strong>E-Mail Address:</strong> {!! $user->email !!}<br />
<strong>Temporary Password:</strong> {!! $newpw !!}<br /><br />
Your account is now ready for use.<br />
Upon first login, please reset your password.
@component('mail::button', ['url' => url('/login')])
Login & Reset Password
@endcomponent
Thanks,<br />
Management, {{ config('app.name') }}
@endcomponent