Skip to content

Commit 6d7e97d

Browse files
committed
LAC-27: Log admin actions with Login as Customer.
1 parent 5b402d5 commit 6d7e97d

File tree

11 files changed

+64
-26
lines changed

11 files changed

+64
-26
lines changed

app/code/Magento/LoginAsCustomerLog/Controller/Adminhtml/Log/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
class Index extends Action implements HttpGetActionInterface
1919
{
20-
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_log';
20+
const ADMIN_RESOURCE = 'Magento_LoginAsCustomerLog::login_log';
2121

2222
/**
2323
* @inheritdoc

app/code/Magento/LoginAsCustomerLog/Model/GetLogList.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ public function __construct(
6464
public function execute(SearchCriteriaInterface $searchCriteria = null): LogSearchResultsInterface
6565
{
6666
$collection = $this->logCollectionFactory->create();
67-
if (null === $searchCriteria) {
68-
$searchCriteria = $this->searchCriteriaBuilder->create();
69-
} else {
70-
$this->collectionProcessor->process($searchCriteria, $collection);
71-
}
67+
$searchCriteria = $searchCriteria ?: $this->searchCriteriaBuilder->create();
68+
$this->collectionProcessor->process($searchCriteria, $collection);
7269

7370
$searchResult = $this->logSearchResultsFactory->create();
7471
$searchResult->setItems($collection->getItems());

app/code/Magento/LoginAsCustomerLog/Model/ResourceModel/SaveLogs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public function execute(array $logs): void
5050
}
5151
$logTable = $this->resourceConnection->getTableName(Log::TABLE_NAME_LOG);
5252
$connection = $this->resourceConnection->getConnection();
53-
$connection->insertOnDuplicate($logTable, $logsData);
53+
$connection->insertMultiple($logTable, $logsData);
5454
}
5555
}

app/code/Magento/LoginAsCustomerLog/Plugin/LoginAsCustomerApi/Api/AuthenticateCustomerInterface/LogAuthenticationPlugin.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\LoginAsCustomerLog\Api\Data\LogInterfaceFactory;
1414
use Magento\LoginAsCustomerLog\Api\SaveLogsInterface;
1515
use Magento\User\Api\Data\UserInterfaceFactory;
16+
use Magento\User\Model\ResourceModel\User;
1617

1718
/**
1819
* Log user logged in as customer plugin.
@@ -39,22 +40,30 @@ class LogAuthenticationPlugin
3940
*/
4041
private $userFactory;
4142

43+
/**
44+
* @var User
45+
*/
46+
private $userResource;
47+
4248
/**
4349
* @param LogInterfaceFactory $logFactory
4450
* @param SaveLogsInterface $saveLogs
4551
* @param CustomerRepositoryInterface $customerRepository
4652
* @param UserInterfaceFactory $userFactory
53+
* @param User $userResource
4754
*/
4855
public function __construct(
4956
LogInterfaceFactory $logFactory,
5057
SaveLogsInterface $saveLogs,
5158
CustomerRepositoryInterface $customerRepository,
52-
UserInterfaceFactory $userFactory
59+
UserInterfaceFactory $userFactory,
60+
User $userResource
5361
) {
5462
$this->logFactory = $logFactory;
5563
$this->saveLogs = $saveLogs;
5664
$this->customerRepository = $customerRepository;
5765
$this->userFactory = $userFactory;
66+
$this->userResource = $userResource;
5867
}
5968

6069
/**
@@ -74,12 +83,18 @@ public function afterExecute(
7483
$customerId = $data->getCustomerId();
7584
$customerEmail = $this->customerRepository->getById($customerId)->getEmail();
7685
$userId = $data->getAdminId();
77-
$userName = $this->userFactory->create()->load($userId)->getUserName();
78-
$log = $this->logFactory->create();
79-
$log->setCustomerId($customerId);
80-
$log->setUserId($userId);
81-
$log->setCustomerEmail($customerEmail);
82-
$log->setUserName($userName);
86+
$user = $this->userFactory->create();
87+
$this->userResource->load($user, $userId);
88+
$log = $this->logFactory->create(
89+
[
90+
'data' => [
91+
'customer_id' => $customerId,
92+
'user_id' => $userId,
93+
'customer_email' => $customerEmail,
94+
'user_name' => $user->getUserName(),
95+
],
96+
]
97+
);
8398
$this->saveLogs->execute([$log]);
8499
}
85100
}

app/code/Magento/LoginAsCustomerLog/Ui/DataProvider/LogDataProvider.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Api\FilterBuilder;
1111
use Magento\Framework\Api\Search\ReportingInterface;
1212
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
13+
use Magento\Framework\Api\SortOrderBuilder;
1314
use Magento\Framework\App\RequestInterface;
1415
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
1516
use Magento\LoginAsCustomerLog\Api\Data\LogInterface;
@@ -24,25 +25,32 @@ class LogDataProvider extends DataProvider
2425
/**
2526
* @var GetLogsListInterface
2627
*/
27-
private $logRepository;
28+
private $getLogsList;
2829

2930
/**
3031
* @var SearchResultFactory
3132
*/
3233
private $searchResultFactory;
3334

3435
/**
35-
* @param $name
36-
* @param $primaryFieldName
37-
* @param $requestFieldName
36+
* @var SortOrderBuilder
37+
*/
38+
private $sortOrderBuilder;
39+
40+
/**
41+
* @param string $name
42+
* @param string $primaryFieldName
43+
* @param string $requestFieldName
3844
* @param ReportingInterface $reporting
3945
* @param SearchCriteriaBuilder $searchCriteriaBuilder
4046
* @param RequestInterface $request
4147
* @param FilterBuilder $filterBuilder
42-
* @param GetLogsListInterface $logRepository
48+
* @param GetLogsListInterface $getLogsList
4349
* @param SearchResultFactory $searchResultFactory
50+
* @param SortOrderBuilder $sortOrderBuilder
4451
* @param array $meta
4552
* @param array $data
53+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
4654
*/
4755
public function __construct(
4856
$name,
@@ -52,8 +60,9 @@ public function __construct(
5260
SearchCriteriaBuilder $searchCriteriaBuilder,
5361
RequestInterface $request,
5462
FilterBuilder $filterBuilder,
55-
GetLogsListInterface $logRepository,
63+
GetLogsListInterface $getLogsList,
5664
SearchResultFactory $searchResultFactory,
65+
SortOrderBuilder $sortOrderBuilder,
5766
array $meta = [],
5867
array $data = []
5968
) {
@@ -68,8 +77,9 @@ public function __construct(
6877
$meta,
6978
$data
7079
);
71-
$this->logRepository = $logRepository;
80+
$this->getLogsList = $getLogsList;
7281
$this->searchResultFactory = $searchResultFactory;
82+
$this->sortOrderBuilder = $sortOrderBuilder;
7383
}
7484

7585
/**
@@ -78,7 +88,13 @@ public function __construct(
7888
public function getSearchResult()
7989
{
8090
$searchCriteria = $this->getSearchCriteria();
81-
$result = $this->logRepository->execute($searchCriteria);
91+
$sortOrders = $searchCriteria->getSortOrders();
92+
$sortOrder = current($sortOrders);
93+
if (!$sortOrder->getField()) {
94+
$sortOrder = $this->sortOrderBuilder->setDescendingDirection()->setField(LogInterface::TIME)->create();
95+
$searchCriteria->setSortOrders([$sortOrder]);
96+
}
97+
$result = $this->getLogsList->execute($searchCriteria);
8298

8399
return $this->searchResultFactory->create(
84100
$result->getItems(),

app/code/Magento/LoginAsCustomerLog/composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"require": {
55
"php": "~7.1.3||~7.2.0||~7.3.0",
66
"magento/framework": "*",
7-
"magento/module-backend": "*"
7+
"magento/module-backend": "*",
8+
"magento/module-customer": "*",
9+
"magento/module-login-as-customer-api": "*",
10+
"magento/module-ui": "*",
11+
"magento/module-user": "*"
812
},
913
"suggest": {
1014
"magento/module-login-as-customer": "*"

app/code/Magento/LoginAsCustomerLog/etc/acl.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<resource id="Magento_Backend::admin">
1212
<resource id="Magento_Customer::customer">
1313
<resource id="Magento_LoginAsCustomer::login" title="Login as Customer" sortOrder="50">
14-
<resource id="Magento_LoginAsCustomer::login_log" title="View Login as Customer Log" sortOrder="20"/>
14+
<resource id="Magento_LoginAsCustomerLog::login_log" title="View Login as Customer Log" sortOrder="20"/>
1515
</resource>
1616
</resource>
1717
</resource>

app/code/Magento/LoginAsCustomerLog/etc/adminhtml/menu.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
module="Magento_LoginAsCustomerLog"
1414
parent="Magento_Customer::customer"
1515
sortOrder="40"
16-
resource="Magento_LoginAsCustomer::login_log"
16+
resource="Magento_LoginAsCustomerLog::login_log"
1717
action="loginascustomer_log/log/index"/>
1818
</menu>
1919
</config>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
<constraint xsi:type="primary" referenceId="PRIMARY">
1717
<column name="log_id"/>
1818
</constraint>
19+
<index referenceId="MAGENTO_LOGIN_AS_CUSTOMER_LOG_USER_ID" indexType="btree">
20+
<column name="user_id"/>
21+
</index>
1922
</table>
2023
</schema>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
},
1010
"constraint": {
1111
"PRIMARY": true
12+
},
13+
"index": {
14+
"MAGENTO_LOGIN_AS_CUSTOMER_LOG_USER_ID": true
1215
}
1316
}
1417
}

0 commit comments

Comments
 (0)