From 317ebd216376dd900d94476240e068f14d77b10f Mon Sep 17 00:00:00 2001 From: Ian Cowan Date: Thu, 29 Dec 2022 11:34:01 -0500 Subject: [PATCH] #238 Fix VATUSA external users login to realops --- app/Http/Controllers/Auth/LoginController.php | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 96665f12a..fe743537c 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -67,9 +67,19 @@ protected function verifyLogin(Request $request) { protected function vatusaAuth($resourceOwner, $accessToken) { $client = new Client(); - $result = $client->request('GET', 'https://api.vatusa.net/v2/user/' . $resourceOwner->data->cid . '?apikey=' . Config::get('vatusa.api_key')); + $result = $client->request('GET', 'https://api.vatusa.net/v2/user/' . $resourceOwner->data->cid . '?apikey=' . Config::get('vatusa.api_key'), ['http_errors' => false]); + $realops_toggle_enabled = toggleEnabled('realops'); + + if (! $result || $result->getStatusCode() != 200) { + if ($realops_toggle_enabled) { + return $this->externalRealopsLogin( + $resourceOwner->data->cid, + $resourceOwner->data->personal->name_first, + $resourceOwner->data->personal->name_last, + $resourceOwner->data->personal->email + ); + } - if (! $result) { return redirect('/')->with('error', 'We are unable to verify your access at this time. Please try again in a few minutes.'); } @@ -82,13 +92,12 @@ protected function vatusaAuth($resourceOwner, $accessToken) { $res = $resu['data']; $userstatuscheck = User::find($res['cid']); - $realops_toggle_enabled = toggleEnabled('realops'); if (! $realops_toggle_enabled && ! $userstatuscheck) { return redirect('/')->with('error', 'You have not been found on the roster. If you have recently joined, please allow up to an hour for the roster to update.'); } elseif (! $realops_toggle_enabled && $userstatuscheck->status == 2) { return redirect('/')->with('error', 'You have not been found on the roster. If you have recently joined, please allow up to an hour for the roster to update.'); } elseif (! $userstatuscheck || $userstatuscheck->status == 2) { - return $this->externalRealopsLogin($res); + return $this->externalRealopsLogin($res['cid'], $res['fname'], $res['lname'], $res['email']); } $userstatuscheck->fname = $res['fname']; @@ -163,16 +172,16 @@ public function realopsLogin() { return $this->completeRealopsLogin($realops_pilot); } - private function externalRealopsLogin($data) { - $realops_pilot = RealopsPilot::find($data['cid']); + private function externalRealopsLogin($cid, $fname, $lname, $email) { + $realops_pilot = RealopsPilot::find($cid); if (! $realops_pilot) { $realops_pilot = new RealopsPilot; } - $realops_pilot->fname = $data['fname']; - $realops_pilot->lname = $data['lname']; - $realops_pilot->email = $data['email']; + $realops_pilot->fname = $fname; + $realops_pilot->lname = $lname; + $realops_pilot->email = $email; $realops_pilot->save(); return $this->completeRealopsLogin($realops_pilot);