55 */
66namespace Magento \ImportExport \Model \ResourceModel \Import ;
77
8+ use Magento \Backend \Model \Auth \Session ;
9+ use Magento \Framework \App \ObjectManager ;
10+ use Magento \Framework \DB \Select ;
11+
812/**
913 * ImportExport import data resource model
1014 *
1115 * @api
1216 * @since 100.0.2
17+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) Necessary to get current logged in user without modifying methods
1318 */
1419class Data extends \Magento \Framework \Model \ResourceModel \Db \AbstractDb implements \IteratorAggregate
1520{
21+ /**
22+ * Offline import user ID
23+ */
24+ private const DEFAULT_USER_ID = 0 ;
1625 /**
1726 * @var \Iterator
1827 */
@@ -24,21 +33,28 @@ class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implemen
2433 * @var \Magento\Framework\Json\Helper\Data
2534 */
2635 protected $ jsonHelper ;
36+ /**
37+ * @var Session
38+ */
39+ private $ authSession ;
2740
2841 /**
2942 * Class constructor
3043 *
3144 * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
3245 * @param \Magento\Framework\Json\Helper\Data $jsonHelper
33- * @param string $connectionName
46+ * @param string|null $connectionName
47+ * @param Session|null $authSession
3448 */
3549 public function __construct (
3650 \Magento \Framework \Model \ResourceModel \Db \Context $ context ,
3751 \Magento \Framework \Json \Helper \Data $ jsonHelper ,
38- $ connectionName = null
52+ $ connectionName = null ,
53+ ?Session $ authSession = null
3954 ) {
4055 parent ::__construct ($ context , $ connectionName );
4156 $ this ->jsonHelper = $ jsonHelper ;
57+ $ this ->authSession = $ authSession ?? ObjectManager::getInstance ()->get (Session::class);
4258 }
4359
4460 /**
@@ -60,6 +76,7 @@ public function getIterator()
6076 {
6177 $ connection = $ this ->getConnection ();
6278 $ select = $ connection ->select ()->from ($ this ->getMainTable (), ['data ' ])->order ('id ASC ' );
79+ $ select = $ this ->prepareSelect ($ select );
6380 $ stmt = $ connection ->query ($ select );
6481
6582 $ stmt ->setFetchMode (\Zend_Db::FETCH_NUM );
@@ -81,7 +98,7 @@ public function getIterator()
8198 */
8299 public function cleanBunches ()
83100 {
84- return $ this ->getConnection ()->delete ($ this ->getMainTable ());
101+ return $ this ->getConnection ()->delete ($ this ->getMainTable (), $ this -> prepareDelete ([]) );
85102 }
86103
87104 /**
@@ -114,7 +131,9 @@ public function getEntityTypeCode()
114131 public function getUniqueColumnData ($ code )
115132 {
116133 $ 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 ));
118137
119138 if (count ($ values ) != 1 ) {
120139 throw new \Magento \Framework \Exception \LocalizedException (
@@ -169,7 +188,67 @@ public function saveBunch($entity, $behavior, array $data)
169188
170189 return $ this ->getConnection ()->insert (
171190 $ this ->getMainTable (),
172- ['behavior ' => $ behavior , 'entity ' => $ entity , 'data ' => $ encodedData ]
191+ $ this -> prepareInsert ( ['behavior ' => $ behavior , 'entity ' => $ entity , 'data ' => $ encodedData ])
173192 );
174193 }
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+ }
175254}
0 commit comments