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

Allow logins using pilot ID #698 #699

Merged
merged 1 commit into from
May 16, 2020
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
43 changes: 43 additions & 0 deletions app/Exceptions/PilotIdNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Exceptions;

class PilotIdNotFound extends AbstractHttpException
{
private $pilot_id;

public function __construct($pilot_id)
{
$this->pilot_id = $pilot_id;
parent::__construct(
404,
'Pilot '.$pilot_id.' not found'
);
}

/**
* Return the RFC 7807 error type (without the URL root)
*/
public function getErrorType(): string
{
return 'pilot-id-not-found';
}

/**
* Get the detailed error string
*/
public function getErrorDetails(): string
{
return $this->getMessage();
}

/**
* Return an array with the error details, merged with the RFC7807 response
*/
public function getErrorMetadata(): array
{
return [
'pilot_id' => $this->pilot_id,
];
}
}
76 changes: 69 additions & 7 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,98 @@
namespace App\Http\Controllers\Auth;

use App\Contracts\Controller;
use App\Exceptions\PilotIdNotFound;
use App\Models\Enums\UserState;
use App\Services\UserService;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

/**
* Class LoginController
*/
class LoginController extends Controller
{
use AuthenticatesUsers;

protected $redirectTo = '/dashboard';

/** @var UserService */
private $userSvc;

/** @var string */
private $loginFieldValue;

/**
* LoginController constructor.
*
* @param UserService $userSvc
*/
public function __construct()
public function __construct(UserService $userSvc)
{
$this->redirectTo = config('phpvms.login_redirect');
$this->middleware('guest', ['except' => 'logout']);
$this->userSvc = $userSvc;
}

/**
* Get the needed authorization credentials from the request.
* Overriding the value from the trait
*
* @override
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
protected function credentials(Request $request)
{
return [
'email' => $this->loginFieldValue,
'password' => $request->input('password'),
];
}

/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* Validate the user login request.
*
* @override
*
* @param \Illuminate\Http\Request $request
*
* @return void
*/
public function showLoginForm()
protected function validateLogin(Request $request)
{
return view('auth/login');
$id_field = $request->input('email');
$validations = ['required', 'string'];

/*
* Trying to login by email or not?
*
* If not, run a validation rule which attempts to split the user by their VA and ID
* Then inject that user's email into the request
*/
if (strpos($id_field, '@') !== false) {
$validations[] = 'email';
$this->loginFieldValue = $request->input('email');
} else {
$validations[] = function ($attr, $value, $fail) use ($request) {
try {
$user = $this->userSvc->findUserByPilotId($value);
} catch (PilotIdNotFound $ex) {
Log::warning('Error logging in, pilot_id not found, id='.$value);
$fail('Pilot not found');
return;
}

$request->email = $user->email;
$this->loginFieldValue = $user->email;
};
}

$request->validate([
'email' => $validations,
'password' => 'required|string',
]);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Events\UserRegistered;
use App\Events\UserStateChanged;
use App\Events\UserStatsChanged;
use App\Exceptions\PilotIdNotFound;
use App\Exceptions\UserPilotIdExists;
use App\Models\Enums\PirepState;
use App\Models\Enums\UserState;
Expand All @@ -14,6 +15,7 @@
use App\Models\Role;
use App\Models\User;
use App\Repositories\AircraftRepository;
use App\Repositories\AirlineRepository;
use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Support\Units\Time;
Expand All @@ -25,22 +27,26 @@
class UserService extends Service
{
private $aircraftRepo;
private $airlineRepo;
private $subfleetRepo;
private $userRepo;

/**
* UserService constructor.
*
* @param AircraftRepository $aircraftRepo
* @param AirlineRepository $airlineRepo
* @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo
*/
public function __construct(
AircraftRepository $aircraftRepo,
AirlineRepository $airlineRepo,
SubfleetRepository $subfleetRepo,
UserRepository $userRepo
) {
$this->aircraftRepo = $aircraftRepo;
$this->airlineRepo = $airlineRepo;
$this->subfleetRepo = $subfleetRepo;
$this->userRepo = $userRepo;
}
Expand Down Expand Up @@ -178,6 +184,42 @@ public function changePilotId(User $user, int $pilot_id): User
return $user;
}

/**
* Split a given pilot ID into an airline and ID portions
*
* @param string $pilot_id
*/
public function findUserByPilotId(string $pilot_id)
{
$airlines = $this->airlineRepo->all(['id', 'icao', 'iata']);

$ident_str = null;
$pilot_id = strtoupper($pilot_id);
foreach ($airlines as $airline) {
if (strpos($pilot_id, $airline->icao) !== false) {
$ident_str = $airline->icao;
break;
}

if (strpos($pilot_id, $airline->iata) !== false) {
$ident_str = $airline->iata;
break;
}
}

if (empty($ident_str)) {
throw new PilotIdNotFound($pilot_id);
}

$parsed_pilot_id = str_replace($ident_str, '', $pilot_id);
$user = User::where(['airline_id' => $airline->id, 'pilot_id' => $parsed_pilot_id])->first();
if (empty($user)) {
throw new PilotIdNotFound($pilot_id);
}

return $user;
}

/**
* Return the subfleets this user is allowed access to,
* based on their current rank
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'newestpilots' => 'Newest Pilots',
'profile' => 'Profile',
'email' => 'Email',
'pilot_id' => 'Pilot ID',
'register' => 'Register',
'login' => 'Log In',
'logout' => 'Log Out',
Expand Down Expand Up @@ -48,6 +49,7 @@
'inactive' => 'Inactive',
'yes' => 'Yes',
'no' => 'No',
'or' => 'or',
'days' => [
'mon' => 'Monday',
'tues' => 'Tuesday',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/es/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'newestpilots' => 'Nuevos pilotos',
'profile' => 'Perfil',
'email' => 'Email',
'pilot_id' => 'Pilot ID',
'register' => 'Registrarse',
'login' => 'Iniciar sesión',
'logout' => 'Cerrar sesión',
Expand Down Expand Up @@ -48,6 +49,7 @@
'inactive' => 'Inactivo',
'yes' => 'Sí',
'no' => 'No',
'or' => 'o',
'days' => [
'mon' => 'lunes',
'tues' => 'martes',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/it/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'newestpilots' => 'Ultimi Piloti',
'profile' => 'Profilo',
'email' => 'Email',
'pilot_id' => 'Pilot ID',
'login' => 'Accesso',
'logout' => 'Uscita',
'register' => 'Registrazione',
Expand Down Expand Up @@ -48,6 +49,7 @@
'inactive' => 'Inattivo',
'yes' => 'Sì',
'no' => 'No',
'or' => 'o',
'days' => [
'mon' => 'Lunedì',
'tues' => 'Martedì',
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layouts/default/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{{
Form::text('email', old('email'), [
'id' => 'email',
'placeholder' => __('common.email'),
'placeholder' => __('common.email').' '.__('common.or').' '.__('common.pilot_id'),
'class' => 'form-control',
'required' => true,
])
Expand Down
1 change: 1 addition & 0 deletions resources/views/vendor/mail/text/button.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $slot }}: {{ $url }}
1 change: 1 addition & 0 deletions resources/views/vendor/mail/text/footer.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $slot }}
1 change: 1 addition & 0 deletions resources/views/vendor/mail/text/header.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{{ $slot }}]({{ $url }})
9 changes: 9 additions & 0 deletions resources/views/vendor/mail/text/layout.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{!! strip_tags($header) !!}

{!! strip_tags($slot) !!}
@isset($subcopy)

{!! strip_tags($subcopy) !!}
@endisset

{!! strip_tags($footer) !!}
27 changes: 27 additions & 0 deletions resources/views/vendor/mail/text/message.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
{{ config('app.name') }}
@endcomponent
@endslot

{{-- Body --}}
{{ $slot }}

{{-- Subcopy --}}
@isset($subcopy)
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endisset

{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.')
@endcomponent
@endslot
@endcomponent
1 change: 1 addition & 0 deletions resources/views/vendor/mail/text/panel.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $slot }}
1 change: 1 addition & 0 deletions resources/views/vendor/mail/text/subcopy.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $slot }}
1 change: 1 addition & 0 deletions resources/views/vendor/mail/text/table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $slot }}
31 changes: 31 additions & 0 deletions tests/UserTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Exceptions\PilotIdNotFound;
use App\Exceptions\UserPilotIdExists;
use App\Models\User;
use App\Repositories\SettingRepository;
Expand All @@ -8,7 +9,10 @@

class UserTest extends TestCase
{
/** @var SettingRepository */
protected $settingsRepo;

/** @var UserService */
protected $userSvc;

public function setUp(): void
Expand Down Expand Up @@ -204,6 +208,33 @@ public function testUserPilotIdChangeAlreadyExists()
$this->userSvc->changePilotId($user1, 2);
}

/**
* Make sure that the splitting of the user ID works
*/
public function testUserPilotIdSplit(): void
{
/** @var \App\Models\User $user */
$user = factory(App\Models\User::class)->create();
$found_user = $this->userSvc->findUserByPilotId($user->ident);
$this->assertEquals($user->id, $found_user->id);

// Look for them with the IATA code
$found_user = $this->userSvc->findUserByPilotId($user->airline->iata.$user->id);
$this->assertEquals($user->id, $found_user->id);
}

/**
* Pilot ID not found
*/
public function testUserPilotIdSplitInvalidId(): void
{
/** @var \App\Models\User $user */
$user = factory(App\Models\User::class)->create();

$this->expectException(PilotIdNotFound::class);
$this->userSvc->findUserByPilotId($user->airline->iata);
}

/**
* Test the pilot ID being added when a new user is created
*/
Expand Down