| Current Path : /var/www/html/plugins/system/jchatlogin/connectors/ |
| Current File : /var/www/html/plugins/system/jchatlogin/connectors/google.php |
<?php
/**
* Manage login/logout for social networks connect
* @package JCHAT::plugins::system
* @subpackage jchatlogin
* @author Joomla! Extensions Store
* @copyright (C) 2024 - Joomla! Extensions Store
* @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
*/
defined ( '_JEXEC' ) or die ();
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Factory;
use Joomla\CMS\User\UserHelper;
use Joomla\CMS\Filter\InputFilter;
use JExtstore\Component\JChat\Administrator\Framework\Helpers\Users as JChatHelpersUsers;
/**
* Google connector for the social login
*
* @package JCHAT::plugins::system
* @subpackage jchatlogin
* @since 2.1
*/
class JChatLoginConnectorGoogle extends JChatLoginConnector {
/**
* Main connector function
*
* @access public
* @return Void
*/
public function execute() {
// Google Plus Login URL generation - Execute only if GPlus login is enabled
try {
// Generate GPlus login URL if the user is a guest
if ($this->joomlaUserObject->guest && ( bool ) !$this->app->getInput()->get->getString ( 'code', false )) {
$googleAPI = new \JChatGoogleLogin($this->appId, $this->secret, Uri::base(), true);
$gplusLoginURL = $googleAPI->init()->getLoginUrl();
$doc = $this->app->getDocument();
$doc->getWebAssetManager()->addInlineScript ( "var jchatGPlusLoginURL = '$gplusLoginURL';" );
}
} catch ( \Exception $e ) {
$this->app->enqueueMessage(Text::sprintf('COM_JCHAT_ERROR_GPLUS_LOGIN', $e->getMessage ()), 'warning');
}
// Google Plus Login - Execute only after link click to perform Google login, after Google redirects back
if ($this->joomlaUserObject->guest && ( bool ) $this->app->getInput()->get->getString ( 'code', false )) {
try {
$googleAPI = new \JChatGoogleLogin($this->appId, $this->secret, Uri::base(), true);
$googleUserObject = $googleAPI->init()->loginCallback($this->app->getInput()->get->getString ( 'code' ));
// Check if users exists already in the Joomla database using email address as primary key, retrieve user id if exists
$authType = $this->cParams->get('auth_type', 'id');
$joomlaIdentifier = $authType == 'id' ? $googleUserObject->getUid() : $googleUserObject->getEmailAddress();
$alreadyCreatedJoomlaUserID = JChatHelpersUsers::getJoomlaId ( $joomlaIdentifier, $authType );
if (! $alreadyCreatedJoomlaUserID) {
// Collect user info
$name = $googleUserObject->getFirstname() . ' ' . $googleUserObject->getlastname();
$username = InputFilter::getInstance()->clean($googleUserObject->getUsername(), 'username');
$password = UserHelper::genRandomPassword ( 5 );
$email = $googleUserObject->getEmailAddress();
// Normalize info array
$fbUserProfileArray ['id'] = $googleUserObject->getUid();
$fbUserProfileArray ['email'] = $email;
$fbUserProfileArray ['picture'] = $googleUserObject->getPicture();
$fbUserProfileArray ['first_name'] = $googleUserObject->getFirstname();
$fbUserProfileArray ['last_name'] = $googleUserObject->getLastname();
$fbUserProfileArray ['name'] = $name;
// Trigger to create a new Joomla user aggregating data from Facebook user profile, pre-populate bind $this->joomlaUserObject
$this->onNewUser(
$this->joomlaUserObject,
$name,
$username,
$password,
$email,
$fbUserProfileArray,
$this->cParams
);
// Do instant Joomla login authentication with new user, aggregated data and random generated password only for login purpouse
$this->onLogin(
$this->joomlaUserObject,
$this->cParams
);
} else {
// get already populated $this->joomlaUserObject
$this->joomlaUserObject = Factory::getContainer()->get(\Joomla\CMS\User\UserFactoryInterface::class)->loadUserById ( $alreadyCreatedJoomlaUserID );
// Do instant Joomla login authentication with new user, aggregated data and random generated password only for login purpouse
$this->onLogin(
$this->joomlaUserObject,
$this->cParams
);
}
} catch ( \Exception $e ) {
$this->app->enqueueMessage(Text::sprintf('COM_JCHAT_ERROR_GPLUS_LOGIN', $e->getMessage ()), 'warning');
}
}
}
/**
* Class Constructor
* @param Object $cParams
*
* @access public
*/
public function __construct($cParams) {
parent::__construct($cParams);
$this->appId = $this->cParams->get('gplusClientID', null);
$this->secret = $this->cParams->get('gplusKey', null);
// Load framework classes without autoloading
require_once JPATH_ROOT . '/plugins/system/jchatlogin/social/google/login.php';
require_once JPATH_ROOT . '/plugins/system/jchatlogin/social/google/user.php';
}
}