Encrypted.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Encrypted config field backend model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Config\Model\Config\Backend;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Encrypted extends \Magento\Framework\App\Config\Value implements
  14. \Magento\Framework\App\Config\Data\ProcessorInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\Encryption\EncryptorInterface
  18. */
  19. protected $_encryptor;
  20. /**
  21. * @param \Magento\Framework\Model\Context $context
  22. * @param \Magento\Framework\Registry $registry
  23. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  24. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  25. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  26. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  27. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  28. * @param array $data
  29. */
  30. public function __construct(
  31. \Magento\Framework\Model\Context $context,
  32. \Magento\Framework\Registry $registry,
  33. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  34. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  35. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  36. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  37. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  38. array $data = []
  39. ) {
  40. $this->_encryptor = $encryptor;
  41. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  42. }
  43. /**
  44. * Magic method called during class serialization
  45. *
  46. * @return string[]
  47. */
  48. public function __sleep()
  49. {
  50. $properties = parent::__sleep();
  51. return array_diff($properties, ['_encryptor']);
  52. }
  53. /**
  54. * Magic method called during class un-serialization
  55. *
  56. * @return void
  57. */
  58. public function __wakeup()
  59. {
  60. parent::__wakeup();
  61. $this->_encryptor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  62. \Magento\Framework\Encryption\EncryptorInterface::class
  63. );
  64. }
  65. /**
  66. * Decrypt value after loading
  67. *
  68. * @return void
  69. */
  70. protected function _afterLoad()
  71. {
  72. $value = (string)$this->getValue();
  73. if (!empty($value) && ($decrypted = $this->_encryptor->decrypt($value))) {
  74. $this->setValue($decrypted);
  75. }
  76. }
  77. /**
  78. * Encrypt value before saving
  79. *
  80. * @return void
  81. */
  82. public function beforeSave()
  83. {
  84. $this->_dataSaveAllowed = false;
  85. $value = (string)$this->getValue();
  86. // don't save value, if an obscured value was received. This indicates that data was not changed.
  87. if (!preg_match('/^\*+$/', $value) && !empty($value)) {
  88. $this->_dataSaveAllowed = true;
  89. $encrypted = $this->_encryptor->encrypt($value);
  90. $this->setValue($encrypted);
  91. } elseif (empty($value)) {
  92. $this->_dataSaveAllowed = true;
  93. }
  94. }
  95. /**
  96. * Process config value
  97. *
  98. * @param string $value
  99. * @return string
  100. */
  101. public function processValue($value)
  102. {
  103. return $this->_encryptor->decrypt($value);
  104. }
  105. }