1
|
|
|
<?php |
2
|
|
|
class IPLoggerSecurityAdminExtension extends LeftAndMainExtension |
|
|
|
|
3
|
|
|
{ |
4
|
|
|
private static $dependencies = array( |
|
|
|
|
5
|
|
|
'loggerService' => '%$IPLoggerService', |
6
|
|
|
'loggerEntry' => '%$IPLoggerEntry', |
7
|
|
|
'banEntry' => '%$IPBanEntry' |
8
|
|
|
); |
9
|
|
|
|
10
|
|
|
public $loggerService; |
11
|
|
|
|
12
|
|
|
public $loggerEntry; |
13
|
|
|
|
14
|
|
|
public $banEntry; |
15
|
|
|
|
16
|
|
|
public function updateEditForm($form) |
17
|
|
|
{ |
18
|
|
|
if (Permission::check('ADMIN')) { |
19
|
|
|
$fields = $form->Fields(); |
20
|
|
|
|
21
|
|
|
$loggerTab = $fields->findOrMakeTab('Root.IPLogs', 'IP Logs'); |
22
|
|
|
|
23
|
|
|
// List ban entries |
24
|
|
|
$banClass = get_class($this->banEntry); |
25
|
|
|
|
26
|
|
|
$banEntries = $banClass::get()->sort('Created DESC'); |
27
|
|
|
|
28
|
|
|
$banGrid = GridField::create('BanGrid', 'Bans', $banEntries); |
29
|
|
|
|
30
|
|
|
$banGrid->setForm($form); |
31
|
|
|
|
32
|
|
|
$banGridConfig = GridFieldConfig_RecordEditor::create(); |
33
|
|
|
$banGridConfig->removeComponentsByType('GridFieldAddNewButton'); |
34
|
|
|
|
35
|
|
|
// Add a new sudo column to show whether or not a ban is active |
36
|
|
|
$dataColumns = $banGridConfig->getComponentByType('GridFieldDataColumns'); |
37
|
|
|
|
38
|
|
|
$displayFields = $dataColumns->getDisplayFields($banGrid); |
39
|
|
|
|
40
|
|
|
$displayFields = array('Active' => '') + $displayFields; |
41
|
|
|
|
42
|
|
|
$dataColumns->setDisplayFields($displayFields); |
43
|
|
|
|
44
|
|
|
$loggerService = $this->loggerService; |
45
|
|
|
$dataColumns->setFieldFormatting( |
46
|
|
|
array( |
47
|
|
|
'Active' => function ($value, $item) use ($loggerService) { |
48
|
|
|
$banSecondsAgo = $item->obj('Created')->TimeDiffIn('seconds'); |
49
|
|
|
$banSeconds = $loggerService->getRule($item->Event)['bantime']; |
50
|
|
|
|
51
|
|
|
$active = $banSeconds === 0 || $banSecondsAgo < $banSeconds; |
52
|
|
|
|
53
|
|
|
switch ($active) { |
54
|
|
|
case true: |
55
|
|
|
$colour = 'rgb(76, 153, 0)'; |
56
|
|
|
$value = 'Active'; |
57
|
|
|
break; |
58
|
|
|
case false: |
59
|
|
|
$colour = 'rgb(255, 0, 0)'; |
60
|
|
|
$value = 'Expired'; |
61
|
|
|
break; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$html = "<strong style='color: {$colour}; |
|
|
|
|
65
|
|
|
font-size: 120%;'>{$value}</strong>"; |
66
|
|
|
|
67
|
|
|
return $html; |
68
|
|
|
} |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$banGrid->setConfig($banGridConfig); |
73
|
|
|
|
74
|
|
|
$loggerTab->push($banGrid); |
75
|
|
|
|
76
|
|
|
// List log entries |
77
|
|
|
$entryClass = get_class($this->loggerEntry); |
78
|
|
|
|
79
|
|
|
$logEntries = $entryClass::get()->sort('Created DESC'); |
80
|
|
|
|
81
|
|
|
$entryGrid = GridField::create('EntryGrid', 'Log Entries', $logEntries); |
82
|
|
|
|
83
|
|
|
$entryGrid->setForm($form); |
84
|
|
|
|
85
|
|
|
$entryGridConfig = GridFieldConfig_RecordEditor::create(); |
86
|
|
|
$entryGridConfig->removeComponentsByType('GridFieldAddNewButton'); |
87
|
|
|
|
88
|
|
|
$entryGrid->setConfig($entryGridConfig); |
89
|
|
|
|
90
|
|
|
$loggerTab->push($entryGrid); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.