5
5
*/
6
6
namespace Magento \ImportExport \Model \ResourceModel \Import ;
7
7
8
+ use Magento \Backend \Model \Auth \Session ;
9
+ use Magento \Framework \App \ObjectManager ;
10
+ use Magento \Framework \DB \Select ;
11
+
8
12
/**
9
13
* ImportExport import data resource model
10
14
*
11
15
* @api
12
16
* @since 100.0.2
17
+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) Necessary to get current logged in user without modifying methods
13
18
*/
14
19
class Data extends \Magento \Framework \Model \ResourceModel \Db \AbstractDb implements \IteratorAggregate
15
20
{
21
+ /**
22
+ * Offline import user ID
23
+ */
24
+ private const DEFAULT_USER_ID = 0 ;
16
25
/**
17
26
* @var \Iterator
18
27
*/
@@ -24,21 +33,28 @@ class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implemen
24
33
* @var \Magento\Framework\Json\Helper\Data
25
34
*/
26
35
protected $ jsonHelper ;
36
+ /**
37
+ * @var Session
38
+ */
39
+ private $ authSession ;
27
40
28
41
/**
29
42
* Class constructor
30
43
*
31
44
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
32
45
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
33
- * @param string $connectionName
46
+ * @param string|null $connectionName
47
+ * @param Session|null $authSession
34
48
*/
35
49
public function __construct (
36
50
\Magento \Framework \Model \ResourceModel \Db \Context $ context ,
37
51
\Magento \Framework \Json \Helper \Data $ jsonHelper ,
38
- $ connectionName = null
52
+ $ connectionName = null ,
53
+ ?Session $ authSession = null
39
54
) {
40
55
parent ::__construct ($ context , $ connectionName );
41
56
$ this ->jsonHelper = $ jsonHelper ;
57
+ $ this ->authSession = $ authSession ?? ObjectManager::getInstance ()->get (Session::class);
42
58
}
43
59
44
60
/**
@@ -60,6 +76,7 @@ public function getIterator()
60
76
{
61
77
$ connection = $ this ->getConnection ();
62
78
$ select = $ connection ->select ()->from ($ this ->getMainTable (), ['data ' ])->order ('id ASC ' );
79
+ $ select = $ this ->prepareSelect ($ select );
63
80
$ stmt = $ connection ->query ($ select );
64
81
65
82
$ stmt ->setFetchMode (\Zend_Db::FETCH_NUM );
@@ -81,7 +98,7 @@ public function getIterator()
81
98
*/
82
99
public function cleanBunches ()
83
100
{
84
- return $ this ->getConnection ()->delete ($ this ->getMainTable ());
101
+ return $ this ->getConnection ()->delete ($ this ->getMainTable (), $ this -> prepareDelete ([]) );
85
102
}
86
103
87
104
/**
@@ -114,7 +131,9 @@ public function getEntityTypeCode()
114
131
public function getUniqueColumnData ($ code )
115
132
{
116
133
$ connection = $ this ->getConnection ();
117
- $ values = array_unique ($ connection ->fetchCol ($ connection ->select ()->from ($ this ->getMainTable (), [$ code ])));
134
+ $ select = $ connection ->select ()->from ($ this ->getMainTable (), [$ code ]);
135
+ $ select = $ this ->prepareSelect ($ select );
136
+ $ values = array_unique ($ connection ->fetchCol ($ select ));
118
137
119
138
if (count ($ values ) != 1 ) {
120
139
throw new \Magento \Framework \Exception \LocalizedException (
@@ -169,7 +188,67 @@ public function saveBunch($entity, $behavior, array $data)
169
188
170
189
return $ this ->getConnection ()->insert (
171
190
$ this ->getMainTable (),
172
- ['behavior ' => $ behavior , 'entity ' => $ entity , 'data ' => $ encodedData ]
191
+ $ this -> prepareInsert ( ['behavior ' => $ behavior , 'entity ' => $ entity , 'data ' => $ encodedData ])
173
192
);
174
193
}
194
+
195
+ /**
196
+ * Prepare select for query
197
+ *
198
+ * @param Select $select
199
+ * @return Select
200
+ */
201
+ private function prepareSelect (Select $ select ): Select
202
+ {
203
+ // check if the table has not been overridden for backward compatibility
204
+ if ($ this ->getMainTable () === $ this ->getTable ('importexport_importdata ' )) {
205
+ // user_id is NULL part is for backward compatibility
206
+ $ select ->where ('user_id=? OR user_id is NULL ' , $ this ->getUserId ());
207
+ }
208
+
209
+ return $ select ;
210
+ }
211
+
212
+ /**
213
+ * Prepare data for insert
214
+ *
215
+ * @param array $data
216
+ * @return array
217
+ */
218
+ private function prepareInsert (array $ data ): array
219
+ {
220
+ // check if the table has not been overridden for backward compatibility
221
+ if ($ this ->getMainTable () === $ this ->getTable ('importexport_importdata ' )) {
222
+ $ data ['user_id ' ] = $ this ->getUserId ();
223
+ }
224
+
225
+ return $ data ;
226
+ }
227
+
228
+ /**
229
+ * Prepare delete constraints
230
+ *
231
+ * @param array $where
232
+ * @return array
233
+ */
234
+ private function prepareDelete (array $ where ): array
235
+ {
236
+ // check if the table has not been overridden for backward compatibility
237
+ if ($ this ->getMainTable () === $ this ->getTable ('importexport_importdata ' )) {
238
+ // user_id is NULL part is for backward compatibility
239
+ $ where ['user_id=? OR user_id is NULL ' ] = $ this ->getUserId ();
240
+ }
241
+
242
+ return $ where ;
243
+ }
244
+
245
+ /**
246
+ * Get current user ID
247
+ *
248
+ * @return int
249
+ */
250
+ private function getUserId (): int
251
+ {
252
+ return $ this ->authSession ->isLoggedIn () ? $ this ->authSession ->getUser ()->getId () : self ::DEFAULT_USER_ID ;
253
+ }
175
254
}
0 commit comments