From be84cf7c0800b0e6adb51d8b2c527ed4bb97fc2f Mon Sep 17 00:00:00 2001 From: Nick Liu Date: Mon, 24 Feb 2020 22:50:48 +0100 Subject: [PATCH] New interface for varied social login configuration fields Uses the new e_user_provider::getFieldsOf() API --- e107_handlers/user_handler.php | 17 ++- .../social/SocialLoginConfigManager.php | 13 ++ e107_plugins/social/admin_config.php | 112 ++++++++++++------ .../languages/English/English_admin.php | 2 + e107_plugins/social/social_setup.php | 2 + e107_tests/tests/unit/e_user_providerTest.php | 1 + 6 files changed, 108 insertions(+), 39 deletions(-) diff --git a/e107_handlers/user_handler.php b/e107_handlers/user_handler.php index 443bc0186..c3400265b 100644 --- a/e107_handlers/user_handler.php +++ b/e107_handlers/user_handler.php @@ -1260,6 +1260,21 @@ class e_user_provider return $type; } + /** + * Get standard and supplementary fields of the specified provider + * @param $providerName string Name of the supported social login provider + * @return array Multidimensional associative array where the keys are the known field names and the values are a + * description of what their key is for. Keys can be nested in parent keys. Parent keys will not + * have a description of the key. All fields take a string value. Return will be empty if the + * specified provider does not have any known fields. + */ + public static function getFieldsOf($providerName) + { + $standardFields = self::getStandardFieldsOf($providerName); + $supplementaryFields = self::getSupplementalFieldsOf($providerName); + return self::array_merge_recursive_distinct($standardFields, $supplementaryFields); + } + /** * Get the standard/common/parent fields of the specified provider * @param $providerName string Name of the supported social login provider @@ -1362,7 +1377,7 @@ class e_user_provider { if (!isset($adapterTokens[$index][1])) { - if (in_array($adapterTokens[$index], [';', '.', ','])) return $carry; + if (in_array($adapterTokens[$index], [';', '.', ',', '?'])) return $carry; ++$index; return self::adapterTokenParseConfig($adapterTokens, $index, $carry); } diff --git a/e107_plugins/social/SocialLoginConfigManager.php b/e107_plugins/social/SocialLoginConfigManager.php index 2f5b76ab3..0b3ab3b75 100644 --- a/e107_plugins/social/SocialLoginConfigManager.php +++ b/e107_plugins/social/SocialLoginConfigManager.php @@ -203,6 +203,19 @@ class SocialLoginConfigManager return e_user_provider::getTypeOf($providerName); } + /** + * Get standard and supplementary fields of the specified provider + * @param $providerName string Name of the supported social login provider + * @return array Multidimensional associative array where the keys are the known field names and the values are a + * description of what their key is for. Keys can be nested in parent keys. Parent keys will not + * have a description of the key. All fields take a string value. Return will be empty if the + * specified provider does not have any known fields. + */ + public function getFieldsOf($providerName) + { + return e_user_provider::getFieldsOf($providerName); + } + /** * Get the providers that are currently configured in the core preferences * @return array String list of configured provider names diff --git a/e107_plugins/social/admin_config.php b/e107_plugins/social/admin_config.php index fb121bacc..21ff2e4cf 100644 --- a/e107_plugins/social/admin_config.php +++ b/e107_plugins/social/admin_config.php @@ -370,17 +370,13 @@ class social_ui extends e_admin_ui - - " . LAN_SOCIAL_ADMIN_04 . " " . LAN_SOCIAL_ADMIN_AUTH_TYPE . " - " . LAN_SOCIAL_ADMIN_05 . " - " . LAN_SOCIAL_ADMIN_06 . " - " . LAN_SOCIAL_ADMIN_38 . " + " . LAN_SOCIAL_ADMIN_COLUMN_CONFIGURATION . " " . LAN_SOCIAL_ADMIN_03 . " @@ -422,41 +418,27 @@ class social_ui extends e_admin_ui $provider_type "; - if ($provider_type == "OpenID") + $text .= ""; + $fieldInfo = self::array_slash($slcm->getFieldsOf($provider_name)); + foreach ($fieldInfo as $fieldSlash => $description) { - $openid_identifier = $slcm->getProviderConfig($provider_name, '/openid_identifier'); - $frm_options = ['size' => 'block-level']; - if (empty($openid_identifier)) - { - try - { - $class = "\Hybridauth\Provider\\$provider_name"; - $reflection = new ReflectionClass($class); - $properties = $reflection->getDefaultProperties(); - $frm_options['placeholder'] = $properties['openidIdentifier']; - } - catch (Exception $e) - { - $openid_identifier = ""; - } - } - $textKeys .= "" . - $frm->text("social_login[$provider_name][openid_identifier]", $openid_identifier, 256, $frm_options) . - ""; - } - else - { - $textKeys .= "" . $frm->text("social_login[$provider_name][keys][id]", $slcm->getProviderConfig($provider_name, '/keys/id'), 128, ['size' => 'block-level']); - $textKeys .= "" . $frm->text("social_login[$provider_name][keys][secret]", $slcm->getProviderConfig($provider_name, '/keys/secret'), 128, ['size' => 'block-level']); - if ($provider_type == "OAuth2" || $slcm->getProviderConfig($provider_name, '/scope')) - { - $textKeys .= "" . $frm->text("social_login[$provider_name][scope]", $slcm->getProviderConfig($provider_name, '/scope'), 128, ['size' => 'block-level']); - } - else - { - $textKeys .= ""; - } + $field = str_replace("/", "][", $fieldSlash); + $frm_options = [ + 'size' => 'block-level', + 'placeholder' => self::getPlaceholderFor($provider_name, $fieldSlash), + ]; + + $text .= "
"; + $text .= $frm->text( + "social_login[$provider_name][$field]", + $slcm->getProviderConfig($provider_name, $fieldSlash), + 256, + $frm_options + ); + $text .= "
$description
"; + $text .= "
"; } + $text .= ""; $textEnabled = $frm->radio_switch("social_login[$provider_name][enabled]", $slcm->isProviderEnabled($provider_name), '', '', ['class' => 'e-expandit']); @@ -469,6 +451,60 @@ class social_ui extends e_admin_ui return $text; } + /** + * Based on Illuminate\Support\Arr::dot() + * @copyright Copyright (c) Taylor Otwell + * @license https://github.com/illuminate/support/blob/master/LICENSE.md MIT License + * @param $array + * @param string $prepend + * @return array + */ + private static function array_slash($array, $prepend = '') + { + $results = []; + + foreach ($array as $key => $value) + { + if (is_array($value) && !empty($value)) + { + $results = array_merge($results, static::array_slash($value, $prepend . $key . '/')); + } + else + { + $results[$prepend . $key] = $value; + } + } + + return $results; + } + + private static function getPlaceholderFor($providerName, $fieldSlash) + { + switch ($fieldSlash) + { + case "scope": + $propertyName = "scope"; + break; + case "openid_identifier": + $propertyName = "openidIdentifier"; + break; + default: + $propertyName = ""; + } + + try + { + $class = "\Hybridauth\Provider\\$providerName"; + $reflection = new ReflectionClass($class); + $properties = $reflection->getDefaultProperties(); + return isset($properties[$propertyName]) ? $properties[$propertyName] : null; + } + catch (ReflectionException $e) + { + return null; + } + } + private function generateAdminFormJs() { return <<database update is required to continue using this plugin." ); +define("LAN_SOCIAL_ADMIN_COLUMN_CONFIGURATION", "Configuration"); + define("LAN_SOCIAL_ADMIN_TEST_PAGE_TOGGLE", "Test Page"); define("LAN_SOCIAL_ADMIN_TEST_PAGE_INFO", "Enable or disable the social login test page"); define("LAN_SOCIAL_ADMIN_08", "Note: In most cases, you will need to obtain an application ID and secret key from social login providers.\nIf a provider's name is a link, that link should take you to the login application configuration documentation.\n\nYou may test your configuration with the following URL after enabling the \"".LAN_SOCIAL_ADMIN_TEST_PAGE_TOGGLE."\" option:"); diff --git a/e107_plugins/social/social_setup.php b/e107_plugins/social/social_setup.php index b89ac9ec8..5e7aaf2f6 100644 --- a/e107_plugins/social/social_setup.php +++ b/e107_plugins/social/social_setup.php @@ -42,6 +42,7 @@ class social_setup $actualNormalizedProviderName = $manager->normalizeProviderName($denormalizedProviderName); $newOptions = $oldOptions; + /* Commented out because there are no known options to migrate from HybridAuth 2 to Hybridauth 3 if (isset($newOptions['keys']['key'])) { $newOptions['keys']['id'] = $newOptions['keys']['key']; @@ -55,6 +56,7 @@ class social_setup "Updated configuration format of social login provider $denormalizedProviderName" ); } + */ if ($actualNormalizedProviderName !== $oldNormalizedProviderName) { diff --git a/e107_tests/tests/unit/e_user_providerTest.php b/e107_tests/tests/unit/e_user_providerTest.php index de8eddfa8..263ae0e4e 100644 --- a/e107_tests/tests/unit/e_user_providerTest.php +++ b/e107_tests/tests/unit/e_user_providerTest.php @@ -109,6 +109,7 @@ class e_user_providerTest extends \Codeception\Test\Unit $result = e_user_provider::getSupplementalFieldsOf("Twitter"); $this->assertTrue(array_key_exists('authorize', $result)); $this->assertTrue(array_key_exists('photo_size', $result)); + $this->assertIsNotArray($result['photo_size']); $result = e_user_provider::getSupplementalFieldsOf("Vkontakte"); $this->assertTrue(array_key_exists('photo_size', $result));