signify-nz /
silverstripe-security-headers
| 1 | <?php |
||
| 2 | namespace Signify\Jobs; |
||
| 3 | |||
| 4 | use DateInterval; |
||
| 5 | use DateTime; |
||
| 6 | use Signify\Models\CSPViolation; |
||
| 7 | use Signify\Reports\CSPViolationsReport; |
||
| 8 | use SilverStripe\Core\Config\Config; |
||
| 9 | use SilverStripe\ORM\DB; |
||
| 10 | use SilverStripe\ORM\DataList; |
||
| 11 | use Symbiote\QueuedJobs\Services\AbstractQueuedJob; |
||
| 12 | use Symbiote\QueuedJobs\Services\QueuedJobService; |
||
| 13 | |||
| 14 | class RemoveOldCSPViolationsJob extends AbstractQueuedJob |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Reports that were last changed longer than this period ago can be deleted. |
||
| 18 | * |
||
| 19 | * The string must be in DateInterval duration format. |
||
| 20 | * @see https://www.php.net/manual/en/dateinterval.construct.php |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private static $retention_period = 'P1M'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | |||
| 26 | public function setup() |
||
| 27 | { |
||
| 28 | $retention = Config::inst()->get(self::class, 'retention_period'); |
||
| 29 | $retention = new DateInterval($retention); |
||
| 30 | |||
| 31 | $date = new DateTime(); |
||
| 32 | $date->sub($retention); |
||
| 33 | $this->retentionDate = $date->format(DateTime::ATOM); |
||
|
0 ignored issues
–
show
|
|||
| 34 | |||
| 35 | $this->reportsDeleted = 0; |
||
|
0 ignored issues
–
show
|
|||
| 36 | |||
| 37 | $this->totalSteps = $this->getItemsList()->count(); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function process() |
||
| 41 | { |
||
| 42 | $batchSize = Config::inst()->get(CSPViolationsReport::class, 'deletion_batch_size'); |
||
| 43 | |||
| 44 | $oldReports = $this->getItemsList()->limit($batchSize); |
||
| 45 | |||
| 46 | $delta = 0; |
||
| 47 | |||
| 48 | // Wrapped in a transaction for performance only. |
||
| 49 | try { |
||
| 50 | DB::get_conn()->transactionStart(); |
||
| 51 | |||
| 52 | /** @var CSPViolation $report */ |
||
| 53 | foreach ($oldReports as $report) { |
||
| 54 | # See https://github.com/silverstripe/silverstripe-framework/issues/1903 |
||
| 55 | $report->Documents()->removeAll(); |
||
| 56 | $report->delete(); |
||
| 57 | $delta++; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | finally { |
||
| 61 | DB::get_conn()->transactionEnd(); |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->reportsDeleted += $delta; |
||
| 65 | $this->currentStep += $delta; |
||
| 66 | |||
| 67 | if ($delta < $batchSize) { |
||
| 68 | $this->isComplete = true; |
||
| 69 | print 'Removed ' . number_format($this->reportsDeleted) . ' reports.' . "\n"; |
||
| 70 | |||
| 71 | $deletionJob = new RemoveUnreferencedCSPDocumentJob(); |
||
| 72 | $jobId = singleton(QueuedJobService::class)->queueJob($deletionJob); |
||
| 73 | |||
| 74 | print "Unreferenced CSP Document job queued with ID $jobId\n"; |
||
| 75 | |||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | public function getTitle() |
||
| 80 | { |
||
| 81 | return 'Remove old CSP Violation reports'; |
||
| 82 | } |
||
| 83 | |||
| 84 | private function getItemsList(): DataList |
||
| 85 | { |
||
| 86 | return CSPViolation::get()->filter(['ReportedTime:LessThan' => $this->retentionDate]); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 |