Backend changes separating id from pilot_id

This commit is contained in:
Nabeel Shahzad
2019-07-16 13:54:14 -04:00
parent 454776ecd4
commit 3e1d9080df
21 changed files with 454 additions and 34 deletions

View File

@@ -8,6 +8,7 @@ $factory->define(App\Models\User::class, function (Faker $faker) {
return [
'id' => null,
'pilot_id' => 0,
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => $password ?: $password = Hash::make('secret'),

View File

@@ -0,0 +1,67 @@
<?php
use App\Contracts\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UsersAddPilotId extends Migration
{
/**
* Kinda of gross operations to change the pilot ID column
* 1. Add an `pilot_id` column, which will get populated with the current ID
* 2. Drop the `id` column, and then recreate it as a string field
* 3. Iterate through all of the users and set their `id` to the `pilot_id`
* 4. Change the other tables column types that reference `user_id`
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->unsignedBigInteger('pilot_id')
->after('id')
->unique()
->nullable()
->index('users_pilot_id');
});
// Migrate the current pilot IDs
DB::update('UPDATE `users` SET `pilot_id`=`id`');
// Drop the old ID column and add a new one
/*Schema::table('users', function (Blueprint $table) {
$table->dropPrimary('users_id_primary');
$table->dropColumn('id');
$table->string('id', Model::ID_MAX_LENGTH)->primary();
});
// Update the users to use the `pilot_id` (so we don't need to migrate data from other tables)
$users = DB::table('users')->get(['id']);
foreach ($users as $user) {
$user->id = $user->pilot_id;
$user->save();
}*/
// role_user
// permission_user
// sessions
// pireps
// bids
// news
// user_awards
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('pilot_id');
});
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace App\Exceptions;
class UserPilotIdExists extends InternalError
{
public const FIELD = 'pilot_id';
public const MESSAGE = 'A user with this pilot ID already exists';
}

View File

@@ -292,7 +292,7 @@ class UserController extends Controller
public function regen_apikey($id, Request $request)
{
$user = User::find($id);
Log::info('Regenerating API key "'.$user->pilot_id.'"');
Log::info('Regenerating API key "'.$user->ident.'"');
$user->api_key = Utils::generateApiKey();
$user->save();

View File

@@ -131,7 +131,7 @@ class AcarsController extends Controller
$this->checkCancelled($pirep);
Log::debug(
'Posting ACARS update (user: '.Auth::user()->pilot_id.', pirep id :'.$id.'): ',
'Posting ACARS update (user: '.Auth::user()->ident.', pirep id :'.$id.'): ',
$request->post()
);

View File

@@ -49,7 +49,7 @@ class LoginController extends Controller
// TODO: How to handle ON_LEAVE?
if ($user->state !== UserState::ACTIVE) {
Log::info('Trying to login '.$user->pilot_id.', state '
Log::info('Trying to login '.$user->ident.', state '
.UserState::label($user->state));
// Log them out

View File

@@ -133,7 +133,7 @@ class ProfileController extends Controller
]);
if ($validator->fails()) {
Log::info('validator failed for user '.$user->pilot_id);
Log::info('validator failed for user '.$user->ident);
Log::info($validator->errors()->toArray());
return redirect(route('frontend.profile.edit', $id))
@@ -153,7 +153,7 @@ class ProfileController extends Controller
}
if ($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
$file_name = $user->pilot_id.'.'.$avatar->getClientOriginalExtension();
$file_name = $user->ident.'.'.$avatar->getClientOriginalExtension();
$path = "avatars/{$file_name}";
// Create the avatar, resizing it and keeping the aspect ratio.
@@ -189,7 +189,7 @@ class ProfileController extends Controller
public function regen_apikey(Request $request)
{
$user = User::find(Auth::user()->id);
Log::info('Regenerating API key "'.$user->pilot_id.'"');
Log::info('Regenerating API key "'.$user->ident.'"');
$user->api_key = Utils::generateApiKey();
$user->save();

View File

@@ -27,6 +27,7 @@ class PirepComment extends Resource
'user' => [
'id' => $user->id,
'pilot_id' => $user->pilot_id,
'ident' => $user->ident,
'name' => $user->name,
],
];

View File

@@ -11,6 +11,7 @@ class User extends Resource
return [
'id' => $this->id,
'pilot_id' => $this->pilot_id,
'ident' => $this->ident,
'name' => $this->name,
'email' => $this->email,
'apikey' => $this->apikey,

View File

@@ -68,7 +68,7 @@ class NotificationEvents extends Listener
public function onUserRegister(UserRegistered $event): void
{
Log::info('onUserRegister: '
.$event->user->pilot_id.' is '
.$event->user->ident.' is '
.UserState::label($event->user->state)
.', sending active email');

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Models\Observers;
use App\Models\User;
use App\Services\UserService;
class UserObserver
{
private $userSvc;
public function __construct(UserService $userSvc)
{
$this->userSvc = $userSvc;
}
/**
* After a user has been created, do some stuff
*
* @param User $user
*/
public function created(User $user): void
{
$this->userSvc->findAndSetPilotId($user);
}
}

View File

@@ -10,14 +10,17 @@ use Illuminate\Notifications\Notifiable;
use Laratrust\Traits\LaratrustUserTrait;
/**
* @property int id
* @property int id
* @property int pilot_id
* @property string name
* @property string email
* @property string password
* @property string api_key
* @property mixed ident
* @property mixed timezone
* @property string ident
* @property string curr_airport_id
* @property string home_airport_id
* @property Airline airline
* @property Flight[] flights
* @property string flight_time
* @property string remember_token
@@ -45,9 +48,11 @@ class User extends Authenticatable
public $journal_type = JournalType::USER;
protected $fillable = [
'id',
'name',
'email',
'password',
'pilot_id',
'airline_id',
'rank_id',
'api_key',
@@ -78,6 +83,8 @@ class User extends Authenticatable
];
protected $casts = [
'id' => 'integer',
'pilot_id' => 'integer',
'flights' => 'integer',
'flight_time' => 'integer',
'transfer_time' => 'integer',
@@ -96,19 +103,11 @@ class User extends Authenticatable
/**
* @return string
*/
public function getPilotIdAttribute()
public function getIdentAttribute()
{
$length = setting('pilots.id_length');
return $this->airline->icao.str_pad($this->id, $length, '0', STR_PAD_LEFT);
}
/**
* @return string
*/
public function getIdentAttribute()
{
return $this->getPilotIdAttribute();
return $this->airline->icao . str_pad($this->pilot_id, $length, '0', STR_PAD_LEFT);
}
/**

View File

@@ -15,10 +15,12 @@ use App\Models\Observers\JournalTransactionObserver;
use App\Models\Observers\SettingObserver;
use App\Models\Observers\Sluggable;
use App\Models\Observers\SubfleetObserver;
use App\Models\Observers\UserObserver;
use App\Models\PirepField;
use App\Models\PirepFieldValue;
use App\Models\Setting;
use App\Models\Subfleet;
use App\Models\User;
use App\Repositories\SettingRepository;
use App\Services\ModuleService;
use Illuminate\Support\Facades\Schema;
@@ -53,6 +55,7 @@ class AppServiceProvider extends ServiceProvider
Setting::observe(SettingObserver::class);
Subfleet::observe(SubfleetObserver::class);
User::observe(UserObserver::class);
}
/**

View File

@@ -425,7 +425,7 @@ class PirepService extends Service
]);
if ($bid) {
Log::info('Bid for user: '.$pirep->user->pilot_id.' on flight '.$flight->ident);
Log::info('Bid for user: '.$pirep->user->ident.' on flight '.$flight->ident);
$bid->delete();
}
}

View File

@@ -6,6 +6,7 @@ use App\Contracts\Service;
use App\Events\UserRegistered;
use App\Events\UserStateChanged;
use App\Events\UserStatsChanged;
use App\Exceptions\UserPilotIdExists;
use App\Models\Enums\PirepState;
use App\Models\Enums\UserState;
use App\Models\Pirep;
@@ -16,6 +17,7 @@ use App\Repositories\AircraftRepository;
use App\Repositories\SubfleetRepository;
use App\Support\Units\Time;
use Illuminate\Support\Collection;
use function is_array;
use Log;
/**
@@ -67,7 +69,7 @@ class UserService extends Service
// $user->attachRole($role);
// Attach any additional roles
if (!empty($groups) && \is_array($groups)) {
if (!empty($groups) && is_array($groups)) {
foreach ($groups as $group) {
$role = Role::where('name', $group)->first();
$user->attachRole($role);
@@ -83,6 +85,55 @@ class UserService extends Service
return $user;
}
/**
* Find the next available pilot ID and set the current user's pilot_id to that +1
* Called from UserObserver right now after a record is created
*
* @param User $user
*
* @return User
*/
public function findAndSetPilotId(User $user): User
{
if ($user->pilot_id !== null && $user->pilot_id > 0) {
return $user;
}
$max = (int) User::max('pilot_id');
$user->pilot_id = $max + 1;
$user->save();
Log::info('Set pilot ID for user ' . $user->id . ' to ' . $user->pilot_id);
return $user;
}
/**
* Change a user's pilot ID
*
* @param User $user
* @param int $pilot_id
*
* @throws UserPilotIdExists
*
* @return User
*/
public function changePilotId(User $user, int $pilot_id): User
{
if (User::where('pilot_id', '=', $pilot_id)->exists()) {
Log::error('User with id '.$pilot_id.' already exists');
throw new UserPilotIdExists();
}
$old_id = $user->pilot_id;
$user->pilot_id = $pilot_id;
$user->save();
Log::info('Changed pilot ID for user '.$user->id.' from '.$old_id.' to '.$user->pilot_id);
return $user;
}
/**
* Return the subfleets this user is allowed access to,
* based on their current rank
@@ -133,7 +184,7 @@ class UserService extends Service
return $user;
}
Log::info('User '.$user->pilot_id.' state changing from '
Log::info('User '.$user->ident.' state changing from '
.UserState::label($old_state).' to '
.UserState::label($user->state));