Skip to content

Commit

Permalink
Merge pull request #269 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
2.4 develop
  • Loading branch information
haitv282 authored Nov 4, 2022
2 parents 72a0dd2 + cebdbf9 commit b48612f
Show file tree
Hide file tree
Showing 9 changed files with 461 additions and 15 deletions.
105 changes: 105 additions & 0 deletions Block/DataDeletion/DeleteData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_SocialLogin
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\SocialLogin\Block\DataDeletion;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Mageplaza\SocialLogin\Helper\Data as DataHelper;
use Mageplaza\SocialLogin\Helper\Social as SocialHelper;

/**
* Class DeleteData
* @package Mageplaza\SocialLogin\Block
*/
class DeleteData extends Template
{
/**
* @type DataHelper
*/
protected $_helper;

/**
* @var SocialHelper
*/
protected $_socialHelper;

/**
* DeleteData constructor.
*
* @param Context $context
* @param DataHelper $helper
* @param SocialHelper $socialHelper
* @param array $data
*/
public function __construct(
Context $context,
DataHelper $helper,
SocialHelper $socialHelper,
array $data = []
) {
$this->_helper = $helper;
$this->_socialHelper = $socialHelper;

parent::__construct($context, $data);
}

/**
* @return DataHelper
*/
public function helper()
{
return $this->_helper;
}

/**
* Check can ShowConfirm
*/
public function isShowConfirm()
{
$type = $this->getRequest()->getParam('type');
try {
if ($type && $this->_socialHelper->getDeleteDataUrl($type)) {
return true;
}
} catch (LocalizedException $e) {

return false;
}

return false;
}

/**
* @return string
*/
public function getStoreName()
{
try {
return $this->_storeManager->getStore()->getName();
} catch (NoSuchEntityException $e) {
return '';
}
}

}
5 changes: 4 additions & 1 deletion Block/System/RedirectUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ protected function _getElementHtml(AbstractElement $element)
{
$elementId = explode('_', $element->getHtmlId());
$redirectUrl = $this->socialHelper->getAuthUrl($elementId[1]);
$html = '<input style="opacity:1;" readonly id="' . $element->getHtmlId() . '" class="input-text admin__control-text" value="' . $redirectUrl . '" onclick="this.select()" type="text">';
if (isset($elementId[2]) && $elementId[2] === 'delete') {
$redirectUrl = $this->socialHelper->getDeleteDataUrl($elementId[1]);
}
$html = '<input style="opacity:1;" readonly id="' . $element->getHtmlId() . '" class="input-text admin__control-text" value="' . $redirectUrl . '" onclick="this.select()" type="text">';

return $html;
}
Expand Down
183 changes: 183 additions & 0 deletions Controller/Social/DataDeletion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_SocialLogin
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\SocialLogin\Controller\Social;

use Exception;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Store\Model\StoreManagerInterface;
use Mageplaza\SocialLogin\Helper\Social as SocialHelper;
use Mageplaza\SocialLogin\Model\Social;
use Psr\Log\LoggerInterface;

/**
* Class DataDeletion
* @package Mageplaza\SocialLogin\Controller\Social
*/
class DataDeletion extends AbstractSocial implements CsrfAwareActionInterface
{
/**
* @var LoggerInterface
*/
protected $_logger;

/**
* DataDeletion constructor.
*
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param AccountManagementInterface $accountManager
* @param SocialHelper $apiHelper
* @param Social $apiObject
* @param Session $customerSession
* @param AccountRedirect $accountRedirect
* @param RawFactory $resultRawFactory
* @param Customer $customerModel
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
AccountManagementInterface $accountManager,
SocialHelper $apiHelper,
Social $apiObject,
Session $customerSession,
AccountRedirect $accountRedirect,
RawFactory $resultRawFactory,
Customer $customerModel,
LoggerInterface $logger
) {
parent::__construct(
$context,
$storeManager,
$accountManager,
$apiHelper,
$apiObject,
$customerSession,
$accountRedirect,
$resultRawFactory,
$customerModel
);
$this->_logger = $logger;

}

/**
* @return ResponseInterface|ResultInterface|void
*/
public function execute()
{
$param = $this->getRequest()->getParams();

if (isset($param['type']) && $param['type'] === 'facebook' && isset($param['signed_request'])) {
$signed_request = $param['signed_request'];
$data = $this->parseSignedRequest($signed_request);
if ($data && $data['user_id']) {
$this->apiObject->load($data['user_id'], 'social_id');
try {
$this->apiObject->delete();
} catch (Exception $e) {
$this->_logger->warning($e->getMessage());

return $this->getResponse()->representJson('');
}
}
$confirmUrl = $this->_url->getUrl(
'sociallogin/social/datadeletion/',
['type' => 'facebook', 'id' => $data['user_id']]
);
$response = [
'url' => $confirmUrl,
'confirmation_code' => $data['user_id'],
];
$response = json_encode($response, JSON_UNESCAPED_SLASHES);

return $this->getResponse()->representJson($response);
}
if (isset($param['type']) && isset($param['id'])) {
$paramsConfirm = [
'id' => $param['id'],
'type' => $param['type'],
];
$this->_forward('index', 'index', 'cms', $paramsConfirm);

return;
}

return $this->getResponse()->representJson('');
}

/**
* @param string $signedRequest
*
* @return mixed|null
*/
public function parseSignedRequest($signedRequest)
{
[$encoded_sig, $payload] = explode('.', $signedRequest, 2);

$this->apiHelper->setType('facebook');
$secret = $this->apiHelper->getAppSecret();
$sig = $this->base64UrlDecode($encoded_sig);
$data = json_decode($this->base64UrlDecode($payload), true);
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
return null;
}

return $data;
}

/**
* @param $input
*
* @return false|string
*/
public function base64UrlDecode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}

/**
* @inheritDoc
*/
public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
{
return null;
}

/**
* @inheritDoc
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
}
36 changes: 34 additions & 2 deletions Helper/Social.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ public function getAuthUrl($type)
return $authUrl . ($param ? (strpos($authUrl, '?') ? '&' : '?') . $param : '');
}

/**
* @param $type
*
* @return string
* @throws LocalizedException
*/
public function getDeleteDataUrl($type)
{
$authUrl = $this->getBaseDelete();
$type = $this->setType($type);

return $authUrl . 'type/' . strtolower($type);
}

/**
* @return string
* @throws LocalizedException
Expand All @@ -208,7 +222,25 @@ public function getBaseAuthUrl()
[
'_nosid' => true,
'_scope' => $storeId,
'_secure' => true
'_secure' => true,
]
);
}

/**
* @return string
* @throws LocalizedException
*/
public function getBaseDelete()
{
$storeId = $this->getScopeUrl();

return $this->_getUrl(
'sociallogin/social/datadeletion',
[
'_nosid' => true,
'_scope' => $storeId,
'_secure' => true,
]
);
}
Expand Down Expand Up @@ -244,7 +276,7 @@ public function getSocialTypesArray()
'vkontakte' => 'Vkontakte',
'github' => 'Github',
'live' => 'Live',
'zalo' => 'Zalo'
'zalo' => 'Zalo',
];
}
}
Loading

0 comments on commit b48612f

Please sign in to comment.