Enable logins using pilot ID #698

This commit is contained in:
Nabeel Shahzad
2020-05-16 11:46:25 -04:00
parent 00505a1607
commit b9aeda1cba
16 changed files with 234 additions and 8 deletions

View File

@@ -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,
];
}
}

View File

@@ -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;
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* Get the needed authorization credentials from the request.
* Overriding the value from the trait
*
* @override
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function showLoginForm()
protected function credentials(Request $request)
{
return view('auth/login');
return [
'email' => $this->loginFieldValue,
'password' => $request->input('password'),
];
}
/**
* Validate the user login request.
*
* @override
*
* @param \Illuminate\Http\Request $request
*
* @return void
*/
protected function validateLogin(Request $request)
{
$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',
]);
}
/**

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\PilotIdNotFound;
use App\Exceptions\UserPilotIdExists;
use App\Models\Enums\PirepState;
use App\Models\Enums\UserState;
@@ -14,6 +15,7 @@ use App\Models\Rank;
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;
@@ -25,6 +27,7 @@ use function is_array;
class UserService extends Service
{
private $aircraftRepo;
private $airlineRepo;
private $subfleetRepo;
private $userRepo;
@@ -32,15 +35,18 @@ class UserService extends Service
* 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;
}
@@ -178,6 +184,42 @@ class UserService extends Service
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