Skip to content

Commit 08df20d

Browse files
web541nabeelio
authored andcommitted
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
1 parent be6e5e8 commit 08df20d

File tree

7 files changed

+157
-9
lines changed

7 files changed

+157
-9
lines changed

app/Console/Commands/Importer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Importer extends BaseCommand
99
{
10-
protected $signature = 'phpvms:importer {db_host} {db_name} {db_user} {db_pass?}';
10+
protected $signature = 'phpvms:importer {db_host} {db_name} {db_user} {db_pass?} {table_prefix=phpvms_}';
1111
protected $description = 'Import from an older version of phpVMS';
1212

1313
/**
@@ -19,7 +19,8 @@ public function handle()
1919
'host' => $this->argument('db_host'),
2020
'name' => $this->argument('db_name'),
2121
'user' => $this->argument('db_user'),
22-
'pass' => $this->argument('db_pass')
22+
'pass' => $this->argument('db_pass'),
23+
'table_prefix' => $this->argument('table_prefix')
2324
];
2425

2526
$importerSvc = new \App\Console\Services\Importer($db_creds);

app/Console/Services/Importer.php

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
use App\Models\Airport;
1414
use App\Models\Rank;
1515
use App\Models\Subfleet;
16+
use App\Models\User;
17+
use App\Models\Enums\UserState;
18+
use App\Facades\Utils;
19+
use Illuminate\Support\Facades\Mail;
20+
use Illuminate\Support\Facades\Hash;
1621

1722
/**
1823
* Class Importer
@@ -68,6 +73,7 @@ public function __construct($db_creds)
6873
'name' => '',
6974
'user' => '',
7075
'pass' => '',
76+
'table_prefix' => 'phpvms_'
7177
], $db_creds);
7278
}
7379

@@ -149,7 +155,11 @@ protected function info($message)
149155
*/
150156
protected function tableName($table)
151157
{
152-
return 'phpvms_'.$table;
158+
if($this->creds['table_prefix'] !== false) {
159+
return $this->creds['table_prefix'].$table;
160+
}
161+
162+
return $table;
153163
}
154164

155165
/**
@@ -163,8 +173,8 @@ protected function saveModel($model)
163173
$model->save();
164174
return true;
165175
} catch (QueryException $e) {
166-
#$this->error($e->getMessage());
167-
return false;
176+
# return false;
177+
return $this->error($e->getMessage());
168178
}
169179
}
170180

@@ -180,7 +190,7 @@ protected function getTotalRows($table)
180190
$rows = $this->conn->query($sql)->fetchColumn();
181191

182192
$this->info('Found '.$rows.' rows in '.$table);
183-
return $rows;
193+
return (int) $rows;
184194
}
185195

186196
/**
@@ -190,6 +200,9 @@ protected function getTotalRows($table)
190200
*/
191201
protected function readRows($table)
192202
{
203+
// Set the table prefix if it has been entered
204+
$this->tableName($table);
205+
193206
$offset = 0;
194207
$total_rows = $this->getTotalRows($table);
195208

@@ -405,15 +418,41 @@ protected function importPireps()
405418

406419
protected function importUsers()
407420
{
408-
/*$this->comment('--- USER IMPORT ---');
421+
$this->comment('--- USER IMPORT ---');
409422

410423
$count = 0;
411424
foreach ($this->readRows('pilots') as $row)
412425
{
426+
# TODO: What to do about pilot ids
427+
428+
$name = $row->firstname.' '.$row->lastname;
429+
$airline_id = $this->airlines[$row->code];
430+
$rank_id = $this->ranks[$row->rank];
431+
$state = $this->getUserState($row->retired);
432+
433+
$attrs = [
434+
'name' => $name,
435+
'email' => $row->email,
436+
'password' => "",
437+
'api_key' => "",
438+
'airline_id' => $airline_id->id,
439+
'rank_id' => $rank_id->rankid,
440+
'home_airport_id' => $row->hub,
441+
'curr_airport_id' => $row->hub,
442+
'flights' => (int) $row->totalflights,
443+
'flight_time' => Utils::hoursToMinutes($row->totalhours),
444+
'state' => $state,
445+
];
413446

447+
if($this->saveModel(new User($attrs))) {
448+
++$count;
449+
}
414450
}
415451

416-
$this->info('Imported ' . $count . ' users');*/
452+
$this->info('Imported ' . $count . ' users');
453+
454+
// Reset Passwords & Generate API Keys
455+
$this->setupUsers();
417456
}
418457

419458
/**
@@ -423,4 +462,59 @@ protected function recalculateRanks()
423462
{
424463
/*$this->comment('--- RECALCULATING RANKS ---');*/
425464
}
465+
466+
/**
467+
* Generate user's API Key and email them their new password
468+
*/
469+
protected function setupUsers()
470+
{
471+
$allusers = User::all();
472+
foreach($allusers as $user)
473+
{
474+
# Generate New User Password
475+
$newpw = substr(md5(date('mdYhs')), 0, 10);
476+
477+
# Generate API Key
478+
$api_key = Utils::generateApiKey();
479+
480+
# Update Info in DB
481+
$user->password = Hash::make($newpw);
482+
$user->api_key = $api_key;
483+
$user->save();
484+
}
485+
486+
# TODO: Think about how to deliver new password to user, email in batch at the end?
487+
# TODO: How to reset password upon first login only for reset users
488+
}
489+
490+
/**
491+
* Get the user's new state from their original state
492+
*/
493+
protected function getUserState($state)
494+
{
495+
// Declare array of classic states
496+
$phpvms_classic_states = [
497+
'ACTIVE' => 0,
498+
'INACTIVE' => 1,
499+
'BANNED' => 2,
500+
'ON_LEAVE' => 3
501+
];
502+
503+
// Decide which state they will be in accordance with v7
504+
if ($state == $phpvms_classic_states['ACTIVE'])
505+
{
506+
# Active
507+
return UserState::ACTIVE;
508+
} elseif ($state == $phpvms_classic_states['INACTIVE']) {
509+
# Rejected
510+
# TODO: Make an inactive state?
511+
return UserState::REJECTED;
512+
} elseif ($state == $phpvms_classic_states['BANNED']) {
513+
# Suspended
514+
return UserState::SUSPENDED;
515+
} elseif ($state == $phpvms_classic_states['ON_LEAVE']) {
516+
# On Leave
517+
return UserState::ON_LEAVE;
518+
}
519+
}
426520
}

