Skip to content

Commit f76f49c

Browse files
committed
Extension activation refactor
1 parent 6eb703a commit f76f49c

File tree

5 files changed

+93
-49
lines changed

5 files changed

+93
-49
lines changed

Block/Adminhtml/System/Config/Form/ProductKeyField.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
3939
return parent::render($element);
4040
} else {
4141
$config = ObjectManager::getInstance()->get(\Magefan\Community\Model\Config::class);
42-
if (!$config->getConfig($section->getName() . '/' . 'g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' . 'm' . 'f' . 'a' . 'c' . 't' . 'i' . 'v' . 'e')
43-
&& $config->getConfig($section->getName() . '/g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' . 'm' . 'f' . 't' . 'y' . 'p' . 'e')) {
44-
$configStructure = $objectManager->get(\Magento\Config\Model\Config\Structure::class);
45-
$moduleSection = $configStructure->getElement($section->getName());
46-
if ($moduleSection && $moduleSection->getAttribute('resource')) {
47-
$moduleName = explode(':', $moduleSection->getAttribute('resource'));
48-
$moduleName = $moduleName[0];
49-
$moduleInfo = $objectManager->get(\Magefan\Community\Api\GetModuleInfoInterface::class)->execute($moduleName);
50-
51-
$url = 'ht' . 'tp'. ':' . '/'. '/'. 'ma' . 'g' . 'ef' . 'an' . '.' . 'c' . 'o' . 'm' . '/' . 'mp' . 'k/a' . 'cti' . 'vat' . 'e/e' . 'xte' . 'nsi' . 'on/' . 'ret' . 'urn_' . 'ur' . 'l/' .
42+
$bp = $section->getName() . '/' . 'g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' ;
43+
if (!$config->getConfig( $bp . Section::ACTIVE) && !$section->getType()) {
44+
$url = 'ht' . 'tps'. ':' . '/'. '/'. 'ma' . 'g' . 'ef' . 'an' . '.' . 'c' . 'o' . 'm' . '/' . 'mp' . 'k/a' . 'cti' . 'vat' . 'e/e' . 'xte' . 'nsi' . 'on/' . 'ret' . 'urn_' . 'ur' . 'l/' .
5245
base64_encode($this->getUrl('m' . 'f' . 'co' . 'mm' . 'uni' . 'ty/' . 'act' . 'iva' . 'te/ext' . 'ens' . 'ion', ['section' => $section->getName()]))
53-
. '/mo' . 'dul' . 'e/' . $moduleInfo->getProductName() . '/se' . 'cti' . 'on/' . $section->getName();
54-
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>';
55-
}
46+
. '/mo' . 'dul' . 'e/' . $section->getModuleName() . '/se' . 'cti' . 'on/' . $section->getName();
47+
return '
48+
<tr id="row_mfblog_general_' . Section::ACTIVE . '">
49+
<td class="label"></td>
50+
<td class="value">
51+
<button id="activate-extension" type="button" class="action-default scalable primary ui-button ui-corner-all ui-widget" onclick="window.open(\'' . $url . '\');">
52+
' . __('Activate Extension') . '
53+
</button>
54+
</td>
55+
<td class=""></td>
56+
</tr>' ;
5657
}
5758
}
5859
}

Controller/Adminhtml/Activate/Extension.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use Magento\Backend\App\Action\Context;
1212
use Magento\Framework\App\Config\Storage\WriterInterface;
1313
use Magento\Framework\App\Cache\TypeListInterface;
14-
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Exception\LocalizedException;
1515
use Magento\Framework\App\Config\ScopeConfigInterface;
1616
use Magento\Framework\App\Cache\Type\Config;
17+
use Magento\Framework\Stdlib\DateTime\DateTime;
18+
use Magefan\Community\Model\Section;
1719

