Commit 193a86c2 authored by Stephen Mustgrave's avatar Stephen Mustgrave
Browse files

Issue #1244238: Check that cron has been run recently - in the last 72 hours

parent fd40a829
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ use Drupal\security_review\Checks\FailedLogins;
use Drupal\security_review\Checks\Field;
use Drupal\security_review\Checks\FilePermissions;
use Drupal\security_review\Checks\InputFormats;
use Drupal\security_review\Checks\LastCronRun;
use Drupal\security_review\Checks\PrivateFiles;
use Drupal\security_review\Checks\QueryErrors;
use Drupal\security_review\Checks\TemporaryFiles;
@@ -41,6 +42,7 @@ function security_review_security_review_checks() {
    new TrustedHosts(),
    new UploadExtensions(),
    new ViewsAccess(),
    new LastCronRun(),
  ];
}

+92 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\security_review\Checks;

use Drupal\Core\Link;
use Drupal\security_review\Check;
use Drupal\security_review\CheckResult;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;

/**
 * Checks the last time cron has ran.
 */
class LastCronRun extends Check {

  /**
   * {@inheritdoc}
   */
  public function getNamespace() {
    return 'Security Review';
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return 'Last cron run';
  }

  /**
   * {@inheritdoc}
   */
  public function getMachineTitle() {
    return 'last_cron_run';
  }

  /**
   * {@inheritdoc}
   */
  public function run() {
    $result = CheckResult::SUCCESS;
    $last_run = TRUE;
    $cron_last = \Drupal::state()->get('system.cron_last');
    if ($cron_last <= strtotime('-3 day')) {
      $result = CheckResult::FAIL;
      $last_run = FALSE;
    }

    return $this->createResult($result, ['last_run' => $last_run]);
  }

  /**
   * {@inheritdoc}
   */
  public function help() {
    $paragraphs = [];
    $paragraphs[] = $this->t('A properly configured cron job executes, initiates, or manages a variety of tasks.');
    return [
      '#theme' => 'check_help',
      '#title' => $this->t('Cron has ran in last 3 days.'),
      '#paragraphs' => $paragraphs,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function evaluatePlain(CheckResult $result) {
    if ($result->result() != CheckResult::FAIL) {
      return '';
    }

    return $this->t('Last cron run');
  }

  /**
   * {@inheritdoc}
   */
  public function getMessage($result_const) {
    switch ($result_const) {
      case CheckResult::SUCCESS:
        return $this->t('Cron has ran within the last 3 days.');

      case CheckResult::FAIL:
        return $this->t('Cron has not ran within the last 3 days.');

      default:
        return $this->t('Unexpected result.');
    }
  }

}