Skip to content

Commit

Permalink
Merge pull request #267 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
2.4 develop
  • Loading branch information
haitv282 committed Sep 30, 2022
2 parents 3977197 + bb597ef commit 72a0dd2
Show file tree
Hide file tree
Showing 58 changed files with 561 additions and 1,560 deletions.
131 changes: 124 additions & 7 deletions Controller/Social/AbstractSocial.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,27 @@
abstract class AbstractSocial extends Action
{
/**
* @type Session
* @var Session
*/
protected $session;

/**
* @type StoreManagerInterface
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @type AccountManagementInterface
* @var AccountManagementInterface
*/
protected $accountManager;

/**
* @type SocialHelper
* @var SocialHelper
*/
protected $apiHelper;

/**
* @type Social
* @var Social
*/
protected $apiObject;

Expand Down Expand Up @@ -159,7 +159,7 @@ public function createCustomerProcess($userProfile, $type)
{
$name = explode(' ', $userProfile->displayName ?: __('New User'));
if (strtolower($type) === 'steam') {
$userProfile->identifier = trim($userProfile->identifier,"https://steamcommunity.com/openid/id/");
$userProfile->identifier = trim($userProfile->identifier, "https://steamcommunity.com/openid/id/");
}
$user = array_merge(
[
Expand All @@ -168,7 +168,8 @@ public function createCustomerProcess($userProfile, $type)
'lastname' => $userProfile->lastName ?: (array_shift($name) ?: $userProfile->identifier),
'identifier' => $userProfile->identifier,
'type' => $type,
'password' => isset($userProfile->password) ? $userProfile->password : null
'password' => isset($userProfile->password) ? $userProfile->password :
$this->getRequest()->getParam('password')
],
$this->getUserData($userProfile)
);
Expand Down Expand Up @@ -346,4 +347,120 @@ private function getCookieMetadataFactory()

return $this->cookieMetadataFactory;
}

/**
* @param $type
*
* @return $this|Raw|void
* @throws FailureToSendException
* @throws InputException
* @throws LocalizedException
*/
public function login($type)
{
try {
if (!$type) {
$type = $this->apiObject->getProviderConnected();
}
$userProfile = $this->apiObject->getUserProfile($type);
if (!$userProfile->identifier) {
return $this->emailRedirect($type);
}
} catch (Exception $e) {
$this->setBodyResponse($e->getMessage());

return;
}

$customer = $this->apiObject->getCustomerBySocial($userProfile->identifier, $type);
$customerData = $this->customerModel->load($customer->getId());

if (!$customer->getId()) {
$requiredMoreInfo = (int) $this->apiHelper->requiredMoreInfo();

if ((!$userProfile->email && $requiredMoreInfo === 2) || $requiredMoreInfo === 1) {
$this->session->setUserProfile($userProfile);

return $this->_appendJs(
sprintf(
"<script>window.close();window.opener.fakeEmailCallback('%s','%s','%s');</script>",
$type,
$userProfile->firstName,
$userProfile->lastName
)
);
}

$customer = $this->createCustomerProcess($userProfile, $type);
} elseif ($this->apiHelper->isCheckMode() && $customerData->getData('password_hash') === null) {
$this->session->setUserProfile($userProfile);

return $this->_appendJs(
sprintf(
"<script>window.close();window.opener.fakeEmailCallback('%s','%s','%s');</script>",
$type,
$userProfile->firstName,
$userProfile->lastName
)
);

}
$this->refresh($customer);

return $this->_appendJs();
}

/**
* @param $key
* @param null $value
*
* @return bool|mixed
*/
public function checkRequest($key, $value = null)
{
$param = $this->getRequest()->getParam($key, false);

if ($value) {
return $param === $value;
}

return $param;
}

/**
* @return bool
*/
public function checkCustomerLogin()
{
return true;
}

/**
* @param $message
*/
protected function setBodyResponse($message)
{
$content = '<html><head></head><body>';
$content .= '<div class="message message-error">' . __('Ooophs, we got an error: %1', $message) . '</div>';
$content .= <<<Style
<style type="text/css">
.message{
background: #fffbbb;
border: none;
border-radius: 0;
color: #333333;
font-size: 1.4rem;
margin: 0 0 10px;
padding: 1.8rem 4rem 1.8rem 1.8rem;
position: relative;
text-shadow: none;
}
.message-error{
background:#ffcccc;
}
</style>
Style;
$content .= '</body></html>';
$this->getResponse()->setBody($content);
}
}
41 changes: 16 additions & 25 deletions Controller/Social/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@

namespace Mageplaza\SocialLogin\Controller\Social;

use Hybrid_Endpoint;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Stdlib\Cookie\FailureToSendException;