1820
class Extension extends \Magento\Backend\App\Action
1921
{
@@ -27,19 +29,27 @@ class Extension extends \Magento\Backend\App\Action
2729
*/
2830
private $cacheTypeList;
2931

32+
/**
33+
* @var DateTime
34+
*/
35+
private $date;
36+
3037
/**
3138
* @param Context $context
3239
* @param WriterInterface $configWriter
3340
* @param TypeListInterface $cacheTypeList
41+
* @param DateTime $date
3442
*/
3543
public function __construct(
3644
Context $context,
3745
WriterInterface $configWriter,
38-
TypeListInterface $cacheTypeList
46+
TypeListInterface $cacheTypeList,
47+
DateTime $date
3948
)
4049
{
4150
$this->configWriter = $configWriter;
4251
$this->cacheTypeList = $cacheTypeList;
52+
$this->date = $date;
4353
parent::__construct($context);
4454
}
4555

@@ -49,27 +59,34 @@ public function __construct(
4959
*/
5060
public function execute()
5161
{
52-
if (!$this->getRequest()->getParam('activation_key')) {
53-
throw new NoSuchEntityException(__('Activation key not found.'));
54-
}
62+
try {
63+
$activationKey = (string)$this->getRequest()->getParam('activation_key');
64+
if (!$this->getRequest()->getParam('activation_key')) {
65+
throw new LocalizedException(__('Activation Key is missing. Please contact Magefan support.'));
66+
}
5567

56-
if (!$this->getRequest()->getParam('section')) {
57-
throw new NoSuchEntityException(__('Section not specified.'));
58-
}
59-
60-
$section = (string)$this->getRequest()->getParam('section');
61-
$urlInfo = parse_url($this->_url->getCurrentUrl());
62-
$domain = isset($urlInfo['host']) ? $urlInfo['host'] : null;
68+
$section = (string)$this->getRequest()->getParam('section');
69+
if (!$section) {
70+
throw new LocalizedException(__('Section param is missing. Please contact Magefan support.'));
71+
}
6372

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

69-
$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);
70-
$this->cacheTypeList->cleanType(Config::TYPE_IDENTIFIER);
76+
$date = $this->date->gmtDate();
77+
$key = sha1(date('y-m-d', strtotime($date)) . '_' . $section . '_' . $domain);
78+
if ($activationKey !== $key) {
79+
throw new LocalizedException(__('Invalid Activation Key. Please contact Magefan support.'));
80+
}
7181

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

85+
$this->messageManager->addSuccess(__('Thank you. Extension has been activated.'));
86+
return $this->resultRedirectFactory->create()->setUrl($this->_url->getUrl('adminhtml/system_config/edit', ['section' => $section]));
87+
} catch (LocalizedException $e) {
88+
$this->messageManager->addError($e->getMessage());
89+
return $this->resultRedirectFactory->create()->setUrl($this->_url->getUrl('adminhtml'));
90+
}
7491
}
7592
}

Model/Section.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ final class Section
2424

2525
const TYPE = 'mftype';
2626

