From 23158c8f3cf29db4d3b07c25565de4152206677d Mon Sep 17 00:00:00 2001 From: VANCLOOSTER Mickael Date: Mon, 2 Oct 2017 10:01:07 +0200 Subject: [PATCH] Refactorize some parts && bug fixe --- .../Service/SocialAuthenticationService.php | 44 +++++----- Classes/Utility/AuthUtility.php | 12 ++- composer.json | 2 +- ext_emconf.php | 2 +- ext_localconf.php | 86 ++++++++++--------- 5 files changed, 79 insertions(+), 67 deletions(-) diff --git a/Classes/Service/SocialAuthenticationService.php b/Classes/Service/SocialAuthenticationService.php index 3ad67e0..cc55980 100755 --- a/Classes/Service/SocialAuthenticationService.php +++ b/Classes/Service/SocialAuthenticationService.php @@ -62,17 +62,8 @@ class SocialAuthenticationService extends AbstractAuthenticationService protected $extConfig = []; /** - * Login data as passed to initAuth() - */ - protected $loginData = []; - - /** - * A reference to the calling object - * - * @var AbstractUserAuthentication + * @var array */ - protected $parentObject; - protected $arrayProvider = [ 'facebook' => 1, 'google' => 2, @@ -100,10 +91,13 @@ class SocialAuthenticationService extends AbstractAuthenticationService protected $signalSlotDispatcher; /** - * 100 / 101 Authenticated / Not authenticated -> in each case go on with additonal auth + * true - this service was able to authenticate the user */ - const STATUS_AUTHENTICATION_SUCCESS_CONTINUE = 100; - const STATUS_AUTHENTICATION_FAILURE_CONTINUE = 101; + const STATUS_AUTHENTICATION_SUCCESS_CONTINUE = true; + /** + * 100 + */ + const STATUS_AUTHENTICATION_FAILURE_CONTINUE = 100; /** * 200 - authenticated and no more checking needed - useful for IP checking without password */ @@ -111,7 +105,7 @@ class SocialAuthenticationService extends AbstractAuthenticationService /** * FALSE - this service was the right one to authenticate the user but it failed */ - const STATUS_AUTHENTICATION_FAILURE_BREAK = 0; + const STATUS_AUTHENTICATION_FAILURE_BREAK = false; /** * @return bool @@ -141,10 +135,6 @@ public function init() public function initAuth($subType, $loginData, $authenticationInformation, $parentObject) { $this->authUtility = $this->objectManager->get(\MV\SocialAuth\Utility\AuthUtility::class); - // Store login and authetication data - $this->loginData = $loginData; - $this->authenticationInformation = $authenticationInformation; - $this->parentObject = $parentObject; parent::initAuth($subType, $loginData, $authenticationInformation, $parentObject); } @@ -176,7 +166,6 @@ protected function initTSFE() public function getUser() { $user = $fileObject = null; - session_start(); // then grab the user profile if ($this->provider && $this->isServiceAvailable()) { //get user @@ -197,7 +186,6 @@ public function getUser() 'name' => $this->cleanData($hybridUser->displayName), 'first_name' => $this->cleanData($hybridUser->firstName), 'last_name' => $this->cleanData($hybridUser->lastName), - 'username' => $this->cleanData($hybridUser->displayName), 'password' => $password, 'email' => $this->cleanData($hybridUser->email), 'telephone' => $this->cleanData($hybridUser->phone), @@ -208,6 +196,12 @@ public function getUser() 'tx_socialauth_identifier' => $this->cleanData($hybridUser->identifier), 'tx_socialauth_source' => $this->arrayProvider[$this->provider] ]; + //username + if (!empty($hybridUser->email)) { + $fields['username'] = $hybridUser->email; + } else { + $fields['username'] = $this->cleanData($hybridUser->displayName, true); + } //grab image if (!empty($hybridUser->photoURL)) { $uniqueName = strtolower($this->provider . '_' . $hybridUser->identifier) . '.jpg'; @@ -277,7 +271,7 @@ public function authUser(&$user) return self::STATUS_AUTHENTICATION_FAILURE_CONTINUE; } $result = self::STATUS_AUTHENTICATION_FAILURE_CONTINUE; - if ($user) { + if ($user && $this->authUtility->isConnectedWithProvider($this->provider)) { $result = self::STATUS_AUTHENTICATION_SUCCESS_BREAK; } //signal slot authUser @@ -286,7 +280,6 @@ public function authUser(&$user) return $result; } - /** * Returns TRUE if single sign on for the given provider is enabled in ext_conf and is available * @@ -357,7 +350,7 @@ protected function createFileReferenceFromFalFileObject($file, $userUid) * @param string $str * @return string */ - protected function cleanData($str) + protected function cleanData($str, $forUsername = false) { $str = strip_tags($str); //Remove extra spaces @@ -368,6 +361,11 @@ protected function cleanData($str) $str = utf8_encode($str); } + if (true === $forUsername) { + $str = str_replace(' ', '', $str); + $str = mb_strtolower($str, 'utf-8'); + } + return $str; } } diff --git a/Classes/Utility/AuthUtility.php b/Classes/Utility/AuthUtility.php index 54ce16b..6d4a765 100755 --- a/Classes/Utility/AuthUtility.php +++ b/Classes/Utility/AuthUtility.php @@ -109,7 +109,6 @@ public function initializeObject() /** * @param string $provider - * @param string $returnTo * * @return \Hybrid_User_Profile|FALSE */ @@ -161,8 +160,19 @@ public function authenticate($provider) } } + /** + * @param string $provider + * + * @return boolean + */ + public function isConnectedWithProvider($provider) + { + return $this->hybridAuth->isConnectedWith($provider); + } + /** * logout from all providers when typo3 logout takes place + * return void */ public function logout() { diff --git a/composer.json b/composer.json index fe0892e..6aa26fa 100755 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ], "homepage": "http://typo3.org", "license": ["GPL-2.0+"], - "version": "1.1.8", + "version": "1.1.9", "require": { "hybridauth/hybridauth": "~2.9.0" }, diff --git a/ext_emconf.php b/ext_emconf.php index 963ba16..6ad2cda 100755 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -21,7 +21,7 @@ 'uploadfolder' => 0, 'createDirs' => '', 'clearCacheOnLoad' => 0, - 'version' => '1.1.8', + 'version' => '1.1.9', 'constraints' => array( 'depends' => array( 'typo3' => '6.2.0-8.7.99' diff --git a/ext_localconf.php b/ext_localconf.php index c2caa95..5205b35 100755 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -2,51 +2,55 @@ if (!defined('TYPO3_MODE')) { die('Access denied.'); } - -$composerAutoloadFile = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'Resources/Private/Librairies/autoload.php'; -if (file_exists($composerAutoloadFile)) { - require_once($composerAutoloadFile); -} - -if (TYPO3_MODE === 'FE') { - \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( - 'MV.' . $_EXTKEY, - 'Pi1', - array( - 'Auth' => 'list, connect, endpoint', - ), - // non-cacheable actions - array( - 'Auth' => 'connect, endpoint' - ) - ); - $extConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['social_auth']); - if ($extConfig['providers.']['facebook.']['enabled'] || $extConfig['providers.']['google.']['enabled'] || $extConfig['providers.']['twitter.']['enabled']) { - $GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth']['setup']['FE_fetchUserIfNoSession'] = true; - $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_post_processing'][$_EXTKEY] = 'MV\SocialAuth\Hooks\LogOffHook->postProcessing'; +$boot = function ($_EXTKEY) { + $composerAutoloadFile = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'Resources/Private/Librairies/autoload.php'; + if (file_exists($composerAutoloadFile)) { + require_once($composerAutoloadFile); + } + if (TYPO3_MODE === 'FE') { + \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( + 'MV.' . $_EXTKEY, + 'Pi1', + array( + 'Auth' => 'list, connect, endpoint', + ), + // non-cacheable actions + array( + 'Auth' => 'connect, endpoint' + ) + ); + $extConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['social_auth']); + if ($extConfig['providers.']['facebook.']['enabled'] || $extConfig['providers.']['google.']['enabled'] || $extConfig['providers.']['twitter.']['enabled']) { + $GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth']['setup']['FE_fetchUserIfNoSession'] = true; + $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_post_processing'][$_EXTKEY] = 'MV\SocialAuth\Hooks\LogOffHook->postProcessing'; + } //add marker to felogin if is loaded if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('felogin')) { $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['postProcContent'][$_EXTKEY] = 'MV\SocialAuth\Hooks\FeLoginHook->postProcContent'; } } -} -\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService($_EXTKEY, - 'auth' /* sv type */, - MV\SocialAuth\Service\SocialAuthenticationService::class /* sv key */, - array( - 'title' => 'Social Authentification Service', - 'description' => 'authentication for users from social providers (facebook, twitter...)', - 'subtype' => 'authUserFE,getUserFE', - 'available' => true, - 'priority' => 82, /* will be called before default typo3 authentication service */ - 'quality' => 82, - 'os' => '', - 'exec' => '', - 'className' => MV\SocialAuth\Service\SocialAuthenticationService::class, - ) -); + \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService($_EXTKEY, + 'auth' /* sv type */, + MV\SocialAuth\Service\SocialAuthenticationService::class /* sv key */, + array( + 'title' => 'Social Authentification Service', + 'description' => 'authentication for users from social providers (facebook, twitter...)', + 'subtype' => 'authUserFE,getUserFE', + 'available' => true, + 'priority' => 82, /* will be called before default typo3 authentication service */ + 'quality' => 82, + 'os' => '', + 'exec' => '', + 'className' => MV\SocialAuth\Service\SocialAuthenticationService::class, + ) + ); + + #Exclude some params + $GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'tx_socialauth_pi1[provider]'; + $GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'tx_socialauth_pi1[redirect]'; +}; + +$boot($_EXTKEY); +unset($boot); -#Exclude some params -$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'tx_socialauth_pi1[provider]'; -$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'tx_socialauth_pi1[redirect]'; \ No newline at end of file