From 08df20de198ec66e70fe2e5610bce3f4fb65fd0c Mon Sep 17 00:00:00 2001 From: web541 Date: Thu, 4 Jan 2018 08:41:21 +1100 Subject: [PATCH] Fixed a few field entries (#116) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- app/Console/Commands/Importer.php | 5 +- app/Console/Services/Importer.php | 106 +++++++++++++++++- app/Mail/NewLoginDetails.php | 29 +++++ app/Models/Airport.php | 1 + app/Models/User.php | 5 +- app/Services/UserService.php | 3 + .../emails/user/newlogindetails.blade.php | 17 +++ 7 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 app/Mail/NewLoginDetails.php create mode 100644 resources/views/emails/user/newlogindetails.blade.php diff --git a/app/Console/Commands/Importer.php b/app/Console/Commands/Importer.php index 91ff255b..f12bf578 100644 --- a/app/Console/Commands/Importer.php +++ b/app/Console/Commands/Importer.php @@ -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); diff --git a/app/Console/Services/Importer.php b/app/Console/Services/Importer.php index 1df5f7f6..94fb3ff2 100644 --- a/app/Console/Services/Importer.php +++ b/app/Console/Services/Importer.php @@ -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; + } + } } diff --git a/app/Mail/NewLoginDetails.php b/app/Mail/NewLoginDetails.php new file mode 100644 index 00000000..123118e4 --- /dev/null +++ b/app/Mail/NewLoginDetails.php @@ -0,0 +1,29 @@ +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]); + } +} diff --git a/app/Models/Airport.php b/app/Models/Airport.php index 6dbb145b..3c3c926c 100644 --- a/app/Models/Airport.php +++ b/app/Models/Airport.php @@ -18,6 +18,7 @@ class Airport extends BaseModel 'icao', 'name', 'location', + 'country', 'lat', 'lon', 'hub', diff --git a/app/Models/User.php b/app/Models/User.php index 00816196..24c34a01 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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', diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 89793906..ef64cb60 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -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; } /** diff --git a/resources/views/emails/user/newlogindetails.blade.php b/resources/views/emails/user/newlogindetails.blade.php new file mode 100644 index 00000000..c8550ef9 --- /dev/null +++ b/resources/views/emails/user/newlogindetails.blade.php @@ -0,0 +1,17 @@ +@component('mail::message') +Your new login details for {{ config('app.name') }} follow: + +Do not share this information with anyone else!
+E-Mail Address: {!! $user->email !!}
+Temporary Password: {!! $newpw !!}

+ +Your account is now ready for use.
+Upon first login, please reset your password. + +@component('mail::button', ['url' => url('/login')]) +Login & Reset Password +@endcomponent + +Thanks,
+Management, {{ config('app.name') }} +@endcomponent