27+
const ACTIVE = 'mfactive';
28+
2729
/**
2830
* @var \Magento\Framework\App\Config\ScopeConfigInterface
2931
*/
@@ -85,14 +87,14 @@ final public function __construct(
8587
*/
8688
final public function isEnabled()
8789
{
88-
return (bool) $this->getConfig(self::ENABLED);
90+
return (bool)$this->getConfig(self::ENABLED);
8991
}
9092

9193
/**
9294
* @param false $e
9395
* @return false|string
9496
*/
95-
final public function getModule($e = false)
97+
final public function getModuleName($e = false)
9698
{
9799
$fs = $e ? [self::MODULE] : [self::MODULE . 'e', self::MODULE . 'p', self::MODULE];
98100
foreach ($fs as $f) {
@@ -101,18 +103,26 @@ final public function getModule($e = false)
101103
break;
102104
}
103105
}
106+
107+
return $module;
108+
}
109+
110+
/**
111+
* @param false $e
112+
* @return false|string
113+
*/
114+
final public function getModule($e = false)
115+
{
116+
$module = $this->getModuleName();
117+
104118
$url = $this->scopeConfig->getValue(
105119
'web/unsecure/base' . '_' . 'url',
106120
ScopeInterface::SCOPE_STORE,
107121
0
108122
);
109123

110124
if (\Magefan\Community\Model\UrlChecker::showUrl($url)) {
111-
if ($module
112-
&& (!$this->getConfig(self::TYPE)
113-
|| $this->getConfig(self::TYPE) && $this->metadata->getEdition() != 'C' . 'omm' . 'un' . 'ity'
114-
)
115-
) {
125+
if ($module && $this->getType()) {
116126
return $module;
117127
}
118128

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

139+
/**
140+
* @return bool
141+
*/
142+
final public function getType()
143+
{
144+
return (!$this->getConfig(self::TYPE)
145+
|| $this->getConfig(self::TYPE) && $this->metadata->getEdition() != 'C' . 'omm' . 'un' . 'ity'
146+
);
147+
}
148+
129149
/**
130150
* @return string
131151
*/

Observer/ConfigObserver.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use Magefan\Community\Model\Section\Info;
1212
use Magento\Framework\Message\ManagerInterface;
1313
use Magefan\Community\Model\SetLinvFlag;
14-
use Magefan\BlogExtra\Model\Config;
14+
use Magefan\Community\Model\Config;
15+
use Magefan\Community\Model\Section;
1516

1617
/**
1718
* Community observer
@@ -85,18 +86,16 @@ final public function execute(\Magento\Framework\Event\Observer $observer)
8586
'key' => $key
8687
]);
8788

88-
$module = $section->getName();
8989
if (!$section->getModule()) {
90-
if (!$this->config->getConfig($section->getName() . '/'.'g'.'e'.'n'.'e'.'r'.'a'.'l'.'/'.'m'.'f'.'a'.'c'.'t'.'i'.'v'.'e')
91-
&& $this->config->getConfig($section->getName() . '/g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' . 'm' . 'f' . 't' . 'y' . 'p' . 'e')
92-
) {
90+
$bp = $section->getName() . '/' . 'g' . 'e' . 'n' . 'e' . 'r' . 'a' . 'l' . '/' ;
91+
if (!$this->config->getConfig( $bp . Section::ACTIVE) && !$section->getType()) {
9392
$this->messageManager->addError(
9493
implode(array_reverse(
9594
[
96-
'.','s','e','r','u','t','a','e','f',' ','s','t','i',
97-
' ','g','n','i','s','u',' ','e','u','n','i','t','n','o','c',' ','o','t',' ','t','i',
98-
' ','e','t','a','v','i','t','c','a',' ','e','s','a','e','l','P',' ','.','e','v','i','t','c','a','n','i',
99-
' ','y','l','t','n','e','r','r','u','c',' ','s','i',' ','e','l','u','d','o','m',' ','e','h','T'
95+
'.','e','g','a','s','u',' ','e','e','r','f',' ','y','o','j','n','e',' ',
96+
'o','t',' ','t','i',' ','e','t','a','v','i','t','c','a',' ','e','s','a',
97+
'e','l','P',' ','.','d','e','t','a','v','i','t','c','a',' ','t','o','n',
98+
' ','s','i',' ','n','o','i','s','n','e','t','x','e',' ','e','h','T'
10099
]
101100
))
102101
);
@@ -105,6 +104,7 @@ final public function execute(\Magento\Framework\Event\Observer $observer)
105104
}
106105
return;
107106
}
107+
$module = $section->getName();
108108
$data = $this->info->load([$section]);
109109

110110
if (!$section->validate($data)) {

etc/adminhtml/routes.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
<?xml version="1.0" ?>
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magefan ([email protected]). All rights reserved.
5+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
6+
*/
7+
-->
28
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
39
<router id="admin">
410
<route frontName="mfcommunity" id="mfcommunity">

0 commit comments

Comments
 (0)