14
14
*/
15
15
class SodiumChachaPatch implements DataPatchInterface
16
16
{
17
+ /**
18
+ * @var \Magento\Framework\Config\ScopeInterface
19
+ */
20
+ private $ scope ;
21
+
17
22
/**
18
23
* @var \Magento\Framework\Setup\ModuleDataSetupInterface
19
24
*/
@@ -35,25 +40,29 @@ class SodiumChachaPatch implements DataPatchInterface
35
40
private $ state ;
36
41
37
42
/**
43
+ * SodiumChachaPatch constructor.
38
44
* @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
39
45
* @param \Magento\Config\Model\Config\Structure\Proxy $structure
40
46
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
41
47
* @param \Magento\Framework\App\State $state
48
+ * @param \Magento\Framework\Config\ScopeInterface $scope
42
49
*/
43
50
public function __construct (
44
51
\Magento \Framework \Setup \ModuleDataSetupInterface $ moduleDataSetup ,
45
52
\Magento \Config \Model \Config \Structure \Proxy $ structure ,
46
53
\Magento \Framework \Encryption \EncryptorInterface $ encryptor ,
47
- \Magento \Framework \App \State $ state
54
+ \Magento \Framework \App \State $ state ,
55
+ \Magento \Framework \Config \ScopeInterface $ scope
48
56
) {
49
57
$ this ->moduleDataSetup = $ moduleDataSetup ;
50
58
$ this ->structure = $ structure ;
51
59
$ this ->encryptor = $ encryptor ;
52
60
$ this ->state = $ state ;
61
+ $ this ->scope = $ scope ;
53
62
}
54
63
55
64
/**
56
- * { @inheritdoc}
65
+ * @inheritdoc
57
66
*/
58
67
public function apply ()
59
68
{
@@ -65,49 +74,75 @@ public function apply()
65
74
}
66
75
67
76
/**
68
- * { @inheritdoc}
77
+ * @inheritdoc
69
78
*/
70
79
public static function getDependencies ()
71
80
{
72
81
return [];
73
82
}
74
83
75
84
/**
76
- * { @inheritdoc}
85
+ * @inheritdoc
77
86
*/
78
87
public function getAliases ()
79
88
{
80
89
return [];
81
90
}
82
91
92
+ /**
93
+ * Re encrypt sensitive data in the system configuration
94
+ */
83
95
private function reEncryptSystemConfigurationValues ()
84
96
{
85
- $ structure = $ this ->structure ;
86
- $ paths = $ this ->state ->emulateAreaCode (
87
- \Magento \Framework \App \Area::AREA_ADMINHTML ,
88
- function () use ($ structure ) {
89
- return $ structure ->getFieldPathsByAttribute (
90
- 'backend_model ' ,
91
- \Magento \Config \Model \Config \Backend \Encrypted::class
92
- );
93
- }
97
+ $ table = $ this ->moduleDataSetup ->getTable ('core_config_data ' );
98
+ $ hasEncryptedData = $ this ->moduleDataSetup ->getConnection ()->fetchOne (
99
+ $ this ->moduleDataSetup ->getConnection ()
100
+ ->select ()
101
+ ->from ($ table , [new \Zend_Db_Expr ('count(value) ' )])
102
+ ->where ('value LIKE ? ' , '0:2% ' )
94
103
);
95
- // walk through found data and re-encrypt it
96
- if ($ paths ) {
97
- $ table = $ this ->moduleDataSetup ->getTable ('core_config_data ' );
98
- $ values = $ this ->moduleDataSetup ->getConnection ()->fetchPairs (
99
- $ this ->moduleDataSetup ->getConnection ()
100
- ->select ()
101
- ->from ($ table , ['config_id ' , 'value ' ])
102
- ->where ('path IN (?) ' , $ paths )
103
- ->where ('value NOT LIKE ? ' , '' )
104
+ if ($ hasEncryptedData !== '0 ' ) {
105
+ $ currentScope = $ this ->scope ->getCurrentScope ();
106
+ $ structure = $ this ->structure ;
107
+ $ paths = $ this ->state ->emulateAreaCode (
108
+ \Magento \Framework \App \Area::AREA_ADMINHTML ,
109
+ function () use ($ structure ) {
110
+ $ this ->scope ->setCurrentScope (\Magento \Framework \App \Area::AREA_ADMINHTML );
111
+ /** Returns list of structure paths to be re encrypted */
112
+ $ paths = $ structure ->getFieldPathsByAttribute (
113
+ 'backend_model ' ,
114
+ \Magento \Config \Model \Config \Backend \Encrypted::class
115
+ );
116
+ /** Returns list of mapping between configPath => [structurePaths] */
117
+ $ mappedPaths = $ structure ->getFieldPaths ();
118
+ foreach ($ mappedPaths as $ mappedPath => $ data ) {
119
+ foreach ($ data as $ structurePath ) {
120
+ if ($ structurePath !== $ mappedPath && $ key = array_search ($ structurePath , $ paths )) {
121
+ $ paths [$ key ] = $ mappedPath ;
122
+ }
123
+ }
124
+ }
125
+
126
+ return array_unique ($ paths );
127
+ }
104
128
);
105
- foreach ($ values as $ configId => $ value ) {
106
- $ this ->moduleDataSetup ->getConnection ()->update (
107
- $ table ,
108
- ['value ' => $ this ->encryptor ->encrypt ($ this ->encryptor ->decrypt ($ value ))],
109
- ['config_id = ? ' => (int )$ configId ]
129
+ $ this ->scope ->setCurrentScope ($ currentScope );
130
+ // walk through found data and re-encrypt it
131
+ if ($ paths ) {
132
+ $ values = $ this ->moduleDataSetup ->getConnection ()->fetchPairs (
133
+ $ this ->moduleDataSetup ->getConnection ()
134
+ ->select ()
135
+ ->from ($ table , ['config_id ' , 'value ' ])
136
+ ->where ('path IN (?) ' , $ paths )
137
+ ->where ('value NOT LIKE ? ' , '' )
110
138
);
139
+ foreach ($ values as $ configId => $ value ) {
140
+ $ this ->moduleDataSetup ->getConnection ()->update (
141
+ $ table ,
142
+ ['value ' => $ this ->encryptor ->encrypt ($ this ->encryptor ->decrypt ($ value ))],
143
+ ['config_id = ? ' => (int )$ configId ]
144
+ );
145
+ }
111
146
}
112
147
}
113
148
}
0 commit comments