Skip to content
This repository was archived by the owner on May 20, 2019. It is now read-only.

Commit f20a58c

Browse files
author
Volodymyr Kublytskyi
committed
ENGCOM-2976: The user passed for async api referenced correct WebAPI user #22
2 parents b4c1595 + f708896 commit f20a58c

File tree

9 files changed

+89
-13
lines changed

9 files changed

+89
-13
lines changed

app/code/Magento/AsynchronousOperations/Api/Data/BulkSummaryInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
interface BulkSummaryInterface extends \Magento\Framework\Bulk\BulkSummaryInterface
1515
{
16+
const USER_TYPE = 'user_type';
17+
1618
/**
1719
* Retrieve existing extension attributes object.
1820
*
@@ -31,4 +33,19 @@ public function getExtensionAttributes();
3133
public function setExtensionAttributes(
3234
\Magento\AsynchronousOperations\Api\Data\BulkSummaryExtensionInterface $extensionAttributes
3335
);
36+
37+
/**
38+
* Get user type
39+
*
40+
* @return int
41+
*/
42+
public function getUserType();
43+
44+
/**
45+
* Set user type
46+
*
47+
* @param int $userType
48+
* @return $this
49+
*/
50+
public function setUserType($userType);
3451
}

app/code/Magento/AsynchronousOperations/Model/BulkManagement.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\AsynchronousOperations\Model;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\App\ResourceConnection;
910
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
1011
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory;
@@ -13,6 +14,7 @@
1314
use Magento\Framework\EntityManager\EntityManager;
1415
use Magento\Framework\EntityManager\MetadataPool;
1516
use Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory;
17+
use Magento\Authorization\Model\UserContextInterface;
1618

1719
/**
1820
* Class BulkManagement
@@ -51,6 +53,11 @@ class BulkManagement implements \Magento\Framework\Bulk\BulkManagementInterface
5153
*/
5254
private $resourceConnection;
5355

