You are here

public function PasswordPolicyExpire::cron in Password Policy 7.2

Cron task for expiration plugin.

Pulls all users that have expired passwords, ensures they are active with this policy, and then notifies them of their soon-to-be expired password.

File

plugins/item/expire.inc, line 295

Class

PasswordPolicyExpire
Policy item that handles password expirations.

Code

public function cron() {

  // Do not do anything if expiration is disabled.
  $enabled = $this->config['expire_enabled'];
  if (!$enabled) {
    return;
  }

  // Don't do anything if the policy does not require notification e-mails.
  if (empty($this->config['expire_warning_email_sent'])) {
    return;
  }
  $notice_interval_strings = explode(',', $this->config['expire_warning_email_sent']);
  $expire_interval_string = $this->config['expire_limit'];
  $policy_name = $this->policy->name;
  foreach ($notice_interval_strings as $notice_interval_string) {
    $notice_interval_string = trim($notice_interval_string);

    // See if we should be subtracting from expire interval.
    $from_interval = drupal_substr($notice_interval_string, 0, 1) == '-';
    $notice_interval_string = ltrim($notice_interval_string, '-');

    // Convert notice interval to secs.
    $notice_int = password_policy_parse_interval($notice_interval_string);

    // If we need to subtract from expire do so.
    $expire_int = password_policy_parse_interval($expire_interval_string);
    if ($from_interval) {
      $notice_int = $expire_int - $notice_int;
    }
    $candidates = _password_policy_expire_query_users($notice_int, $policy_name);
    foreach ($candidates as $candidate) {
      $account = user_load($candidate->uid, TRUE);
      if ($this->policy
        ->match($account)) {
        $message = _password_policy_expire_notify($account, $candidate, $candidate->created + $expire_int, $this);
        if ($message['result']) {
          watchdog('password_policy', 'Password expiration warning mailed to %username at %email.', array(
            '%username' => $account->name,
            '%email' => $account->mail,
          ));
          $notice_history = (object) array(
            'hid' => $candidate->hid,
            'name' => $this->policy->name,
            'timeframe' => $notice_int,
            // We use time() and not REQUEST_TIME so the stored sent time
            // will be as close as possible to the time the expiration
            // warning e-mail was sent.
            // @ignore upgrade7x_6
            'sent' => time(),
          );
          drupal_write_record('password_policy_notice_history', $notice_history);
        }
      }
    }
  }
}