1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\SecurityChecker\Extensions; |
4
|
|
|
|
5
|
|
|
use BringYourOwnIdeas\SecurityChecker\Models\SecurityAlert; |
6
|
|
|
use SilverStripe\View\ArrayData; |
7
|
|
|
use SilverStripe\ORM\DataExtension; |
8
|
|
|
|
9
|
|
|
class PackageSecurityExtension extends DataExtension |
10
|
|
|
{ |
11
|
|
|
private static $has_many = [ |
|
|
|
|
12
|
|
|
'SecurityAlerts' => SecurityAlert::class |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
private static $summary_fields = [ |
|
|
|
|
16
|
|
|
'listSecurityAlertIdentifiers' => 'Security alerts', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Simply returns a comma separated list of active SecurityAlert Identifiers for this record. |
21
|
|
|
* Used in CSV exports as a type of brief indication (as opposed to full info) |
22
|
|
|
*/ |
23
|
|
|
public function listSecurityAlertIdentifiers() |
24
|
|
|
{ |
25
|
|
|
$alerts = $this->owner->SecurityAlerts()->Column('Identifier'); |
26
|
|
|
return $alerts ? implode(', ', $alerts) : null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Renders the SecurityAlerts relationship |
31
|
|
|
* intended for use in the on screen version of the SiteSummary Report. |
32
|
|
|
*/ |
33
|
|
|
public function listAlerts() |
34
|
|
|
{ |
35
|
|
|
$templates = ['PackageSecurityAlerts']; |
36
|
|
|
$this->owner->extend('updateListAlerts', $templates); |
37
|
|
|
return $this->owner->renderWith($templates); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* updates the badges that render as part of the screen targeted |
42
|
|
|
* summary for this Package |
43
|
|
|
* |
44
|
|
|
* @param ArrayList $badges |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public function updateBadges(&$badges) |
47
|
|
|
{ |
48
|
|
|
if ($this->owner->SecurityAlerts()->exists()) { |
49
|
|
|
$badges->push(ArrayData::create([ |
50
|
|
|
'Title' => _t(__CLASS__ . '.BADGE_SECURITY', 'RISK: Security'), |
51
|
|
|
'Type' => 'warning security-alerts__toggler', |
52
|
|
|
])); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Appends our own summary info to that of the default output |
58
|
|
|
* of the Package getSummary method. |
59
|
|
|
* |
60
|
|
|
* @param HTMLText $summary |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function updateSummary(&$summary) |
63
|
|
|
{ |
64
|
|
|
$summary->setValue($summary . $this->listAlerts()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|