-
Notifications
You must be signed in to change notification settings - Fork 10
/
Plugin.php
338 lines (313 loc) · 13.4 KB
/
Plugin.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace LukeTowers\EasyAudit;
use Backend;
use Backend\Models\User;
use Config;
use Event;
use LukeTowers\EasyAudit\Behaviors\TrackableModel;
use LukeTowers\EasyAudit\Models\Activity;
use System\Classes\PluginBase;
/**
* EasyAudit Plugin Information File
*
* TODO:
* - General Facade for generating activity entries
* - Documentation
* - Add trackableGetRecordName and trackableGetRecordUrl methods for the activity log to detect the name and URL for a given activity target
* - Implement templateable descriptions through supporting language strings, some way to provide the attributes and their assigned keys
* for being used in the language string as variables. ($activity->description(':event was triggered by :source.full_name'))
* To be considered: Import / export of log entries when using language strings. Reevaluate pros/cons of using translateable strings
* for the event description in the first place. Perhaps use a system allowing admins to replace event name / description labels for
* other users.
*
* TODO: Paid version (SystemAuditer or something like that)
* - Implement ability to enable this plugin's features on other third party plugins that don't actually have support for this plugin built in
* Could be very useful, especially the revisionable trait, and even just in general the ability to listen to other plugins and configure auditing for them automatically
* - Implement ability to have configurable drivers for ouput of the tracking capabilities
* - Implement Revisionable / trackable properties abilities
* Have an option as a model property as to what events to track the revisions on, and then in the backend, you would have the ability to restore revisions (as their own incremental change), but only on the events that revisions are tracked on
* - Implement configurable rolling log system to dump activity db contents (perhaps even create export jobs for different activity queries),
* that will remove db entries past a set date and export them into an importable format and compress them on the disk. Could be part of
* a "pro" release
*/
class Plugin extends PluginBase
{
/**
* @var bool Plugin requires elevated permissions in order to continue logging changes
* in privileged areas of the application
*/
public $elevated = true;
/**
* Returns information about this plugin.
*/
public function pluginDetails(): array
{
return [
'name' => 'luketowers.easyaudit::lang.plugin.name',
'description' => 'luketowers.easyaudit::lang.plugin.description',
'author' => 'Luke Towers',
'icon' => 'icon-list-alt',
'homepage' => 'https://github.com/LukeTowers/wn-easyaudit-plugin',
];
}
/**
* Registers any backend permissions used by this plugin.
*/
public function registerPermissions(): array
{
return [
'luketowers.easyaudit.clear_logs' => [
'tab' => 'luketowers.easyaudit::lang.plugin.name',
'label' => 'luketowers.easyaudit::lang.permissions.clear_logs'
],
'luketowers.easyaudit.activities.view_all' => [
'tab' => 'luketowers.easyaudit::lang.plugin.name',
'label' => 'luketowers.easyaudit::lang.permissions.activities.view_all'
],
'luketowers.easyaudit.activities.view_own' => [
'tab' => 'luketowers.easyaudit::lang.plugin.name',
'label' => 'luketowers.easyaudit::lang.permissions.activities.view_own'
],
];
}
/**
* Registers the settings used by this plugin
*/
public function registerSettings(): array
{
return [
'logs' => [
'label' => 'luketowers.easyaudit::lang.controllers.activities.label',
'description' => 'luketowers.easyaudit::lang.controllers.activities.description',
'icon' => 'icon-eye',
'url' => Backend::url('luketowers/easyaudit/activities'),
'order' => 1100,
'permissions' => [
'luketowers.easyaudit.activities.*',
],
'category' => \System\Classes\SettingsManager::CATEGORY_LOGS,
],
];
}
/**
* Register the plugin's form widgets
*/
public function registerFormWidgets(): array
{
return [
\LukeTowers\EasyAudit\FormWidgets\ActivityLog::class => 'activitylog',
];
}
/**
* Register the plugin's report widgets
*/
public function registerReportWidgets(): array
{
return [
\LukeTowers\EasyAudit\ReportWidgets\MyActivities::class => [
'label' => 'luketowers.easyaudit::lang.widgets.myactivities.label',
'context' => 'dashboard',
'permissions' => [
'luketowers.easyaudit.activities.view_own'
],
],
\LukeTowers\EasyAudit\ReportWidgets\SystemActivities::class => [
'label' => 'luketowers.easyaudit::lang.widgets.systemactivities.label',
'context' => 'dashboard',
'permissions' => [
'luketowers.easyaudit.activities.view_all'
],
],
];
}
/**
* Runs when the plugin is booted
*/
public function boot(): void
{
$this->registerMediaLibraryTracking();
$this->registerModelTracking();
$this->extendBackendForms();
$this->extendBackendUserModel();
}
/**
* Setup the media library tracking
*/
protected function registerMediaLibraryTracking(): void
{
Event::listen('media.*', function ($eventName, $params) {
$action = '';
switch ($eventName) {
case 'media.file.delete':
case 'media.folder.delete':
$action = 'deleted';
break;
case 'media.file.move':
case 'media.folder.move':
$action = 'moved';
break;
case 'media.file.rename':
case 'media.folder.rename':
$action = 'renamed';
break;
case 'media.file.upload':
case 'media.file.streamedUpload':
$action = 'uploaded';
break;
case 'media.folder.create':
$action = 'created';
break;
}
$properties = [];
if (count($params) === 3 && is_string($params[2])) {
$description = $params[1] . " was $action to " . $params[2];
$properties = [
'changes' => [
'path' => [
'from' => $params[1],
'to' => $params[2],
],
],
];
} else {
$description = $params[1] . " was $action";
$properties = ['path' => $params[1]];
}
audit()
->inLog('System.Media')
->event($eventName)
->description($description)
->properties($properties)
->log();
});
}
/**
* Setup the model tracking
*/
protected function registerModelTracking(): void
{
$modelsToTrack = Config::get('luketowers.easyaudit::modelsToTrack', []);
foreach ($modelsToTrack as $class => $config) {
if (is_array($config)) {
$modelClass = $class;
} else {
$modelClass = $config;
$config = [];
}
if (!class_exists($modelClass)) {
continue;
}
$modelClass::extend(function ($model) use ($config) {
$model->addDynamicProperty('trackableIgnoredAttributes', $config['ignoredAttributes'] ?? []);
$model->extendClassWith(TrackableModel::class);
});
}
}
/**
* Extend the backend forms to add the activity log widget to models
* implementing the TrackableModel behavior
*/
protected function extendBackendForms(): void
{
if ($this->app->runningInBackend()) {
// Add the audit log to models implementing trackable model
Event::listen('backend.form.extendFieldsBefore', function (\Backend\Widgets\Form $widget) {
if (
$widget->isNested
|| (
method_exists($widget->model, 'isClassExtendedWith')
&& !$widget->model->isClassExtendedWith(TrackableModel::class)
)
|| !(
$widget->model->trackableInjectActvitiesFormWidget
?? Config::get('luketowers.easyaudit.autoInjectActvitiesFormWidget', true)
)
) {
return;
}
$tabsFields = $widget->tabs['fields'] ?? [];
$secondaryTabsFields = $widget->secondaryTabs['fields'] ?? [];
$location = (count($tabsFields) > count($secondaryTabsFields)) ? 'tabs' : 'secondaryTabs';
$widget->{$location}['fields'] = array_merge(${$location . 'Fields'}, [
'activities' => [
'tab' => 'luketowers.easyaudit::lang.models.activity.audit_log',
'context' => ['update', 'preview', 'relation'],
'type' => 'activitylog',
'span' => 'full',
'cssClass' => 'container'
],
]);
$widget->{$location}['icons']['luketowers.easyaudit::lang.models.activity.audit_log'] = 'icon-eye';
});
}
}
/**
* Extend the backend user model to add the $user->name accessor for
* use in the activity log
*/
protected function extendBackendUserModel(): void
{
User::extend(function ($model) {
if (empty($model->name)) {
$model->addDynamicMethod('getNameAttribute', function () use ($model) {
return $model->first_name . ' ' . $model->last_name;
});
}
// Setup the inverse of the polymorphic ActivityModel relationship to this model
$model->addMorphManyRelation('user_activities', [Activity::class, 'name' => 'source']);
// Hide activities from the array version of the model
$model->addHidden(['user_activities']);
});
// Add the audit log to models implementing trackable model
Event::listen('backend.form.extendFieldsBefore', function (\Backend\Widgets\Form $widget) {
if (
$widget->isNested
|| !($widget->model instanceof User)
|| !($widget->getController() instanceof \Backend\Controllers\Users)
) {
return;
}
$tabsFields = $widget->tabs['fields'] ?? [];
$secondaryTabsFields = $widget->secondaryTabs['fields'] ?? [];
$location = (count($tabsFields) > count($secondaryTabsFields)) ? 'tabs' : 'secondaryTabs';
if ($widget->context === 'myaccount') {
$widget->{$location}['fields'] = array_merge(${$location . 'Fields'}, [
// Only users with view_all permissions should be able to see the audit log
// for their own account as it could reveal sensitive information about the
// system administrators
'activities@myaccount' => [
'tab' => 'luketowers.easyaudit::lang.models.activity.audit_log',
'type' => 'activitylog',
'permissions' => ['luketowers.easyaudit.activities.view_all'],
'span' => 'full',
'cssClass' => 'container'
],
'own_activities' => [
'tab' => 'luketowers.easyaudit::lang.models.activity.label_plural',
'context' => ['myaccount'],
'type' => 'activitylog',
'permissions' => ['luketowers.easyaudit.activities.view_own'],
'subject' => false,
'source' => 'formModel',
'span' => 'full',
'cssClass' => 'container'
],
]);
} else {
$widget->{$location}['fields'] = array_merge(${$location . 'Fields'}, [
'user_activities' => [
'tab' => 'luketowers.easyaudit::lang.models.activity.label_plural',
'context' => ['update', 'preview'],
'permissions' => ['luketowers.easyaudit.activities.view_all'],
'type' => 'activitylog',
'subject' => false,
'source' => 'formModel',
'span' => 'full',
'cssClass' => 'container'
],
]);
}
$widget->{$location}['icons']['luketowers.easyaudit::lang.models.activity.label_plural'] = 'icon-hourglass';
});
}
}