Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Change.php
Go to the documentation of this file.
1 <?php
7 
12 
23 {
29  protected $encryptor;
30 
36  protected $directory;
37 
43  protected $structure;
44 
50  protected $writer;
51 
58  protected $random;
59 
69  public function __construct(
70  \Magento\Framework\Model\ResourceModel\Db\Context $context,
71  \Magento\Framework\Filesystem $filesystem,
72  \Magento\Config\Model\Config\Structure $structure,
73  \Magento\Framework\Encryption\EncryptorInterface $encryptor,
74  \Magento\Framework\App\DeploymentConfig\Writer $writer,
75  \Magento\Framework\Math\Random $random,
76  $connectionName = null
77  ) {
78  $this->encryptor = clone $encryptor;
79  parent::__construct($context, $connectionName);
80  $this->directory = $filesystem->getDirectoryWrite(DirectoryList::CONFIG);
81  $this->structure = $structure;
82  $this->writer = $writer;
83  $this->random = $random;
84  }
85 
91  protected function _construct()
92  {
93  $this->_init('core_config_data', 'config_id');
94  }
95 
103  public function changeEncryptionKey($key = null)
104  {
105  // prepare new key, encryptor and new configuration segment
106  if (!$this->writer->checkIfWritable()) {
107  throw new \Exception(__('Deployment configuration file is not writable.'));
108  }
109 
110  if (null === $key) {
111  $key = md5($this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE));
112  }
113  $this->encryptor->setNewKey($key);
114 
115  $encryptSegment = new ConfigData(ConfigFilePool::APP_ENV);
116  $encryptSegment->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, $this->encryptor->exportKeys());
117 
118  $configData = [$encryptSegment->getFileKey() => $encryptSegment->getData()];
119 
120  // update database and config.php
121  $this->beginTransaction();
122  try {
123  $this->_reEncryptSystemConfigurationValues();
125  $this->writer->saveConfig($configData);
126  $this->commit();
127  return $key;
128  } catch (\Exception $e) {
129  $this->rollBack();
130  throw $e;
131  }
132  }
133 
139  protected function _reEncryptSystemConfigurationValues()
140  {
141  // look for encrypted node entries in all system.xml files
143  $configStructure = $this->structure;
144  $paths = $configStructure->getFieldPathsByAttribute(
145  'backend_model',
146  \Magento\Config\Model\Config\Backend\Encrypted::class
147  );
148 
149  // walk through found data and re-encrypt it
150  if ($paths) {
151  $table = $this->getTable('core_config_data');
152  $values = $this->getConnection()->fetchPairs(
153  $this->getConnection()
154  ->select()
155  ->from($table, ['config_id', 'value'])
156  ->where('path IN (?)', $paths)
157  ->where('value NOT LIKE ?', '')
158  );
159  foreach ($values as $configId => $value) {
160  $this->getConnection()->update(
161  $table,
162  ['value' => $this->encryptor->encrypt($this->encryptor->decrypt($value))],
163  ['config_id = ?' => (int)$configId]
164  );
165  }
166  }
167  }
168 
174  protected function _reEncryptCreditCardNumbers()
175  {
176  $table = $this->getTable('sales_order_payment');
177  $select = $this->getConnection()->select()->from($table, ['entity_id', 'cc_number_enc']);
178 
179  $attributeValues = $this->getConnection()->fetchPairs($select);
180  // save new values
181  foreach ($attributeValues as $valueId => $value) {
182  $this->getConnection()->update(
183  $table,
184  ['cc_number_enc' => $this->encryptor->encrypt($this->encryptor->decrypt($value))],
185  ['entity_id = ?' => (int)$valueId]
186  );
187  }
188  }
189 }
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Filesystem $filesystem, \Magento\Config\Model\Config\Structure $structure, \Magento\Framework\Encryption\EncryptorInterface $encryptor, \Magento\Framework\App\DeploymentConfig\Writer $writer, \Magento\Framework\Math\Random $random, $connectionName=null)
Definition: Change.php:69
$paths
Definition: _bootstrap.php:83
$table
Definition: trigger.php:14
$filesystem