Skip to content

Commit a7af0ff

Browse files
author
Rafael Grigorian
committed
Fixed #10
1 parent f9f95ab commit a7af0ff

File tree

1 file changed

+89
-24
lines changed
  • src/app/code/JetRails/Cloudflare/Console/Command

1 file changed

+89
-24
lines changed

src/app/code/JetRails/Cloudflare/Console/Command/SetAuth.php

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace JetRails\Cloudflare\Console\Command;
44

5-
use JetRails\Cloudflare\Helper\Adminhtml\Data;
5+
use stdClass;
6+
use Magento\Framework\App\Cache\Type\Config as ConfigType;
67
use Magento\Framework\App\Cache\TypeListInterface;
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\App\Config\Storage\WriterInterface;
10+
use Magento\Framework\Encryption\EncryptorInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
713
use Symfony\Component\Console\Command\Command;
814
use Symfony\Component\Console\Input\InputInterface;
915
use Symfony\Component\Console\Input\InputOption;
@@ -12,33 +18,33 @@
1218
class SetAuth extends Command {
1319

1420
/**
15-
* These internal data members include instances of helper classes that are injected into
16-
* the class using dependency injection on runtime. Also a boolean variable is included
17-
* that defines if the action should be run if the feature is disabled in the store config.
18-
* @var TypeListInterface _cacheTypeList Instance of the TypeListInterface class
19-
* @var Data _data Instance of the Data class
20-
* @var Logger _logger Instance of the Logger class
21-
* @var Purger _purger Instance of the Purger class
22-
* @var Boolean _runIfDisabled Execute method if feature isn't on?
21+
* @var string XPATH_AUTH_ZONE Path to auth zone setting
22+
* @var string XPATH_AUTH_TOKEN Path to auth token setting
2323
*/
24+
const XPATH_AUTH_ZONE = "cloudflare/configuration/auth_zone";
25+
const XPATH_AUTH_TOKEN = "cloudflare/configuration/auth_token";
26+
2427
protected $_cacheTypeList;
25-
protected $_data;
28+
protected $_configReader;
29+
protected $_configWriter;
30+
protected $_encryptor;
31+
protected $_storeManager;
2632

27-
/**
28-
* This constructor is overloaded from the parent class in order to use dependency injection
29-
* to get the dependency classes that we need for this module's command actions to execute.
30-
* @param Data data Instance of the Data class
31-
* @param TypeListInterface cacheTypeList Instance of the TypeListInterface class
32-
*/
3333
public function __construct (
34-
Data $data,
35-
TypeListInterface $cacheTypeList
34+
StoreManagerInterface $storeManager,
35+
TypeListInterface $cacheTypeList,
36+
EncryptorInterface $encryptor,
37+
ScopeConfigInterface $configReader,
38+
WriterInterface $configWriter
3639
) {
3740
// Call the parent constructor
3841
parent::__construct ();
3942
// Save injected classes internally
43+
$this->_storeManager = $storeManager;
4044
$this->_cacheTypeList = $cacheTypeList;
41-
$this->_data = $data;
45+
$this->_configReader = $configReader;
46+
$this->_configWriter = $configWriter;
47+
$this->_encryptor = $encryptor;
4248
}
4349

4450
/**
@@ -74,6 +80,67 @@ protected function configure () {
7480
parent::configure ();
7581
}
7682

83+
/**
84+
* This method looks though all the stores that are setup in Magento. It
85+
* then extracts the domain name from the store's base URL.
86+
* @return array All domains for all stores
87+
*/
88+
private function _getDomainNames () {
89+
$domains = array ();
90+
$stores = $this->_storeManager->getStores ();
91+
foreach ( $stores as $store ) {
92+
$domain = parse_url ( $store->getBaseUrl () ) ["host"];
93+
array_push ( $domains, $domain );
94+
}
95+
$domains = array_unique ( $domains );
96+
sort ( $domains );
97+
return $domains;
98+
}
99+
100+
/**
101+
* This method takes in a new zone, saves that zone internally, and
102+
* that zone is then used for CF authentication.
103+
* @param string zone Set CF auth zone to this
104+
* @param string domain Domain name
105+
*/
106+
private function _setAuthZone ( $zone, $domain ) {
107+
$old = $this->_configReader->getValue (
108+
self::XPATH_AUTH_ZONE,
109+
ScopeInterface::SCOPE_STORE
110+
);
111+
$old = $this->_encryptor->decrypt ( $old );
112+
$old = json_decode ( $old );
113+
if ( !( $old instanceof stdClass ) ) $old = new stdClass ();
114+
$zone = trim ( strval ( $zone ) );
115+
$old->$domain = $zone;
116+
$old = json_encode ( $old );
117+
$old = $this->_encryptor->encrypt ( $old );
118+
$this->_configWriter->save ( self::XPATH_AUTH_ZONE, $old );
119+
$this->_cacheTypeList->cleanType ( ConfigType::TYPE_IDENTIFIER );
120+
}
121+
122+
/**
123+
* This method takes in a new zone, saves that token internally, and
124+
* that token is then used for CF authentication.
125+
* @param string token Set CF auth token to this
126+
* @param string domain Domain name
127+
*/
128+
private function _setAuthToken ( $token, $domain ) {
129+
$old = $this->_configReader->getValue (
130+
self::XPATH_AUTH_TOKEN,
131+
ScopeInterface::SCOPE_STORE
132+
);
133+
$old = $this->_encryptor->decrypt ( $old );
134+
$old = json_decode ( $old );
135+
if ( !( $old instanceof stdClass ) ) $old = new stdClass ();
136+
$token = trim ( strval ( $token ) );
137+
$old->$domain = $token;
138+
$old = json_encode ( $old );
139+
$old = $this->_encryptor->encrypt ( $old );
140+
$this->_configWriter->save ( self::XPATH_AUTH_TOKEN, $old );
141+
$this->_cacheTypeList->cleanType ( ConfigType::TYPE_IDENTIFIER );
142+
}
143+
77144
/**
78145
* This method is here because it interfaces with the abstract parent class. It takes in an
79146
* input and output interface and it runs the command.
@@ -97,9 +164,7 @@ public function execute ( InputInterface $input, OutputInterface $output ) {
97164
$output->writeln ("Error: please pass token name with --token option.");
98165
return $this;
99166
}
100-
$domains = array_map ( function ( $entry ) {
101-
return $entry ["name"];
102-
}, $this->_data->getDomainNames () );
167+
$domains = $this->_getDomainNames ();
103168
if ( !in_array ( $domain, $domains ) ) {
104169
$output->writeln ("Error: invalid domain name, available options are:");
105170
$output->writeln ("");
@@ -109,8 +174,8 @@ public function execute ( InputInterface $input, OutputInterface $output ) {
109174
$output->writeln ("");
110175
return $this;
111176
}
112-
$this->_data->setAuthZone ( $zone, $domain );
113-
$this->_data->setAuthToken ( $token, $domain );
177+
$this->_setAuthZone ( $zone, $domain );
178+
$this->_setAuthToken ( $token, $domain );
114179
$output->writeln ("Successfully saved zone and token for domain $domain.");
115180
}
116181

0 commit comments

Comments
 (0)