Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a few field entries #116

Merged
merged 6 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/Console/Commands/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Importer extends BaseCommand
{
protected $signature = 'phpvms:importer {db_host} {db_name} {db_user} {db_pass?}';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where my comment went, change this one back for now, I'll probably change this to use a config file, since I think there will be more settings to add. For testing, though, it's a pain with the ask() every time

protected $signature = 'phpvms:importer {db_host} {db_name} {db_user} {db_pass?} {table_prefix=phpvms_}';
protected $description = 'Import from an older version of phpVMS';

/**
Expand All @@ -19,7 +19,8 @@ public function handle()
'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);
Expand Down
106 changes: 100 additions & 6 deletions app/Console/Services/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
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
Expand Down Expand Up @@ -68,6 +73,7 @@ public function __construct($db_creds)
'name' => '',
'user' => '',
'pass' => '',
'table_prefix' => 'phpvms_'
], $db_creds);
}

Expand Down Expand Up @@ -149,7 +155,11 @@ protected function info($message)
*/
protected function tableName($table)
{
return 'phpvms_'.$table;
if($this->creds['table_prefix'] !== false) {
return $this->creds['table_prefix'].$table;
}

return $table;
}

/**
Expand All @@ -163,8 +173,8 @@ protected function saveModel($model)
$model->save();
return true;
} catch (QueryException $e) {
#$this->error($e->getMessage());
return false;
# return false;
return $this->error($e->getMessage());
}
}

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

$this->info('Found '.$rows.' rows in '.$table);
return $rows;
return (int) $rows;
}

/**
Expand All @@ -190,6 +200,9 @@ protected function getTotalRows($table)
*/
protected function readRows($table)
{
// Set the table prefix if it has been entered
$this->tableName($table);

$offset = 0;
$total_rows = $this->getTotalRows($table);

Expand Down Expand Up @@ -405,15 +418,41 @@ protected function importPireps()

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];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets the rank before it's imported, so the id remains as rankid in the old DB. Any ideas on how to change this to get the imported rank in the new DB?

Copy link
Owner

@nabeelio nabeelio Jan 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point! You can put it back to the DB lookup. I'll think of something more efficient tonight

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh, I remember what I was meaning to do. I was gonna set the value to the new ID from the insert. I'll fix it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good, this might be the same with airlines as well (only tested with the one that was already there, so not sure).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll fix it. Good to merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, I'll add the admin permissions thingo in another branch after so it doesn't stuff the current stuff up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also for that, importUsers function or separate function?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm I gotta thing about that. I might have to do a bit of refactoring for the ID mapping stuff. Probably makes sense to go into the method that's resetting the password/api key, etc. But that too will likely be once I refactor the mapping thing in a couple of hours

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np, I'll leave it for now.

$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();
}

/**
Expand All @@ -423,4 +462,59 @@ protected function recalculateRanks()
{
/*$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;
}
}
}
29 changes: 29 additions & 0 deletions app/Mail/NewLoginDetails.php
Original file line number Diff line number Diff line change
@@ -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]);
}
}
1 change: 1 addition & 0 deletions app/Models/Airport.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Airport extends BaseModel
'icao',
'name',
'location',
'country',
'lat',
'lon',
'hub',
Expand Down
5 changes: 4 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on these!

'flight_time',
'balance',
'timezone',
'state',
'status',
Expand Down
3 changes: 3 additions & 0 deletions app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services;

use Log;
use App\Facades\Utils;
use App\Models\User;
use App\Models\Rank;
Expand Down Expand Up @@ -70,6 +71,8 @@ public function changeUserState(User $user, $old_state): User
. UserState::label($user->state));

event(new UserStateChanged($user, $old_state));

return $user;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions resources/views/emails/user/newlogindetails.blade.php
Original file line number Diff line number Diff line change
@@ -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