Skip to content

Commit

Permalink
Extension activation refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Jan 24, 2025
1 parent 6eb703a commit f76f49c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 49 deletions.
27 changes: 14 additions & 13 deletions Block/Adminhtml/System/Config/Form/ProductKeyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
return parent::render($element);
} else {
$config = ObjectManager::getInstance()->get(\Magefan\Community\Model\Config::class);
if (!$config->getConfig($section->getName() . '/' . 'g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' . 'm' . 'f' . 'a' . 'c' . 't' . 'i' . 'v' . 'e')
&& $config->getConfig($section->getName() . '/g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' . 'm' . 'f' . 't' . 'y' . 'p' . 'e')) {
$configStructure = $objectManager->get(\Magento\Config\Model\Config\Structure::class);
$moduleSection = $configStructure->getElement($section->getName());
if ($moduleSection && $moduleSection->getAttribute('resource')) {
$moduleName = explode(':', $moduleSection->getAttribute('resource'));
$moduleName = $moduleName[0];
$moduleInfo = $objectManager->get(\Magefan\Community\Api\GetModuleInfoInterface::class)->execute($moduleName);

$url = 'ht' . 'tp'. ':' . '/'. '/'. 'ma' . 'g' . 'ef' . 'an' . '.' . 'c' . 'o' . 'm' . '/' . 'mp' . 'k/a' . 'cti' . 'vat' . 'e/e' . 'xte' . 'nsi' . 'on/' . 'ret' . 'urn_' . 'ur' . 'l/' .
$bp = $section->getName() . '/' . 'g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' ;
if (!$config->getConfig( $bp . Section::ACTIVE) && !$section->getType()) {
$url = 'ht' . 'tps'. ':' . '/'. '/'. 'ma' . 'g' . 'ef' . 'an' . '.' . 'c' . 'o' . 'm' . '/' . 'mp' . 'k/a' . 'cti' . 'vat' . 'e/e' . 'xte' . 'nsi' . 'on/' . 'ret' . 'urn_' . 'ur' . 'l/' .
base64_encode($this->getUrl('m' . 'f' . 'co' . 'mm' . 'uni' . 'ty/' . 'act' . 'iva' . 'te/ext' . 'ens' . 'ion', ['section' => $section->getName()]))
. '/mo' . 'dul' . 'e/' . $moduleInfo->getProductName() . '/se' . 'cti' . 'on/' . $section->getName();
return '<button id="activate-extension" type="button" class="action-default scalable primary ui-button ui-corner-all ui-widget" onclick="window.location.href=\'' . $url . '\';">Activate extension</button>';
}
. '/mo' . 'dul' . 'e/' . $section->getModuleName() . '/se' . 'cti' . 'on/' . $section->getName();
return '
<tr id="row_mfblog_general_' . Section::ACTIVE . '">
<td class="label"></td>
<td class="value">
<button id="activate-extension" type="button" class="action-default scalable primary ui-button ui-corner-all ui-widget" onclick="window.open(\'' . $url . '\');">
' . __('Activate Extension') . '
</button>
</td>
<td class=""></td>
</tr>' ;
}
}
}
Expand Down
55 changes: 36 additions & 19 deletions Controller/Adminhtml/Activate/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Cache\Type\Config;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magefan\Community\Model\Section;

