1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-02 12:48:26 +02:00

New interface for varied social login configuration fields

Uses the new e_user_provider::getFieldsOf() API
This commit is contained in:
Nick Liu
2020-02-24 22:50:48 +01:00
parent daa31bef56
commit be84cf7c08
6 changed files with 108 additions and 39 deletions

View File

@@ -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);
}

View File

@@ -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

View File

@@ -370,17 +370,13 @@ class social_ui extends e_admin_ui
<col style='width:10%' />
<col style='width:5%' />
<col class='col-control' />
<col class='col-control' />
<col class='col-control' />
<col style='width:5%' />
</colgroup>
<thead>
<tr>
<th>" . LAN_SOCIAL_ADMIN_04 . "</th>
<th>" . LAN_SOCIAL_ADMIN_AUTH_TYPE . "</th>
<th>" . LAN_SOCIAL_ADMIN_05 . "</th>
<th>" . LAN_SOCIAL_ADMIN_06 . "</th>
<th>" . LAN_SOCIAL_ADMIN_38 . "</th>
<th>" . LAN_SOCIAL_ADMIN_COLUMN_CONFIGURATION . "</th>
<th class='center'>" . LAN_SOCIAL_ADMIN_03 . "</th>
</tr>
</thead>
@@ -422,41 +418,27 @@ class social_ui extends e_admin_ui
<td>$provider_type</td>
";
if ($provider_type == "OpenID")
$text .= "<td>";
$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 .= "<td>" .
$frm->text("social_login[$provider_name][openid_identifier]", $openid_identifier, 256, $frm_options) .
"</td><td></td><td></td>";
}
else
{
$textKeys .= "<td>" . $frm->text("social_login[$provider_name][keys][id]", $slcm->getProviderConfig($provider_name, '/keys/id'), 128, ['size' => 'block-level']);
$textKeys .= "<td>" . $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 .= "<td>" . $frm->text("social_login[$provider_name][scope]", $slcm->getProviderConfig($provider_name, '/scope'), 128, ['size' => 'block-level']);
}
else
{
$textKeys .= "<td></td>";
}
$field = str_replace("/", "][", $fieldSlash);
$frm_options = [
'size' => 'block-level',
'placeholder' => self::getPlaceholderFor($provider_name, $fieldSlash),
];
$text .= "<div><label>$fieldSlash</label>";
$text .= $frm->text(
"social_login[$provider_name][$field]",
$slcm->getProviderConfig($provider_name, $fieldSlash),
256,
$frm_options
);
$text .= "<div class='smalltext field-help'>$description</div>";
$text .= "</div>";
}
$text .= "</td>";
$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 <<<EOD

View File

@@ -55,6 +55,8 @@ define("LAN_SOCIAL_UPDATE_REQUIRED",
"A <a href=\"" . e_ADMIN_ABS . "e107_update.php\">database update</a> 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:");

View File

@@ -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)
{

View File

@@ -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));