Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tig-robertgrundeken committed Sep 23, 2021
2 parents 7a6569c + cdc8a26 commit 09066a6
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Block/Adminhtml/Config/Support/Tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Tab extends Template implements RendererInterface
{
const MODULE_NAME = 'TIG_TinifyCDN';

const EXTENSION_VERSION = '1.3.1';
const EXTENSION_VERSION = '1.3.2';

// @codingStandardsIgnoreLine
protected $_template = 'TIG_TinifyCDN::config/support/tab.phtml';
Expand All @@ -54,7 +54,7 @@ class Tab extends Template implements RendererInterface
'2.1' => ['5.6' => ['5', '+'], '7.0' => ['2', '5', '6', '+']],
'2.2' => ['7.0' => ['2', '5', '6', '+'], '7.1' => ['+']],
'2.3' => ['7.0' => ['2', '5', '6', '+'], '7.1' => ['+'], '7.2' => ['+'], '7.3' => ['+'], '7.4' => ['+']],
'2.4' => ['7.4' => ['+'], '8.1' => ['+']]
'2.4' => ['7.4' => ['+'], '8.1' => ['+']]
];

/** @var GeneralConfiguration */
Expand Down
16 changes: 14 additions & 2 deletions Controller/Adminhtml/Cdn/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,24 @@ public function execute()
{
$provider = $this->createTinifyProviderInstance();
$authCode = $this->getRequest()->getParam('code');
$errorCode = $this->getRequest()->getParam('error');
$this->storeId = $this->getSessionData('id');
$this->scope = $this->getSessionData('scope');

$redirect = $this->resultRedirectFactory->create();
$redirect->setPath(static::SYSTEM_CONFIG_TIG_TINIFYCDN_SECTION, [$this->scope => $this->storeId]);

if (!$authCode) {
// Received error from Tinify CDN
if (isset($errorCode)) {
$this->messageManager->addErrorMessage(
__('Authorization for use of Tinify CDN denied.')
);

return $redirect;
}

// Received no code from Tinify CDN
if (!isset($authCode)) {
$this->messageManager->addErrorMessage(
__('No authorization code provided. Direct access not allowed.')
);
Expand Down Expand Up @@ -179,6 +190,7 @@ private function saveAccessToken(TinifyProvider $provider, $authCode)

// If Authorization is successful, remove oAuth Credentials from session.
$this->unsetSessionData(static::TINIFYCDN_OAUTH_CREDENTIALS_PARAM);

}

/**
Expand All @@ -205,7 +217,7 @@ private function saveEndpoint($currentSite)

$this->messageManager->addNoticeMessage(__('Don\'t forget to save your configuration!'));

return $this->messageManager->addSuccessMessage(__('Your Tinify CDN endpoint was successfully set.'));
return $this->messageManager->addSuccessMessage(__('Your Tinify CDN endpoint was successfully set and will be visible after you save your configuration.'));
}

/**
Expand Down
18 changes: 14 additions & 4 deletions Controller/Cdn/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,26 @@ public function execute()
{
$params = $this->getRequest()->getParams();

if (!$params['code'] && !$params['state']) {
if (!isset($params['code']) && !isset($params['state'])) {
return $this->exception->throwException('Invalid request. No direct access allowed.');
}

$formKey = $params['state'];
$redirect = $this->resultRedirectFactory->create();

// Error handled by authorization controller
if (isset($params['error'])) {
$authUrl = $this->urlBuilder->createAuthorizeUrl(
[
UrlInterface::SECRET_KEY_PARAM_NAME => $params['state'],
'error' => $params['error']
]
);
$redirect->setPath($authUrl);
return $redirect;
}

$authUrl = $this->urlBuilder->createAuthorizeUrl(
[
UrlInterface::SECRET_KEY_PARAM_NAME => $formKey,
UrlInterface::SECRET_KEY_PARAM_NAME => $params['state'],
'code' => $params['code']
]
);
Expand Down
51 changes: 42 additions & 9 deletions Model/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

use Magento\Framework\HTTP\Client\Curl;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
use TIG\TinifyCDN\Model\Config\Provider\CDN\Configuration;

abstract class AbstractApi
Expand All @@ -47,35 +48,43 @@ abstract class AbstractApi
/** @var Configuration $config */
private $config;

/** @var CollectionFactory */
private $configCollection;

/**
* AbstractApi constructor.
*
* @param Curl $curl
* @param StoreManagerInterface $storeManager
* @param Configuration $config
* @param CollectionFactory $configCollection
*/
public function __construct(
Curl $curl,
StoreManagerInterface $storeManager,
Configuration $config
Configuration $config,
CollectionFactory $configCollection
) {
$this->curl = $curl;
$this->storeManager = $storeManager;
$this->config = $config;
$this->curl = $curl;
$this->storeManager = $storeManager;
$this->config = $config;
$this->configCollection = $configCollection;
}

/**
* @param string $uri
* @param string $method
* @param bool $includeToken
* @param string $uri
* @param string $method
* @param bool $includeToken
* @param int|null $storeId
*
* @return array
*/
public function call(string $uri, string $method, bool $includeToken)
public function call(string $uri, string $method, bool $includeToken, int $storeId = null)
{
$url = $this->config->getApiUrl($uri);

if ($includeToken) {
$token = $this->config->getAccessToken();
$token = $this->getAccessTokenFromConfig($storeId);
$this->curl->addHeader(Configuration::TINIFYCDN_CDN_AUTH_PARAM, 'Bearer ' . $token);
}

Expand Down Expand Up @@ -103,4 +112,28 @@ public function getStore($storeId = null)
{
return $this->storeManager->getStore($storeId);
}

/**
* @param null $storeId
*
* @return mixed|string|null
*/
public function getAccessTokenFromConfig($storeId = null)
{
$token = $this->config->getAccessToken($storeId);
if ($token != 0) {
return $token;
}

// Fallback method: Read directly from core_config_table
// Scenario: New install, token is saved in database but config cache return old value
$collection = $this->configCollection->create();
$collection->addFieldToFilter("path",['eq' => Configuration::XPATH_TINIFYCDN_CDN_ACCESS_TOKEN]);
$collection->addFieldToFilter("scope_id",['eq' => intval($storeId)]);
if($collection->count()>0){
return $collection->getFirstItem()->getData()['value'];
}

return null;
}
}
14 changes: 9 additions & 5 deletions Model/Api/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Site extends AbstractApi
*/
public function fetchSite($storeId = null)
{
$sites = $this->getAvailableSites();
$sites = $this->getAvailableSites($storeId);
/** @var \Magento\Store\Model\Store $store */
$store = $this->getStore($storeId);
$baseUrl = $store->getBaseUrl();
Expand Down Expand Up @@ -85,20 +85,24 @@ function ($properties) use ($baseUrl) {
}

/**
* @param null $storeId
*
* @return array
*/
private function getAvailableSites()
private function getAvailableSites($storeId = null)
{
$result = $this->doGetRequest();
$result = $this->doGetRequest($storeId);

return json_decode($result['body']);
}

/**
* @param null $storeId
*
* @return array
*/
private function doGetRequest()
private function doGetRequest($storeId = null)
{
return $this->call(static::TINIFY_API_CDN_SITES, 'get', true);
return $this->call(static::TINIFY_API_CDN_SITES, 'get', true, $storeId);
}
}
22 changes: 15 additions & 7 deletions Model/Config/Provider/CDN/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ class Configuration extends AbstractConfigProvider
/**
* Configuration constructor.
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param Challenge $challenge
* @param GeneralConfiguration $generalConfig
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\App\RequestInterface $request
* @param Challenge $challenge
* @param GeneralConfiguration $generalConfig
* @param Url $urlBuilder
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
*/
public function __construct(
\Magento\Framework\Model\Context $context,
Expand Down Expand Up @@ -142,14 +144,18 @@ public function getLiveCredentials()
}

/**
* @param null $storeId
*
* @return string
*/
public function getAccessToken($storeId = null)
{
return $this->getConfigValue(static::XPATH_TINIFYCDN_CDN_ACCESS_TOKEN. $storeId);
return $this->getConfigValue(static::XPATH_TINIFYCDN_CDN_ACCESS_TOKEN, $storeId);
}

/**
* @param null $storeId
*
* @return string
*/
public function getCdnEndpoint($storeId = null)
Expand All @@ -158,6 +164,8 @@ public function getCdnEndpoint($storeId = null)
}

/**
* @param null $storeId
*
* @return string
*/
public function getSiteId($storeId = null)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"homepage": "http://tig.nl"
}
],
"version": "v1.3.1",
"version": "v1.3.2",
"autoload": {
"files": [
"registration.php"
Expand Down

0 comments on commit 09066a6

Please sign in to comment.