CSPViolationsReport::sourceRecords()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Signify\Reports;
4
5
use SilverStripe\Reports\Report;
6
use Signify\Models\CSPViolation;
7
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
8
use Signify\Forms\GridField\GridFieldDeleteRelationsButton;
9
use SilverStripe\View\Requirements;
10
use SilverStripe\Forms\DropdownField;
11
use SilverStripe\Forms\NumericField;
12
use SilverStripe\Forms\TextField;
13
use SilverStripe\Forms\DatetimeField;
14
use SilverStripe\Forms\ListboxField;
15
16
class CSPViolationsReport extends Report
17
{
18
19
    /**
20
     * The number of reports that can be loaded simultaneously when deleting.
21
     *
22
     * @var integer
23
     */
24
    private static $deletion_batch_size = 50;
0 ignored issues
show
introduced by
The private property $deletion_batch_size is not used, and could be removed.
Loading history...
25
26
    public function title()
27
    {
28
        return _t(__CLASS__ . '.TITLE', 'CSP violations');
29
    }
30
31
    public function description()
32
    {
33
        $desc = _t(
34
            __CLASS__ . '.DESCRIPTION',
35
            'Lists violations caught by the Content Security Policy.'
36
            . ' For more details see <a href="{url}" target="_blank">the MDN documentation</a>.',
37
            ['url' => 'https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP#Violation_report_syntax']
38
        );
39
        return str_replace('</a>', ' <span class="font-icon-external-link"></span></a>', $desc);
40
    }
41
42
    public function sourceRecords($params = [], $sort = null, $limit = null)
43
    {
44
        return CSPViolation::get();
45
    }
46
47
    public function getReportField()
48
    {
49
        Requirements::css('signify-nz/silverstripe-security-headers:client/dist/main.css');
50
        /* @var $gridConfig \SilverStripe\Forms\GridField\GridFieldConfig */
51
        $gridField = parent::getReportField();
52
        $gridConfig = $gridField->getConfig();
53
54
        $dispositions = CSPViolation::get()->columnUnique('Disposition');
55
        $dispositions = array_combine($dispositions, $dispositions);
56
        $directives = CSPViolation::get()->columnUnique('EffectiveDirective');
57
        $directives = array_combine($directives, $directives);
58
59
        $gridConfig->addComponents([
60
            new GridFieldDeleteAction(),
61
            GridFieldDeleteRelationsButton::create('buttons-before-left')
62
            ->setFilterFields([
63
                DatetimeField::create('ReportedTime'),
64
                DropdownField::create('Disposition', 'Disposition', $dispositions),
65
                TextField::create('BlockedURI'),
66
                ListboxField::create('EffectiveDirective', 'EffectiveDirective', $directives),
67
                NumericField::create('Violations', '# Violations'),
68
                TextField::create('Documents.URI', 'Document URIs'),
69
            ])
70
            ->setFilterOptions([
71
                'ReportedTime' => GridFieldDeleteRelationsButton::NUMBER_DATE_FILTER_OPTIONS,
72
                'Disposition' => [
73
                    'ExactMatch',
74
                ],
75
                'BlockedURI' => GridFieldDeleteRelationsButton::STRING_FILTER_OPTIONS,
76
                'EffectiveDirective' => [
77
                    'ExactMatch',
78
                ],
79
                'Violations' => GridFieldDeleteRelationsButton::NUMBER_DATE_FILTER_OPTIONS,
80
                'Documents.URI' => GridFieldDeleteRelationsButton::STRING_FILTER_OPTIONS,
81
            ]),
82
        ]);
83
84
        return $gridField;
85
    }
86
}
87