Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
402 lines (370 loc) · 16.2 KB
/
Copy pathPlugin.php
File metadata and controls
402 lines (370 loc) · 16.2 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
namespace LukeTowers\EasyAudit;
use Backend\Facades\Backend;
use Backend\Models\User;
use LukeTowers\EasyAudit\Behaviors\TrackableModel;
use LukeTowers\EasyAudit\FormWidgets\ActivityLog;
use LukeTowers\EasyAudit\FormWidgets\ChangeViewer;
use LukeTowers\EasyAudit\Models\Activity;
use System\Classes\PluginBase;
use System\Classes\PluginManager;
use Winter\Storm\Exception\SystemException;
use Winter\Storm\Support\Facades\Config;
use Winter\Storm\Support\Facades\Event;
/**
* 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 array Form sections the activities FormWidget can be explicitly injected into
*/
const ACTIVITIES_FORM_WIDGET_LOCATIONS = ['fields', 'tabs', 'secondaryTabs'];
/**
* @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 [
ActivityLog::class => 'activitylog',
ChangeViewer::class => 'changeviewer',
];
}
/**
* 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
{
if (PluginManager::$noInit && $this->app->runningInConsole()) {
return;
}
$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) {
// Only inject when the config actually specifies it. An
// unconditional call adds an empty dynamic property, and
// dynamic properties take precedence over real ones in
// ExtendableTrait::extendableGet(), so a model that declares
// its own $trackableIgnoredAttributes would have it shadowed
// purely by being listed in modelsToTrack.
if (array_key_exists('ignoredAttributes', $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)
)
) {
return;
}
$location = $this->resolveActivitiesFormWidgetLocation($widget);
if (empty($location)) {
return;
}
$widget->{$location}['fields'] = array_merge($widget->{$location}['fields'] ?? [], [
'activities' => [
'tab' => 'luketowers.easyaudit::lang.models.activity.audit_log',
'context' => ['update', 'preview', 'relation'],
'type' => 'activitylog',
'span' => 'full',
'cssClass' => 'container'
],
]);
// The outside fields aren't tabbed, so there's no tab to put an icon on.
if ($location !== 'fields') {
$widget->{$location}['icons']['luketowers.easyaudit::lang.models.activity.audit_log'] = 'icon-eye';
}
});
}
}
/**
* Resolve which section of the provided form the activities FormWidget should be
* injected into, from the model's $trackableInjectActivitiesFormWidget property
* (falling back to the luketowers.easyaudit.autoInjectActivitiesFormWidget config
* setting). Accepted values:
*
* false Don't inject the widget
* true Inject it into whichever tab section already holds the most fields
* 'fields' Inject it into the form's outside fields
* 'tabs' Inject it into the form's primary tabs
* 'secondaryTabs' Inject it into the form's secondary tabs
*
* @return string|null The form section to inject into, or null to skip injection
*/
protected function resolveActivitiesFormWidgetLocation(\Backend\Widgets\Form $widget): ?string
{
$location = $widget->model->trackableInjectActivitiesFormWidget
?? Config::get('luketowers.easyaudit.autoInjectActivitiesFormWidget', true);
// Default to whichever section is already carrying the bulk of the form.
if ($location === true) {
return count($widget->tabs['fields'] ?? []) > count($widget->secondaryTabs['fields'] ?? [])
? 'tabs'
: 'secondaryTabs';
}
if (in_array($location, static::ACTIVITIES_FORM_WIDGET_LOCATIONS, true)) {
return $location;
}
if (!empty($location)) {
throw new SystemException(sprintf(
'Invalid $trackableInjectActivitiesFormWidget value (%s) on %s. Expected true, false, or one of: %s',
var_export($location, true),
get_class($widget->model),
"'" . implode("', '", static::ACTIVITIES_FORM_WIDGET_LOCATIONS) . "'"
));
}
return null;
}
/**
* 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';
});
}
}