Skip to content

Commit

Permalink
Merge pull request #324 from nabeelio/265-editable-pilot-id
Browse files Browse the repository at this point in the history
Backend changes separating id from pilot_id
  • Loading branch information
nabeelio authored Jul 17, 2019
2 parents 454776e + bb15119 commit 0225a84
Show file tree
Hide file tree
Showing 36 changed files with 522 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .travis/deploy_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ if [ "$TRAVIS" = "true" ]; then
# clear any app specific stuff that might have been loaded in
find storage/app -mindepth 1 -not -name '.gitignore' -not -name public -not -name import -print0 -exec rm -rf {} +
find storage/app/public -mindepth 1 -not -name '.gitignore' -not -name avatar -not -name uploads -print0 -exec rm -rf {} +
find storage/app/public/avatar -mindepth 1 -not -name '.gitignore' -print0 -exec rm -rf {} +
find storage/app/public/avatars -mindepth 1 -not -name '.gitignore' -print0 -exec rm -rf {} +
find storage/app/public/uploads -mindepth 1 -not -name '.gitignore' -print0 -exec rm -rf {} +
find storage/debugbar -mindepth 1 -not -name '.gitignore' -print0 -exec rm -rf {} +
find storage/docker -mindepth 1 -not -name '.gitignore' -print0 -exec rm -rf {} +
Expand Down
1 change: 1 addition & 0 deletions app/Database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

return [
'id' => null,
'pilot_id' => 0,
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => $password ?: $password = Hash::make('secret'),
Expand Down
66 changes: 66 additions & 0 deletions app/Database/migrations/2019_07_16_141152_users_add_pilot_id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

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');
});
}
}
3 changes: 3 additions & 0 deletions app/Database/seeds/sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ roles:

users:
- id: 1
pilot_id: 1
name: Admin User
email: admin@phpvms.net
password: admin
Expand All @@ -34,6 +35,7 @@ users:
created_at: now
updated_at: now
- id: 2
pilot_id: 2
name: Carla Walters
email: carla.walters68@example.com
password: admin
Expand All @@ -50,6 +52,7 @@ users:
opt_in: 1
toc_accepted: 1
- id: 3
pilot_id: 3
name: Raymond Pearson
email: raymond.pearson56@example.com
password: admin
Expand Down
9 changes: 9 additions & 0 deletions app/Exceptions/UserPilotIdExists.php
Original file line number Diff line number Diff line change
@@ -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';
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public function destroy($id)
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();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/AcarsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function acars_store($id, PositionRequest $request)
$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()
);

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function sendLoginResponse(Request $request)

// 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
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Frontend/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function update(Request $request)
]);

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))
Expand All @@ -153,7 +153,7 @@ public function update(Request $request)
}
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.
Expand Down Expand Up @@ -189,7 +189,7 @@ public function update(Request $request)
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();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/CreatePirepRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Models\Pirep;
use App\Repositories\PirepFieldRepository;
use Illuminate\Foundation\Http\FormRequest;
use Log;
use Illuminate\Support\Facades\Log;

class CreatePirepRequest extends FormRequest
{
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Requests/CreateUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public function authorize(): bool
public function rules(): array
{
$rules = User::$rules;

$rules['email'] .= '|unique:users,email';
$rules['pilot_id'] .= '|unique:users,pilot_id';

return $rules;
}
}
10 changes: 9 additions & 1 deletion app/Http/Requests/UpdateUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use function request;

class UpdateUserRequest extends FormRequest
{
Expand All @@ -24,6 +25,13 @@ public function authorize()
*/
public function rules()
{
return User::$rules;
$rules = User::$rules;

$user_id = request('id', null);

$rules['email'] .= '|unique:users,email,'.$user_id.',id';
$rules['pilot_id'] .= '|unique:users,pilot_id,'.$user_id.',id';

return $rules;
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/PirepComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function toArray($request)
'user' => [
'id' => $user->id,
'pilot_id' => $user->pilot_id,
'ident' => $user->ident,
'name' => $user->name,
],
];
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function toArray($request)
return [
'id' => $this->id,
'pilot_id' => $this->pilot_id,
'ident' => $this->ident,
'name' => $this->name,
'email' => $this->email,
'apikey' => $this->apikey,
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/NotificationEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function sendEmail($to, $email)
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');

Expand Down
26 changes: 26 additions & 0 deletions app/Models/Observers/UserObserver.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
28 changes: 14 additions & 14 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -78,6 +83,8 @@ class User extends Authenticatable
];

protected $casts = [
'id' => 'integer',
'pilot_id' => 'integer',
'flights' => 'integer',
'flight_time' => 'integer',
'transfer_time' => 'integer',
Expand All @@ -89,26 +96,19 @@ class User extends Authenticatable
];

public static $rules = [
'name' => 'required',
'email' => 'required|email',
'name' => 'required',
'email' => 'required|email',
'pilot_id' => 'required|integer',
];

/**
* @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);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
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;
Expand Down Expand Up @@ -53,6 +55,7 @@ public function boot(): void

Setting::observe(SettingObserver::class);
Subfleet::observe(SubfleetObserver::class);
User::observe(UserObserver::class);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Services/PirepService.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public function removeBid(Pirep $pirep)
]);

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();
}
}
Expand Down
Loading

0 comments on commit 0225a84

Please sign in to comment.