1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\HealthCheckProvider\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\CheckboxField; |
6
|
|
|
use SilverStripe\Forms\ReadonlyField; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\Security\Member; |
9
|
|
|
use SilverStripe\Security\Security; |
10
|
|
|
|
11
|
|
|
class HealthCheckProviderSecurity extends DataObject |
12
|
|
|
{ |
13
|
|
|
####################### |
14
|
|
|
### Names Section |
15
|
|
|
####################### |
16
|
|
|
|
17
|
|
|
private static $singular_name = 'Security Check'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
private static $plural_name = 'Security Checks'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
private static $table_name = 'HealthCheckProviderSecurity'; |
|
|
|
|
22
|
|
|
|
23
|
|
|
####################### |
24
|
|
|
### Model Section |
25
|
|
|
####################### |
26
|
|
|
|
27
|
|
|
private static $db = [ |
|
|
|
|
28
|
|
|
'Secret' => 'Varchar(255)', |
29
|
|
|
'IpAddress' => 'Varchar(64)', |
30
|
|
|
'Allowed' => 'Boolean', |
31
|
|
|
'AllowAllData' => 'Boolean', |
32
|
|
|
'DefinitelyNotOk' => 'Boolean', |
33
|
|
|
'AccessCount' => 'Int', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private static $has_one = [ |
|
|
|
|
37
|
|
|
'Editor' => Member::class, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
####################### |
41
|
|
|
### Further DB Field Details |
42
|
|
|
####################### |
43
|
|
|
|
44
|
|
|
private static $default_sort = [ |
|
|
|
|
45
|
|
|
'Allowed' => 'DESC', |
46
|
|
|
'Created' => 'DESC', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
####################### |
50
|
|
|
### Field Names and Presentation Section |
51
|
|
|
####################### |
52
|
|
|
|
53
|
|
|
private static $field_labels = [ |
|
|
|
|
54
|
|
|
'Secret' => 'Api Key Provided by Retriever', |
55
|
|
|
'IpAddress' => 'IP Address of Retriever', |
56
|
|
|
'Allowed' => 'Allow this key from this IP address?', |
57
|
|
|
'Editor' => 'Report Editor', |
58
|
|
|
'EditorID' => 'Report Editor', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
private static $summary_fields = [ |
|
|
|
|
62
|
|
|
'Created' => 'First Access', |
63
|
|
|
'Allowed.Nice' => 'Allow', |
64
|
|
|
'Editor.Title' => 'Editor', |
65
|
|
|
'Secret' => 'Api Key Provided', |
66
|
|
|
'IpAddress' => 'IP', |
67
|
|
|
'AccessCount' => 'Access Count', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
####################### |
71
|
|
|
### Casting Section |
72
|
|
|
####################### |
73
|
|
|
|
74
|
|
|
private static $casting = [ |
|
|
|
|
75
|
|
|
'Title' => 'Varchar', |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
public static function check(string $key, string $ip): bool |
79
|
|
|
{ |
80
|
|
|
$obj = self::get_object_from_filter($key, $ip); |
81
|
|
|
|
82
|
|
|
return (bool) $obj->Allowed; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function get_editor_id(string $key, string $ip): int |
86
|
|
|
{ |
87
|
|
|
$obj = self::get_object_from_filter($key, $ip); |
88
|
|
|
|
89
|
|
|
return (int) $obj->EditorID; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* casted variable |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getTitle(): string |
97
|
|
|
{ |
98
|
|
|
return 'Retrieval attempt from "' . $this->IpAddress . '" using "' . $this->Secret . '" as key'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
####################### |
102
|
|
|
### can Section |
103
|
|
|
####################### |
104
|
|
|
|
105
|
|
|
public function canCreate($member = null, $context = []) |
106
|
|
|
{ |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function canDelete($member = null) |
111
|
|
|
{ |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function canEdit($member = null) |
116
|
|
|
{ |
117
|
|
|
return $this->DefinitelyNotOk ? false : parent::canEdit($member); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
####################### |
121
|
|
|
### write Section |
122
|
|
|
####################### |
123
|
|
|
|
124
|
|
|
public function onBeforeWrite() |
125
|
|
|
{ |
126
|
|
|
parent::onBeforeWrite(); |
127
|
|
|
if (! $this->EditorID) { |
128
|
|
|
$user = Security::getCurrentUser(); |
129
|
|
|
if ($user) { |
|
|
|
|
130
|
|
|
$this->EditorID = Security::getCurrentUser()->ID; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
if (! $this->Secret) { |
134
|
|
|
$this->Secret = 'Careful: no key set - ' . mt_rand(0, 9999999999999999); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
if (! $this->IpAddress) { |
137
|
|
|
$this->IpAddress = 'Careful: no IP Set'; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
if ($this->DefinitelyNotOk) { |
|
|
|
|
140
|
|
|
$this->Allowed = false; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function onAfterWrite() |
145
|
|
|
{ |
146
|
|
|
parent::onAfterWrite(); |
147
|
|
|
if($this->AllowAllData) { |
|
|
|
|
148
|
|
|
$items = HealthCheckItemProvider::get()->filter(['Include' => false]); |
149
|
|
|
foreach($items as $item) { |
150
|
|
|
$item->Include = true; |
151
|
|
|
$item->write(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
####################### |
157
|
|
|
### CMS Edit Section |
158
|
|
|
####################### |
159
|
|
|
|
160
|
|
|
public function getCMSFields() |
161
|
|
|
{ |
162
|
|
|
$fields = parent::getCMSFields(); |
163
|
|
|
$fields->addFieldsToTab( |
164
|
|
|
'Root.Main', |
165
|
|
|
[ |
166
|
|
|
ReadonlyField::create('Secret', 'Secret Key'), |
167
|
|
|
ReadonlyField::create('IpAddress', 'IP'), |
168
|
|
|
ReadonlyField::create('AccessCount', 'Access Count'), |
169
|
|
|
CheckboxField::create('Allowed', 'Allow this IP with this Key? If unsure, please double-check!') |
170
|
|
|
->setDescription('Make sure that you are OK with both the key and the IP address to ensure security.'), |
171
|
|
|
CheckboxField::create('AllowAllData', 'Check this box to allow access to all for any IPs granted access') |
172
|
|
|
->setDescription('Carefully consider if you are ok with this'), |
173
|
|
|
CheckboxField::create('DefinitelyNotOk', 'Check if you think this is a bad request') |
174
|
|
|
->setDescription('Careful, checking this will stop any future retrievals with this key and IP.'), |
175
|
|
|
] |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
return $fields; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected static function get_object_from_filter(string $key, string $ip): HealthCheckProviderSecurity |
182
|
|
|
{ |
183
|
|
|
$filter = [ |
184
|
|
|
'Secret' => $key, |
185
|
|
|
'IpAddress' => $ip, |
186
|
|
|
]; |
187
|
|
|
|
188
|
|
|
//we make sure we get the last one! Just in case there is more one. |
189
|
|
|
/** @var HealthCheckProviderSecurity|null */ |
190
|
|
|
$obj = HealthCheckProviderSecurity::get()->filter($filter)->last(); |
191
|
|
|
if (! $obj) { |
|
|
|
|
192
|
|
|
$obj = HealthCheckProviderSecurity::create($filter); |
193
|
|
|
} |
194
|
|
|
$obj->AccessCount++; |
195
|
|
|
|
196
|
|
|
$obj->write(); |
197
|
|
|
|
198
|
|
|
return $obj; |
|
|
|
|
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|