56+
/**
57+
* @var \Magento\Authorization\Model\UserContextInterface
58+
*/
59+
private $userContext;
60+
5461
/**
5562
* @var \Psr\Log\LoggerInterface
5663
*/
@@ -65,6 +72,7 @@ class BulkManagement implements \Magento\Framework\Bulk\BulkManagementInterface
6572
* @param MetadataPool $metadataPool
6673
* @param ResourceConnection $resourceConnection
6774
* @param \Psr\Log\LoggerInterface $logger
75+
* @param UserContextInterface $userContext
6876
*/
6977
public function __construct(
7078
EntityManager $entityManager,
@@ -73,7 +81,8 @@ public function __construct(
7381
BulkPublisherInterface $publisher,
7482
MetadataPool $metadataPool,
7583
ResourceConnection $resourceConnection,
76-
\Psr\Log\LoggerInterface $logger
84+
\Psr\Log\LoggerInterface $logger,
85+
UserContextInterface $userContext = null
7786
) {
7887
$this->entityManager = $entityManager;
7988
$this->bulkSummaryFactory= $bulkSummaryFactory;
@@ -82,6 +91,7 @@ public function __construct(
8291
$this->resourceConnection = $resourceConnection;
8392
$this->publisher = $publisher;
8493
$this->logger = $logger;
94+
$this->userContext = $userContext ?: ObjectManager::getInstance()->get(UserContextInterface::class);
8595
}
8696

8797
/**
@@ -93,13 +103,18 @@ public function scheduleBulk($bulkUuid, array $operations, $description, $userId
93103
$connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
94104
// save bulk summary and related operations
95105
$connection->beginTransaction();
106+
$userType = $this->userContext->getUserType();
107+
if ($userType === null) {
108+
$userType = UserContextInterface::USER_TYPE_ADMIN;
109+
}
96110
try {
97111
/** @var \Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface $bulkSummary */
98112
$bulkSummary = $this->bulkSummaryFactory->create();
99113
$this->entityManager->load($bulkSummary, $bulkUuid);
100114
$bulkSummary->setBulkId($bulkUuid);
101115
$bulkSummary->setDescription($description);
102116
$bulkSummary->setUserId($userId);
117+
$bulkSummary->setUserType($userType);
103118
$bulkSummary->setOperationCount((int)$bulkSummary->getOperationCount() + count($operations));
104119

105120
$this->entityManager->save($bulkSummary);

app/code/Magento/AsynchronousOperations/Model/BulkSummary.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ public function setUserId($userId)
7878
return $this->setData(self::USER_ID, $userId);
7979
}
8080

81+
/**
82+
* @inheritDoc
83+
*/
84+
public function getUserType()
85+
{
86+
return $this->getData(self::USER_TYPE);
87+
}
88+
89+
/**
90+
* @inheritDoc
91+
*/
92+
public function setUserType($userType)
93+
{
94+
return $this->setData(self::USER_TYPE, $userType);
95+
}
96+
8197
/**
8298
* @inheritDoc
8399
*/

app/code/Magento/AsynchronousOperations/Model/MassSchedule.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\AsynchronousOperations\Model;
1010

11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\DataObject\IdentityGeneratorInterface;
1213
use Magento\Framework\Exception\LocalizedException;
1314
use Magento\AsynchronousOperations\Api\Data\ItemStatusInterfaceFactory;
@@ -18,9 +19,12 @@
1819
use Magento\Framework\Exception\BulkException;
1920
use Psr\Log\LoggerInterface;
2021
use Magento\AsynchronousOperations\Model\ResourceModel\Operation\OperationRepository;
22+
use Magento\Authorization\Model\UserContextInterface;
2123

2224
/**
2325
* Class MassSchedule used for adding multiple entities as Operations to Bulk Management with the status tracking
26+
*
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) Suppressed without refactoring to not introduce BiC
2428
*/
2529
class MassSchedule
2630
{
@@ -54,6 +58,11 @@ class MassSchedule
5458
*/
5559
private $operationRepository;
5660

61+
/**
62+
* @var \Magento\Authorization\Model\UserContextInterface
63+
*/
64+
private $userContext;
65+
5766
/**
5867
* Initialize dependencies.
5968
*
@@ -63,30 +72,33 @@ class MassSchedule
6372
* @param BulkManagementInterface $bulkManagement
6473
* @param LoggerInterface $logger
6574
* @param OperationRepository $operationRepository
75+
* @param UserContextInterface $userContext
6676
*/
6777
public function __construct(
6878
IdentityGeneratorInterface $identityService,
6979
ItemStatusInterfaceFactory $itemStatusInterfaceFactory,
7080
AsyncResponseInterfaceFactory $asyncResponseFactory,
7181
BulkManagementInterface $bulkManagement,
7282
LoggerInterface $logger,
73-
OperationRepository $operationRepository
83+
OperationRepository $operationRepository,
84+
UserContextInterface $userContext = null
7485
) {
7586
$this->identityService = $identityService;
7687
$this->itemStatusInterfaceFactory = $itemStatusInterfaceFactory;
7788
$this->asyncResponseFactory = $asyncResponseFactory;
7889
$this->bulkManagement = $bulkManagement;
7990
$this->logger = $logger;
8091
$this->operationRepository = $operationRepository;
92+
$this->userContext = $userContext ?: ObjectManager::getInstance()->get(UserContextInterface::class);
8193
}
8294

8395
/**
8496
* Schedule new bulk operation based on the list of entities
8597
*
86-
* @param $topicName
87-
* @param $entitiesArray
88-
* @param null $groupId
89-
* @param null $userId
98+
* @param string $topicName
99+
* @param array $entitiesArray
100+
* @param string $groupId
101+
* @param string $userId
90102
* @return AsyncResponseInterface
91103
* @throws BulkException
92104
* @throws LocalizedException
@@ -95,6 +107,10 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
95107
{
96108
$bulkDescription = __('Topic %1', $topicName);
97109

110+
if ($userId == null) {
111+
$userId = $this->userContext->getUserId();
112+
}
113+
98114
if ($groupId == null) {
99115
$groupId = $this->identityService->generateId();
100116

app/code/Magento/AsynchronousOperations/Test/Unit/Model/BulkManagementTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function testScheduleBulk()
108108
$bulkUuid = 'bulk-001';
109109
$description = 'Bulk summary description...';
110110
$userId = 1;
111+
$userType = \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN;
111112
$connectionName = 'default';
112113
$topicNames = ['topic.name.0', 'topic.name.1'];
113114
$operation = $this->getMockBuilder(\Magento\AsynchronousOperations\Api\Data\OperationInterface::class)
@@ -131,6 +132,7 @@ public function testScheduleBulk()
131132
$bulkSummary->expects($this->once())->method('setBulkId')->with($bulkUuid)->willReturnSelf();
132133
$bulkSummary->expects($this->once())->method('setDescription')->with($description)->willReturnSelf();
133134
$bulkSummary->expects($this->once())->method('setUserId')->with($userId)->willReturnSelf();
135+
$bulkSummary->expects($this->once())->method('setUserType')->with($userType)->willReturnSelf();
134136
$bulkSummary->expects($this->once())->method('getOperationCount')->willReturn(1);
135137
$bulkSummary->expects($this->once())->method('setOperationCount')->with(3)->willReturnSelf();
136138
$this->entityManager->expects($this->once())->method('save')->with($bulkSummary)->willReturn($bulkSummary);

app/code/Magento/AsynchronousOperations/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"magento/module-authorization": "*",
1111
"magento/module-backend": "*",
1212
"magento/module-ui": "*",
13-
"magento/module-user": "*",
1413
"php": "~7.1.3||~7.2.0"
1514
},
1615
"suggest": {

app/code/Magento/AsynchronousOperations/etc/db_schema.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
<column xsi:type="varbinary" name="uuid" nullable="true" length="39"
1515
comment="Bulk UUID (can be exposed to reference bulk entity)"/>
1616
<column xsi:type="int" name="user_id" padding="10" unsigned="true" nullable="true" identity="false"
17-
comment="ID of the user that performed an action"/>
17+
comment="ID of the WebAPI user that performed an action"/>
18+
<column xsi:type="int" name="user_type" nullable="true" comment="Which type of user"/>
1819
<column xsi:type="varchar" name="description" nullable="true" length="255" comment="Bulk Description"/>
1920
<column xsi:type="int" name="operation_count" padding="10" unsigned="true" nullable="false" identity="false"
2021
comment="Total number of operations scheduled within this bulk"/>
@@ -23,11 +24,12 @@
2324
<constraint xsi:type="primary" name="PRIMARY">
2425
<column name="id"/>
2526
</constraint>
26-
<constraint xsi:type="foreign" name="MAGENTO_BULK_USER_ID_ADMIN_USER_USER_ID" table="magento_bulk"
27-
column="user_id" referenceTable="admin_user" referenceColumn="user_id" onDelete="CASCADE"/>
2827
<constraint xsi:type="unique" name="MAGENTO_BULK_UUID">
2928
<column name="uuid"/>
3029
</constraint>
30+
<index name="MAGENTO_BULK_USER_ID_ADMIN_USER_USER_ID" indexType="btree">
31+
<column name="user_id"/>
32+
</index>
3133
</table>
3234
<table name="magento_operation" resource="default" engine="innodb" comment="Operation entity">
3335
<column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" identity="true"

app/code/Magento/AsynchronousOperations/etc/db_schema_whitelist.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
"id": true,
55
"uuid": true,
66
"user_id": true,
7+
"user_type": true,
78
"description": true,
89
"operation_count": true,
910
"start_time": true
1011
},
12+
"index": {
13+
"MAGENTO_BULK_USER_ID_ADMIN_USER_USER_ID": true,
14+
"MAGENTO_BULK_USER_ID": true
15+
},
1116
"constraint": {
1217
"PRIMARY": true,
13-
"MAGENTO_BULK_USER_ID_ADMIN_USER_USER_ID": true,
1418
"MAGENTO_BULK_UUID": true
1519
}
1620
},

dev/tests/integration/testsuite/Magento/AsynchronousOperations/_files/bulk.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,35 @@
1818
'not_started' => [
1919
'uuid' => 'bulk-uuid-1',
2020
'user_id' => 1,
21+
'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
2122
'description' => 'Bulk Description',
2223
'operation_count' => 1,
2324
],
2425
'in_progress_success' => [
2526
'uuid' => 'bulk-uuid-2',
2627
'user_id' => 1,
28+
'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
2729
'description' => 'Bulk Description',
2830
'operation_count' => 3,
2931
],
3032
'in_progress_failed' => [
3133
'uuid' => 'bulk-uuid-3',
3234
'user_id' => 1,
35+
'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
3336
'description' => 'Bulk Description',
3437
'operation_count' => 2,
3538
],
3639
'finish_success' => [
3740
'uuid' => 'bulk-uuid-4',
3841
'user_id' => 1,
42+
'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
3943
'description' => 'Bulk Description',
4044
'operation_count' => 1,
4145
],
4246
'finish_failed' => [
4347
'uuid' => 'bulk-uuid-5',
4448
'user_id' => 1,
49+
'user_type' => \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN,
4550
'description' => 'Bulk Description',
4651
'operation_count' => 2,
4752
],
@@ -90,8 +95,8 @@
9095
],
9196
];
9297

93-
$bulkQuery = "INSERT INTO {$bulkTable} (`uuid`, `user_id`, `description`, `operation_count`)"
94-
. " VALUES (:uuid, :user_id, :description, :operation_count);";
98+
$bulkQuery = "INSERT INTO {$bulkTable} (`uuid`, `user_id`, `user_type`, `description`, `operation_count`)"
99+
. " VALUES (:uuid, :user_id, :user_type, :description, :operation_count);";
95100
foreach ($bulks as $bulk) {
96101
$connection->query($bulkQuery, $bulk);
97102
}

0 commit comments

Comments
 (0)