From bd4ce3584b571391a2ac5a6843a4d91f8e20be0f Mon Sep 17 00:00:00 2001 From: Omar Ramos Date: Wed, 30 Sep 2020 11:06:00 -0700 Subject: [PATCH] Adding a Potential Fix for Issue #12 Added a new link method (not sure if that's ok to do) and then added some extra checks in the authenticate() method to check and see if there may be an existing User record for the provided username, then gets the User ID for that row in the database and adds/links it to the OAuth2 User ID that is coming in from the OAuth2 authentication provider. Not sure if this is the best way to hook into the process but it did work for my basic tests. --- Auth/GenericOAuth2Provider.php | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Auth/GenericOAuth2Provider.php b/Auth/GenericOAuth2Provider.php index 63467f1..f4899a1 100644 --- a/Auth/GenericOAuth2Provider.php +++ b/Auth/GenericOAuth2Provider.php @@ -61,6 +61,27 @@ public function authenticate() if (! empty($profile)) { $this->userInfo = new GenericOAuth2UserProvider($this->container, $profile); + + $oauth2Username = $this->userInfo->getUsername(); + + if ($oauth2Username) { + $existingUserId = $this->userModel->getIdByUsername($oauth2Username); + + if ($existingUserId) { + $oauth2UserId = $this->userInfo->getExternalId(); + + if ($oauth2UserId) { + if (DEBUG) { + $this->logger->debug(__METHOD__.': The OAuth2 username is: '.$this->userInfo->getUsername()); + $this->logger->debug(__METHOD__.': The Existing User ID is: '.$existingUserId); + $this->logger->debug(__METHOD__.': The OAuth2 User ID is: '.$oauth2UserId); + } + + $this->link($existingUserId, $oauth2UserId); + } + } + } + return true; } @@ -134,6 +155,21 @@ public function getProfile() ); } + /** + * Link user + * + * @access public + * @param integer $userId + * @return bool + */ + public function link($userId, $oauthUserId) + { + return $this->userModel->update(array( + 'id' => $userId, + 'oauth2_user_id' => $oauthUserId, + )); + } + /** * Unlink user *