app/Mail/NewLoginDetails.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use App\Models\User;
6+
use Illuminate\Mail\Mailable;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class NewLoginDetails extends Mailable
11+
{
12+
use Queueable, SerializesModels;
13+
14+
public $subject, $user, $newpw;
15+
16+
public function __construct(User $user, $newpw=null, $subject=null)
17+
{
18+
$this->subject = $subject ?: 'New Login Details';
19+
$this->newpw = $newpw ?: 'N/A';
20+
$this->user = $user;
21+
}
22+
23+
public function build()
24+
{
25+
return $this->markdown('emails.user.newlogindetails')
26+
->subject($this->subject)
27+
->with(['user' => $this->user, 'newpw' => $this->newpw]);
28+
}
29+
}

app/Models/Airport.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Airport extends BaseModel
1818
'icao',
1919
'name',
2020
'location',
21+
'country',
2122
'lat',
2223
'lon',
2324
'hub',

app/Models/User.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ class User extends Authenticatable
3535
'email',
3636
'password',
3737
'airline_id',
38+
'rank_id',
3839
'api_key',
3940
'home_airport_id',
4041
'curr_airport_id',
4142
'last_pirep_id',
42-
'rank_id',
43+
'flights',
44+
'flight_time',
45+
'balance',
4346
'timezone',
4447
'state',
4548
'status',

app/Services/UserService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Services;
44

5+
use Log;
56
use App\Facades\Utils;
67
use App\Models\User;
78
use App\Models\Rank;
@@ -70,6 +71,8 @@ public function changeUserState(User $user, $old_state): User
7071
. UserState::label($user->state));
7172

7273
event(new UserStateChanged($user, $old_state));
74+
75+
return $user;
7376
}
7477

7578
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@component('mail::message')
2+
Your new login details for {{ config('app.name') }} follow:
3+
4+
Do not share this information with anyone else! <br />
5+
<strong>E-Mail Address:</strong> {!! $user->email !!}<br />
6+
<strong>Temporary Password:</strong> {!! $newpw !!}<br /><br />
7+
8+
Your account is now ready for use.<br />
9+
Upon first login, please reset your password.
10+
11+
@component('mail::button', ['url' => url('/login')])
12+
Login & Reset Password
13+
@endcomponent
14+
15+
Thanks,<br />
16+
Management, {{ config('app.name') }}
17+
@endcomponent

0 commit comments

Comments
 (0)