You are here

class AutoLoginUrlGeneral in Auto Login URL 8

Same name and namespace in other branches
  1. 2.x src/AutoLoginUrlGeneral.php \Drupal\auto_login_url\AutoLoginUrlGeneral

Class AutoLoginUrlGeneral.

@package Drupal\auto_login_url

Hierarchy

Expanded class hierarchy of AutoLoginUrlGeneral

1 string reference to 'AutoLoginUrlGeneral'
auto_login_url.services.yml in ./auto_login_url.services.yml
auto_login_url.services.yml
1 service uses AutoLoginUrlGeneral
auto_login_url.general in ./auto_login_url.services.yml
Drupal\auto_login_url\AutoLoginUrlGeneral

File

src/AutoLoginUrlGeneral.php, line 14

Namespace

Drupal\auto_login_url
View source
class AutoLoginUrlGeneral {

  /**
   * Constructor.
   */
  public function __construct() {
  }

  /**
   * Check if this IP is blocked by flood.
   *
   * @return bool
   *   TRUE if it is blocked.
   */
  function checkFlood() {

    // Maybe use DI in the future.
    $flood_config = \Drupal::config('user.flood');
    $flood = \Drupal::flood();
    if (!$flood
      ->isAllowed('user.failed_login_ip', $flood_config
      ->get('ip_limit'), $flood_config
      ->get('ip_window'))) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * Register flood event for this IP.
   *
   * @param string $hash
   *   Code that passes through URL.
   */
  function registerFlood($hash) {
    $flood_config = \Drupal::config('user.flood');
    $flood = \Drupal::flood();

    // Register flood event.
    $flood
      ->register('user.failed_login_ip', $flood_config
      ->get('ip_window'));

    // Log error.
    \Drupal::logger('auto_login_url')
      ->error('Failed Auto Login URL from ip: @ip and hash: @hash', [
      '@ip' => \Drupal::request()
        ->getClientIp(),
      '@hash' => $hash,
    ]);
  }

  /**
   * Get secret key for ALU or create now.
   */
  function getSecret() {
    $config = \Drupal::config('auto_login_url.settings');

    // Check if it exists.
    $secret = $config
      ->get('secret');

    // Create if it does not exist.
    if ($secret == '') {
      $random_generator = new Random();
      $secret = $random_generator
        ->name(64);
      \Drupal::configFactory()
        ->getEditable('auto_login_url.settings')
        ->set('secret', $secret)
        ->save();
    }
    return $secret;
  }

  /**
   * Get user password hash.
   *
   * @param integer $uid
   *   User id.
   *
   * @return string
   *   Hashed password.
   */
  function getUserHash($uid) {
    $query = \Drupal::database()
      ->select('users_field_data', 'u');
    $query
      ->addField('u', 'pass');
    $query
      ->condition('u.uid', $uid);
    $query
      ->range(0, 1);
    return $query
      ->execute()
      ->fetchField();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AutoLoginUrlGeneral::checkFlood function Check if this IP is blocked by flood.
AutoLoginUrlGeneral::getSecret function Get secret key for ALU or create now.
AutoLoginUrlGeneral::getUserHash function Get user password hash.
AutoLoginUrlGeneral::registerFlood function Register flood event for this IP.
AutoLoginUrlGeneral::__construct public function Constructor.