/
AuditLog.php
220 lines (197 loc) · 8.96 KB
/
AuditLog.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
class AuditLog extends PluginBase {
protected $storage = 'DbStorage';
static protected $description = 'Example plugin';
static protected $name = 'auditlog';
public function __construct(PluginManager $manager, $id) {
parent::__construct($manager, $id);
$this->subscribe('beforeSurveySettings');
$this->subscribe('newSurveySettings');
$this->subscribe('beforeActivate');
$this->subscribe('beforeUserSave');
$this->subscribe('beforeUserDelete');
$this->subscribe('beforePermissionSetSave');
$this->subscribe('beforeParticipantSave');
$this->subscribe('beforeParticipantDelete');
}
/**
* Saves permissions changes to the audit log
* @param PluginEvent $event
*/
public function beforePermissionSetSave(PluginEvent $event)
{
$aNewPermissions=$event->get('aNewPermissions');
$iSurveyID=$event->get('iSurveyID');
$iUserID=$event->get('iUserID');
$oCurrentUser=$this->api->getCurrentUser();
$oOldPermission=$this->api->getUserPermissionSet($iUserID,$iSurveyID);
$sAction='update'; // Permissions are in general only updated (either you have a permission or you don't)
if (count(array_diff_assoc_recursive($aNewPermissions,$oOldPermission)))
{
$oAutoLog = $this->api->newModel($this, 'log');
$oAutoLog->uid=$oCurrentUser->uid;
$oAutoLog->entity='permission';
$oAutoLog->entityid=$aNewPermissions['uid'].'-'.$aNewPermissions['sid'];
$oAutoLog->action=$sAction;
$oAutoLog->oldvalues=json_encode(array_diff_assoc_recursive($oOldPermission,$aNewPermissions));
$oAutoLog->newvalues=json_encode(array_diff_assoc_recursive($aNewPermissions,$oOldPermission));
$oAutoLog->fields=implode(',',array_keys(array_diff_assoc_recursive($aNewPermissions,$oOldPermission)));
$oAutoLog->save();
}
}
/**
* Function catches if a participant was modified or created
* All data is saved - only the password hash is anonymized for security reasons
*
* @param PluginEvent $event
*/
public function beforeParticipantSave(PluginEvent $event)
{
$oNewParticipant=$event->getSender();
if ($oNewParticipant->isNewRecord)
{
return;
}
$oCurrentUser=$this->api->getCurrentUser();
$aOldValues=$this->api->getParticipant($oNewParticipant->participant_id)->getAttributes();
$aNewValues=$oNewParticipant->getAttributes();
if (count(array_diff_assoc($aNewValues,$aOldValues)))
{
$oAutoLog = $this->api->newModel($this, 'log');
$oAutoLog->uid=$oCurrentUser->uid;
$oAutoLog->entity='participant';
$oAutoLog->action='update';
$oAutoLog->entityid=$aNewValues['participant_id'];
$oAutoLog->oldvalues=json_encode(array_diff_assoc($aOldValues,$aNewValues));
$oAutoLog->newvalues=json_encode(array_diff_assoc($aNewValues,$aOldValues));
$oAutoLog->fields=implode(',',array_keys(array_diff_assoc($aNewValues,$aOldValues)));
$oAutoLog->save();
}
}
/**
* Function catches if a participant was modified or created
* All data is saved - only the password hash is anonymized for security reasons
*
* @param PluginEvent $event
*/
public function beforeParticipantDelete(PluginEvent $event)
{
$oNewParticipant=$event->getSender();
$oCurrentUser=$this->api->getCurrentUser();
$aValues=$oNewParticipant->getAttributes();
$oAutoLog = $this->api->newModel($this, 'log');
$oAutoLog->uid=$oCurrentUser->uid;
$oAutoLog->entity='participant';
$oAutoLog->action='delete';
$oAutoLog->entityid=$aValues['participant_id'];
$oAutoLog->oldvalues=json_encode($aValues);
$oAutoLog->fields=implode(',',array_keys($aValues));
$oAutoLog->save();
}
/**
* Function catches if a user was modified or created
* All data is saved - only the password hash is anonymized for security reasons
*
* @param PluginEvent $event
*/
public function beforeUserSave(PluginEvent $event)
{
$oUserData=$event->getSender();
$oCurrentUser=$this->api->getCurrentUser();
$oOldUser=$this->api->getUser($oUserData->uid);
if (!$oOldUser)
{
$sAction='create';
$aOldValues=array();
}
else
{
$sAction='update';
$aOldValues=$oOldUser->getAttributes();
}
$aNewValues=$oUserData->getAttributes();
// If the password has changed then indicate that it has changed but assign fake hashes
if ($aNewValues['password']!=$aOldValues['password'])
{
$aOldValues['password']=hash('md5','12345');
$aNewValues['password']=hash('md5','67890');
};
if (count(array_diff_assoc($aNewValues,$aOldValues)))
{
$oAutoLog = $this->api->newModel($this, 'log');
$oAutoLog->uid=$oCurrentUser->uid;
$oAutoLog->entity='user';
if ($sAction=='update') $oAutoLog->entityid=$oOldUser['uid'];
$oAutoLog->action=$sAction;
$oAutoLog->oldvalues=json_encode(array_diff_assoc($aOldValues,$aNewValues));
$oAutoLog->newvalues=json_encode(array_diff_assoc($aNewValues,$aOldValues));
$oAutoLog->fields=implode(',',array_keys(array_diff_assoc($aNewValues,$aOldValues)));
$oAutoLog->save();
}
}
public function beforeUserDelete(PluginEvent $event)
{
$oUserData=$event->getSender();
$oCurrentUser=$this->api->getCurrentUser();
$oOldUser=$this->api->getUser($oUserData->uid);
if ($oOldUser)
{
$aOldValues=$oOldUser->getAttributes();
unset($aOldValues['password']);
$oAutoLog = $this->api->newModel($this, 'log');
$oAutoLog->uid=$oCurrentUser->uid;
$oAutoLog->entity='user';
$oAutoLog->entityid=$oOldUser['uid'];
$oAutoLog->action='delete';
$oAutoLog->oldvalues=json_encode($aOldValues);
$oAutoLog->fields=implode(',',array_keys($aOldValues));
$oAutoLog->save();
}
}
public function beforeActivate(PluginEvent $event)
{
if (!$this->api->tableExists($this, 'log'))
{
$this->api->createTable($this, 'log', array('id'=>'pk',
'created'=>'datetime',
'uid'=>'string',
'entity'=>'string',
'entityid'=>'string',
'action'=>'string',
'fields'=>'text',
'oldvalues'=>'text',
'newvalues'=>'text'));
}
}
/**
* This event is fired by the administration panel to gather extra settings
* available for a survey.
* The plugin should return setting meta data.
* @param PluginEvent $event
*/
public function beforeSurveySettings(PluginEvent $event)
{
$event->set("surveysettings.{$this->id}", array(
'name' => get_class($this),
'settings' => array(
'auditing' => array(
'type' => 'select',
'options'=>array(0=>'No',
1=>'Yes'),
'default'=>0,
'tab'=>'notification', // @todo: Setting no used yet
'category'=>'Auditing for person-related data', // @todo: Setting no used yet
'label' => 'Audit log for this survey:',
'current' => $this->get('auditing', 'Survey', $event->get('survey'))
)
)
));
}
public function newSurveySettings(PluginEvent $event)
{
foreach ($event->get('settings') as $name => $value)
{
$this->set($name, $value, 'Survey', $event->get('survey'));
}
}
}