This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminUserPlugin.php
139 lines (123 loc) · 3.73 KB
/
AdminUserPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Ryvon\EventLog\Plugin;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\User\Model\ResourceModel\User as UserResourceModel;
use Magento\User\Model\User;
use Magento\User\Model\UserFactory;
/**
* Plugin to monitor the saving and deletion of admin users.
*/
class AdminUserPlugin
{
/**
* @var ManagerInterface
*/
private $eventManager;
/**
* @var UserResourceModel
*/
private $userResourceModel;
/**
* @var UserFactory
*/
private $userFactory;
/**
* @var RequestInterface
*/
private $request;
/**
* @param ManagerInterface $eventManager
* @param UserResourceModel $userResourceModel
* @param UserFactory $userFactory
* @param RequestInterface $request
*/
public function __construct(
ManagerInterface $eventManager,
UserResourceModel $userResourceModel,
UserFactory $userFactory,
RequestInterface $request
) {
$this->eventManager = $eventManager;
$this->userResourceModel = $userResourceModel;
$this->userFactory = $userFactory;
$this->request = $request;
}
/**
* Wrap the save function to add an event log on save and create.
*
* @param User $subject
* @param callable $proceed
* @return mixed
*/
public function aroundSave(User $subject, callable $proceed)
{
// We proceed with the save to obtain the ID in case this is a new user.
$return = $proceed();
if ($this->isEditingUser()) {
$this->eventManager->dispatch('event_log_info', [
'group' => 'admin',
'message' => 'Admin user {user} {action}.',
'context' => [
'user' => [
'text' => $subject->getUserName(),
'id' => $subject->getId(),
],
'action' => $subject->isObjectNew() ? 'created' : 'modified',
],
]);
}
return $return;
}
/**
* Add an event log on delete.
*
* @param User $subject
* @return array
*/
public function beforeDelete(User $subject): array
{
// We load the user since during testing the subject only contained the ID, not the username.
$user = $this->userFactory->create();
$this->userResourceModel->load($user, $subject->getId());
if ($user->getId()) {
$this->eventManager->dispatch('event_log_info', [
'group' => 'admin',
'message' => 'Admin user {user} {action}.',
'context' => [
'user' => [
'text' => $user->getUserName(),
'id' => $user->getId(),
],
'action' => 'deleted',
],
]);
}
return [];
}
/**
* Checks whether the current request is a post request to the user edit page.
*
* This is needed due to the role form modifying the user on save.
*
* @return bool
*/
private function isEditingUser(): bool
{
if (!$this->request instanceof Http) {
return false;
}
if (!$this->request->isPost() || !in_array($this->request->getFullActionName(), [
// Editing their own account
'adminhtml_system_account_save',
'adminhtml_system_account_delete',
// Editing another user's account
'adminhtml_user_save',
'adminhtml_user_delete',
])) {
return false;
}
return true;
}
}