From 639943e7ed18929b17c1990c68ec5294ccb8c2ec Mon Sep 17 00:00:00 2001 From: Nick Liu Date: Tue, 25 Feb 2020 12:46:44 +0100 Subject: [PATCH] Correct user_xup changed value in Steam social login provider Info: https://github.com/e107inc/e107/pull/4099#issuecomment-590579521 --- e107_plugins/social/social_setup.php | 61 +++++++++++++++++++ .../unit/plugins/social/social_setupTest.php | 28 ++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/e107_plugins/social/social_setup.php b/e107_plugins/social/social_setup.php index 5e7aaf2f6..deda62f66 100644 --- a/e107_plugins/social/social_setup.php +++ b/e107_plugins/social/social_setup.php @@ -13,10 +13,19 @@ require_once("SocialLoginConfigManager.php"); class social_setup { public function upgrade_required() + { + return ( + $this->upgrade_required_provider_name_normalization() || + $this->upgrade_required_steam_xup_bug() + ); + } + + private function upgrade_required_provider_name_normalization() { $coreConfig = e107::getConfig(); $manager = new SocialLoginConfigManager($coreConfig); $providerConfig = $coreConfig->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF); + if (!is_array($providerConfig)) $providerConfig = []; $normalizedProviderNames = array_keys($providerConfig); foreach ($normalizedProviderNames as $normalizedProviderName) { @@ -27,13 +36,30 @@ class social_setup return false; } + /** + * @see https://github.com/e107inc/e107/pull/4099#issuecomment-590579521 + */ + private function upgrade_required_steam_xup_bug() + { + $db = e107::getDb(); + $count = $db->count('user', '(*)', "user_xup LIKE 'Steam_https://steamcommunity.com/openid/id/%'"); + return $count >= 1; + } + public function upgrade_pre() + { + $this->upgrade_pre_provider_name_normalization(); + $this->upgrade_pre_steam_xup_bug(); + } + + private function upgrade_pre_provider_name_normalization() { $coreConfig = e107::getConfig(); $logger = e107::getMessage(); $manager = new SocialLoginConfigManager($coreConfig); $providerConfig = $coreConfig->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF); + if (!is_array($providerConfig)) $providerConfig = []; foreach ($providerConfig as $oldNormalizedProviderName => $oldOptions) { @@ -86,4 +112,39 @@ class social_setup } return $denormalizedProviderName; } + + /** + * @see https://github.com/e107inc/e107/pull/4099#issuecomment-590579521 + */ + private function upgrade_pre_steam_xup_bug() + { + $logger = e107::getMessage(); + $db = e107::getDb(); + $db->select('user', '*', "user_xup LIKE 'Steam_https://steamcommunity.com/openid/id/%'"); + $rows = $db->rows(); + foreach ($rows as $row) + { + $old_user_xup = $row['user_xup']; + $new_user_xup = str_ireplace( + ['http://steamcommunity.com/openid/id/', 'https://steamcommunity.com/openid/id/'], + '', + $old_user_xup + ); + $status = $db->update( + 'user', + "user_xup = '".$db->escape($new_user_xup)."' WHERE user_id = ".$db->escape($row['user_id']) + ); + if ($status !== 1) + { + $logger->addError( + "Unexpected error while correcting user_xup of user_id = ".$row['user_id']." from \"".$old_user_xup."\" to \"".$new_user_xup."\": ". + $db->getLastErrorText() + ); + } + else + { + $logger->addSuccess("Corrected user_xup of user_id = ".$row['user_id']." from \"".$old_user_xup."\" to \"".$new_user_xup."\""); + } + } + } } \ No newline at end of file diff --git a/e107_tests/tests/unit/plugins/social/social_setupTest.php b/e107_tests/tests/unit/plugins/social/social_setupTest.php index e06854d39..72f46d037 100644 --- a/e107_tests/tests/unit/plugins/social/social_setupTest.php +++ b/e107_tests/tests/unit/plugins/social/social_setupTest.php @@ -10,11 +10,14 @@ class social_setupTest extends \Codeception\Test\Unit { - public function testUpgrade() + public function _before() { include_once(e_PLUGIN . "social/SocialLoginConfigManager.php"); include_once(e_PLUGIN . "social/social_setup.php"); + } + public function testUpgradeProviderNameNormalization() + { e107::getConfig()->set(SocialLoginConfigManager::SOCIAL_LOGIN_PREF, SOCIAL_LOGIN_LEGACY_DATA); $social_setup = new social_setup(); $this->assertTrue($social_setup->upgrade_required()); @@ -26,6 +29,29 @@ class social_setupTest extends \Codeception\Test\Unit $this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL")); $this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL-OpenID")); } + + /** + * @see https://github.com/e107inc/e107/pull/4099#issuecomment-590579521 + */ + public function testUpgradeFixSteamXupBug() + { + $db = e107::getDb(); + $db->insert('user', [ + 'user_loginname' => 'SteambB8047', + 'user_password' => '$2y$10$.u22u/U392cUhvJm2DJ57.wsKtxKKj3WsZ.x6LsXoUVHVuprZGgUu', + 'user_email' => '', + 'user_xup' => 'Steam_https://steamcommunity.com/openid/id/76561198006790310', + ]); + $insertId = $db->lastInsertId(); + + $social_setup = new social_setup(); + $this->assertTrue($social_setup->upgrade_required()); + $social_setup->upgrade_pre(); + + $result = $db->retrieve('user', '*', 'user_id=' . $insertId); + $this->assertEquals('Steam_76561198006790310', $result['user_xup']); + $this->assertFalse($social_setup->upgrade_required()); + } } const SOCIAL_LOGIN_LEGACY_DATA = array(