diff --git a/e107_plugins/social/social_setup.php b/e107_plugins/social/social_setup.php index deda62f66..abf229179 100644 --- a/e107_plugins/social/social_setup.php +++ b/e107_plugins/social/social_setup.php @@ -12,10 +12,17 @@ require_once("SocialLoginConfigManager.php"); class social_setup { + const RENAMED_PROVIDERS = [ + 'AOL' => 'AOLOpenID', + 'Github' => 'GitHub', + 'Live' => 'WindowsLive', + ]; + public function upgrade_required() { return ( $this->upgrade_required_provider_name_normalization() || + $this->upgrade_required_rename_xup() || $this->upgrade_required_steam_xup_bug() ); } @@ -36,19 +43,31 @@ class social_setup return false; } + private function upgrade_required_rename_xup() + { + $db = e107::getDb(); + $whereSegment = array_map(function ($oldProviderName) + { + return "user_xup LIKE BINARY '{$oldProviderName}\_%'"; + }, array_keys(self::RENAMED_PROVIDERS)); + $count = $db->count('user', '(*)', implode(' OR ', $whereSegment)); + return $count >= 1; + } + /** * @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/%'"); + $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_rename_xup(); $this->upgrade_pre_steam_xup_bug(); } @@ -101,26 +120,38 @@ class social_setup private function upgradeDenormalizedProviderQuirks($denormalizedProviderName) { - switch ($denormalizedProviderName) - { - case 'AOL': - $denormalizedProviderName = 'AOLOpenID'; - break; - case 'Live': - $denormalizedProviderName = 'WindowsLive'; - break; - } + $renamedProviders = self::RENAMED_PROVIDERS; + if (isset($renamedProviders[$denormalizedProviderName])) return $renamedProviders[$denormalizedProviderName]; return $denormalizedProviderName; } + private function upgrade_pre_rename_xup() + { + $db = e107::getDb(); + foreach (self::RENAMED_PROVIDERS as $oldProviderName => $newProviderName) + { + $db->select('user', '*', "user_xup LIKE '{$oldProviderName}\_%'"); + $rows = $db->rows(); + foreach ($rows as $row) + { + $old_user_xup = $row['user_xup']; + $new_user_xup = preg_replace( + '/^' . preg_quote($oldProviderName) . '_/', + $newProviderName . '_', + $old_user_xup + ); + $this->fixUserXup($db, $row['user_id'], $old_user_xup, $new_user_xup); + } + } + } + /** * @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/%'"); + $db->select('user', '*', "user_xup LIKE 'Steam\_https://steamcommunity.com/openid/id/%'"); $rows = $db->rows(); foreach ($rows as $row) { @@ -130,21 +161,33 @@ class social_setup '', $old_user_xup ); - $status = $db->update( - 'user', - "user_xup = '".$db->escape($new_user_xup)."' WHERE user_id = ".$db->escape($row['user_id']) + $this->fixUserXup($db, $row['user_id'], $old_user_xup, $new_user_xup); + } + } + + /** + * @param e_db_mysql $db + * @param string $user_id + * @param string $old_user_xup + * @param string $new_user_xup + */ + private function fixUserXup($db, $user_id, $old_user_xup, $new_user_xup) + { + $logger = e107::getMessage(); + $status = $db->update( + 'user', + "user_xup = '" . $db->escape($new_user_xup) . "' WHERE user_id = " . $db->escape($user_id) + ); + if ($status !== 1) + { + $logger->addError( + "Unexpected error while correcting user_xup of user_id = " . $user_id . " from \"" . $old_user_xup . "\" to \"" . $new_user_xup . "\": " . + $db->getLastErrorText() ); - 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."\""); - } + } + else + { + $logger->addSuccess("Corrected user_xup of user_id = " . $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 72f46d037..374423ded 100644 --- a/e107_tests/tests/unit/plugins/social/social_setupTest.php +++ b/e107_tests/tests/unit/plugins/social/social_setupTest.php @@ -23,11 +23,44 @@ class social_setupTest extends \Codeception\Test\Unit $this->assertTrue($social_setup->upgrade_required()); $this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL")); $this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL-OpenID")); + $this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/Github")); + $this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/GitHub-OAuth2")); + $this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/Live")); + $this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/WindowsLive")); $social_setup->upgrade_pre(); $this->assertFalse($social_setup->upgrade_required()); $this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL")); $this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL-OpenID")); + $this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/Github")); + $this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/GitHub-OAuth2")); + $this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/Live")); + $this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/WindowsLive-OAuth2")); + } + + public function testUpgradeFixRenamedProvidersXup() + { + $renamedProviders = social_setup::RENAMED_PROVIDERS; + foreach ($renamedProviders as $oldProviderName => $newProviderName) + { + $db = e107::getDb(); + $db->insert('user', [ + 'user_loginname' => $oldProviderName . '012345', + 'user_name' => $oldProviderName . '012345', + 'user_password' => '559b3b2f2d54b647ae7a5beb5c8c36c3', + 'user_email' => '', + 'user_xup' => $oldProviderName . '_ThisSegmentDoesNotMatter', + ]); + $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($newProviderName . '_ThisSegmentDoesNotMatter', $result['user_xup']); + $this->assertFalse($social_setup->upgrade_required()); + } } /** @@ -38,6 +71,7 @@ class social_setupTest extends \Codeception\Test\Unit $db = e107::getDb(); $db->insert('user', [ 'user_loginname' => 'SteambB8047', + 'user_name' => 'SteambB8047', 'user_password' => '$2y$10$.u22u/U392cUhvJm2DJ57.wsKtxKKj3WsZ.x6LsXoUVHVuprZGgUu', 'user_email' => '', 'user_xup' => 'Steam_https://steamcommunity.com/openid/id/76561198006790310',