/**
* Class Callback
Expand All @@ -31,15 +36,21 @@
class Callback extends AbstractSocial
{
/**
* @inheritdoc
* @return ResponseInterface|Raw|ResultInterface|Callback|void
*
* @throws FailureToSendException
* @throws InputException
* @throws LocalizedException
*/
public function execute()
{
$param = $this->getRequest()->getParams();

if (isset($param['live.php'])) {
$request = array_merge($param, ['hauth_done' => 'Live']);
$param = array_merge($param, ['hauth_done' => 'Live']);
}

$type = $param['hauth_done'] ?? '';

if ($this->checkRequest('hauth_start', false)
&& (($this->checkRequest('error_reason', 'user_denied')
&& $this->checkRequest('error', 'access_denied')
Expand All @@ -49,27 +60,7 @@ public function execute()
) {
return $this->_appendJs(sprintf('<script>window.close();</script>'));
}
if (isset($request)) {
Hybrid_Endpoint::process($request);
}

Hybrid_Endpoint::process();
}

/**
* @param $key
* @param null $value
*
* @return bool|mixed
*/
public function checkRequest($key, $value = null)
{
$param = $this->getRequest()->getParam($key, false);

if ($value) {
return $param === $value;
}

return $param;
return $this->login($type);
}
}
60 changes: 20 additions & 40 deletions Controller/Social/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

namespace Mageplaza\SocialLogin\Controller\Social;

use Exception;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
Expand Down Expand Up @@ -143,7 +142,7 @@ public function execute()
*/
$resultJson = $this->resultJsonFactory->create();
$params = $this->getRequest()->getParams();
$type = $this->apiHelper->setType($params['type']);
$type = $this->apiHelper->setType($params['type'] ?? "");

if (!$type) {
$this->_forward('noroute');
Expand All @@ -156,56 +155,37 @@ public function execute()
$firstname = isset($params['firstname']) ? $params['firstname'] : null;
$lastname = isset($params['lastname']) ? $params['lastname'] : null;
$password = isset($params['password']) ? $this->_encrypt->getHash($params['password'], true) : null;
$customer = $this->customerFactory->create()
->setWebsiteId($this->getStore()->getWebsiteId())
->loadByEmail($realEmail);

if ($customer->getId()) {
$result['message'] = __('Email already exists');

return $resultJson->setData($result);
if ($realEmail) {
$customer = $this->customerFactory->create()
->setWebsiteId($this->getStore()->getWebsiteId())
->loadByEmail($realEmail);
if ($customer->getId()) {
$result['message'] = __('Email already exists');

return $resultJson->setData($result);
}
}

$userProfile = $this->session->getUserProfile();
$userProfile->email = $realEmail ?: $userProfile->email;
$userProfile->firstName = $firstname ?: $userProfile->firstName;
$userProfile->lastName = $lastname ?: $userProfile->lastName;
$userProfile->password = $password ?: null;

$checkCustomer = $this->customerFactory->create()
->setWebsiteId($this->getStore()->getWebsiteId())
->loadByEmail($userProfile->email);

if ($checkCustomer->getId()) {
if ($userProfile->hash !== '') {
$loginTrial = $this->accountManager;
$session = $this->session;

try {
$customer = $loginTrial->authenticate($userProfile->email, $params['password']);
} catch (Exception $e) {
$result['message'] = __('Wrong password');

return $resultJson->setData($result);
}

$session->setCustomerDataAsLoggedIn($customer);
$session->regenerateId();
} else {
$session = $this->session;
$customerRepositoryInterface = $this->_customerRepositoryInterface;
$customerId = $customerRepositoryInterface->get(
$userProfile->email,
$websiteId = null
)->getId();
$customer = $customerRepositoryInterface->getById($customerId);
$customerRegistry = $this->_customerRegistry;
$customerSecure = $customerRegistry->retrieveSecureData($customerId);
$customerSecure->setPasswordHash($userProfile->password);
$customerRepositoryInterface->save($customer);
$session->setCustomerDataAsLoggedIn($customer);
$session->regenerateId();
}
$session = $this->session;
$customerRepositoryInterface = $this->_customerRepositoryInterface;
$customerId = $customerRepositoryInterface->get($userProfile->email)->getId();
$customer = $customerRepositoryInterface->getById($customerId);
$customerRegistry = $this->_customerRegistry;
$customerSecure = $customerRegistry->retrieveSecureData($customerId);
$customerSecure->setPasswordHash($password);
$customerRepositoryInterface->save($customer);
$session->setCustomerDataAsLoggedIn($customer);
$session->regenerateId();
} else {
$customer = $this->createCustomerProcess($userProfile, $type);
$this->refresh($customer);
Expand Down

0 comments on commit 72a0dd2

Please sign in to comment.