class Extension extends \Magento\Backend\App\Action
{
Expand All @@ -27,19 +29,27 @@ class Extension extends \Magento\Backend\App\Action
*/
private $cacheTypeList;

/**
* @var DateTime
*/
private $date;

/**
* @param Context $context
* @param WriterInterface $configWriter
* @param TypeListInterface $cacheTypeList
* @param DateTime $date
*/
public function __construct(
Context $context,
WriterInterface $configWriter,
TypeListInterface $cacheTypeList
TypeListInterface $cacheTypeList,
DateTime $date
)
{
$this->configWriter = $configWriter;
$this->cacheTypeList = $cacheTypeList;
$this->date = $date;
parent::__construct($context);
}

Expand All @@ -49,27 +59,34 @@ public function __construct(
*/
public function execute()
{
if (!$this->getRequest()->getParam('activation_key')) {
throw new NoSuchEntityException(__('Activation key not found.'));
}
try {
$activationKey = (string)$this->getRequest()->getParam('activation_key');
if (!$this->getRequest()->getParam('activation_key')) {
throw new LocalizedException(__('Activation Key is missing. Please contact Magefan support.'));
}

if (!$this->getRequest()->getParam('section')) {
throw new NoSuchEntityException(__('Section not specified.'));
}

$section = (string)$this->getRequest()->getParam('section');
$urlInfo = parse_url($this->_url->getCurrentUrl());
$domain = isset($urlInfo['host']) ? $urlInfo['host'] : null;
$section = (string)$this->getRequest()->getParam('section');
if (!$section) {
throw new LocalizedException(__('Section param is missing. Please contact Magefan support.'));
}

$key = sha1(date('y-m-d'). '_' . $section . '_' . $domain);
if ($this->getRequest()->getParam('activation_key') !== $key) {
throw new NoSuchEntityException(__('Invalid activation key provided. Please try again.'));
}
$urlInfo = parse_url($this->_url->getCurrentUrl());
$domain = isset($urlInfo['host']) ? $urlInfo['host'] : '';

$this->configWriter->save($section . '/g'.'e'.'n'.'e'.'r'.'a'.'l'.'/'.'m'.'f'.'a'.'c'.'t'.'i'.'v'.'e', 1, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0);
$this->cacheTypeList->cleanType(Config::TYPE_IDENTIFIER);
$date = $this->date->gmtDate();
$key = sha1(date('y-m-d', strtotime($date)) . '_' . $section . '_' . $domain);
if ($activationKey !== $key) {
throw new LocalizedException(__('Invalid Activation Key. Please contact Magefan support.'));
}

return $this->resultRedirectFactory->create()->setUrl($this->_url->getUrl('adminhtml/system_config/edit', ['section' => $section]));
$this->configWriter->save($section . '/g'.'e'.'n'.'e'.'r'.'a'.'l'.'/'.Section::ACTIVE, 1, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0);
$this->cacheTypeList->cleanType(Config::TYPE_IDENTIFIER);

$this->messageManager->addSuccess(__('Thank you. Extension has been activated.'));
return $this->resultRedirectFactory->create()->setUrl($this->_url->getUrl('adminhtml/system_config/edit', ['section' => $section]));
} catch (LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
return $this->resultRedirectFactory->create()->setUrl($this->_url->getUrl('adminhtml'));
}
}
}
34 changes: 27 additions & 7 deletions Model/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ final class Section

const TYPE = 'mftype';

const ACTIVE = 'mfactive';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
Expand Down Expand Up @@ -85,14 +87,14 @@ final public function __construct(
*/
final public function isEnabled()
{
return (bool) $this->getConfig(self::ENABLED);
return (bool)$this->getConfig(self::ENABLED);
}

/**
* @param false $e
* @return false|string
*/
final public function getModule($e = false)
final public function getModuleName($e = false)
{
$fs = $e ? [self::MODULE] : [self::MODULE . 'e', self::MODULE . 'p', self::MODULE];
foreach ($fs as $f) {
Expand All @@ -101,18 +103,26 @@ final public function getModule($e = false)
break;
}
}

return $module;
}

/**
* @param false $e
* @return false|string
*/
final public function getModule($e = false)
{
$module = $this->getModuleName();

$url = $this->scopeConfig->getValue(
'web/unsecure/base' . '_' . 'url',
ScopeInterface::SCOPE_STORE,
0
);

if (\Magefan\Community\Model\UrlChecker::showUrl($url)) {
if ($module
&& (!$this->getConfig(self::TYPE)
|| $this->getConfig(self::TYPE) && $this->metadata->getEdition() != 'C' . 'omm' . 'un' . 'ity'
)
) {
if ($module && $this->getType()) {
return $module;
}

Expand All @@ -126,6 +136,16 @@ final public function getModule($e = false)
return false;
}

/**
* @return bool
*/
final public function getType()
{
return (!$this->getConfig(self::TYPE)
|| $this->getConfig(self::TYPE) && $this->metadata->getEdition() != 'C' . 'omm' . 'un' . 'ity'
);
}

/**
* @return string
*/
Expand Down
18 changes: 9 additions & 9 deletions Observer/ConfigObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Magefan\Community\Model\Section\Info;
use Magento\Framework\Message\ManagerInterface;
use Magefan\Community\Model\SetLinvFlag;
use Magefan\BlogExtra\Model\Config;
use Magefan\Community\Model\Config;
use Magefan\Community\Model\Section;

/**
* Community observer
Expand Down Expand Up @@ -85,18 +86,16 @@ final public function execute(\Magento\Framework\Event\Observer $observer)
'key' => $key
]);

$module = $section->getName();
if (!$section->getModule()) {
if (!$this->config->getConfig($section->getName() . '/'.'g'.'e'.'n'.'e'.'r'.'a'.'l'.'/'.'m'.'f'.'a'.'c'.'t'.'i'.'v'.'e')
&& $this->config->getConfig($section->getName() . '/g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' . 'm' . 'f' . 't' . 'y' . 'p' . 'e')
) {
$bp = $section->getName() . '/' . 'g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' ;
if (!$this->config->getConfig( $bp . Section::ACTIVE) && !$section->getType()) {
$this->messageManager->addError(
implode(array_reverse(
[
'.','s','e','r','u','t','a','e','f',' ','s','t','i',
' ','g','n','i','s','u',' ','e','u','n','i','t','n','o','c',' ','o','t',' ','t','i',
' ','e','t','a','v','i','t','c','a',' ','e','s','a','e','l','P',' ','.','e','v','i','t','c','a','n','i',
' ','y','l','t','n','e','r','r','u','c',' ','s','i',' ','e','l','u','d','o','m',' ','e','h','T'
'.','e','g','a','s','u',' ','e','e','r','f',' ','y','o','j','n','e',' ',
'o','t',' ','t','i',' ','e','t','a','v','i','t','c','a',' ','e','s','a',
'e','l','P',' ','.','d','e','t','a','v','i','t','c','a',' ','t','o','n',
' ','s','i',' ','n','o','i','s','n','e','t','x','e',' ','e','h','T'
]
))
);
Expand All @@ -105,6 +104,7 @@ final public function execute(\Magento\Framework\Event\Observer $observer)
}
return;
}
$module = $section->getName();
$data = $this->info->load([$section]);

if (!$section->validate($data)) {
Expand Down
8 changes: 7 additions & 1 deletion etc/adminhtml/routes.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?xml version="1.0" ?>
<?xml version="1.0"?>
<!--
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route frontName="mfcommunity" id="mfcommunity">
Expand Down

0 comments on commit f76f49c

Please sign in to comment.