<?php

/**
 * Implements hook_install().
 */
function password_policy_history_install() {
  //add current user passwords
  $users = entity_load_multiple('user');
  $connection = \Drupal\Core\Database\Database::getConnection();

  foreach ($users as $user) {
    $hashed_pass = $user->getPassword();
    if ($hashed_pass) {
      $values = array(
        $user->id(),
        $hashed_pass,
        time()
      );
      $connection->insert('password_policy_history')
        ->fields(array('uid', 'pass_hash', 'timestamp'), $values)
        ->execute();
    }
  }
}

/**
 * Implements hook_schema().
 */
function password_policy_history_schema() {
  $schema['password_policy_history'] = array(
    'description' => 'Stores book outline information. Uniquely defines the location of each node in the book outline',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique history ID.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid of the user who changed their password.',
      ),
      'pass_hash' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Password hash for historical item.',
      ),
      'timestamp' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Unix timestamp of when password change occurred.',
      ),
    ),
    'primary key' => array('id'),
  );
  return $schema;
}