You are here

password_policy.install in Password Policy 7.2

Install functions for Password Policy module.

File

password_policy.install
View source
<?php

/**
 * @file
 * Install functions for Password Policy module.
 */

/**
 * Implements hook_schema().
 */
function password_policy_schema() {
  $schema['password_policy'] = array(
    'description' => 'Table storing Password Policies.',
    'export' => array(
      'key' => 'name',
      'key name' => 'Name',
      'primary key' => 'name',
      // Exports will be as $myobj.
      'identifier' => 'password_policy',
      // Function hook name.
      'default hook' => 'default_password_policy',
      'api' => array(
        'owner' => 'password_policy',
        // Base name for api include files.
        'api' => 'default_password_policy',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
        'description' => 'Unique ID for a Password Policy.',
      ),
      'config' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Configuration for Password Policy',
        'size' => 'big',
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );
  $schema['password_policy_history'] = array(
    'description' => 'Stores user\'s old password hashes.',
    'fields' => array(
      'hid' => array(
        'description' => 'Primary Key: Unique history ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'User\'s {users}.uid.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'pass' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'User\'s password (hashed).',
      ),
      'created' => array(
        'description' => 'Timestamp for when the policy was created.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'is_generated' => array(
        'description' => 'Boolean indicating whether the password is generated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'password_policy' => array(
        'table' => 'password_policy',
        'columns' => array(
          'name' => 'name',
        ),
      ),
      'user' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
    ),
    'primary key' => array(
      'hid',
    ),
  );
  $schema['password_policy_notice_history'] = array(
    'description' => 'Recorded when notices are sent',
    'fields' => array(
      'nhid' => array(
        'description' => 'Primary Key: Unique notice history ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'hid' => array(
        'description' => 'Password history id.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Policy name.',
      ),
      'timeframe' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Timeframe instruction when sent',
      ),
      'sent' => array(
        'description' => 'Timestamp for when notice was sent.',
        'type' => 'int',
        'not null' => TRUE,
      ),
    ),
    'foreign keys' => array(
      'password_policy' => array(
        'table' => 'password_policy',
        'columns' => array(
          'name' => 'name',
        ),
      ),
      'password_policy_history' => array(
        'table' => 'password_policy_history',
        'columns' => array(
          'hid' => 'hid',
        ),
      ),
    ),
    'indexes' => array(
      'timeframe' => array(
        'timeframe',
      ),
    ),
    'primary key' => array(
      'nhid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function password_policy_install() {
  variable_set('password_policy_install_time', REQUEST_TIME);
}

/**
 * Implements hook_uninstall().
 */
function password_policy_uninstall() {

  // Remove any values the module added to the {variables} table.
  variable_del('password_policy_install_time');
}

/**
 * Set a flag to denote the time that the new force plugin was installed.
 */
function password_policy_update_7200() {
  variable_set('password_policy_install_time', REQUEST_TIME);
}

/**
 * Update per correction of username constraint config ID.
 *
 * Remove obsolete 'enabled' config ID.
 */
function password_policy_update_7201() {
  ctools_include('export');
  $infos = ctools_export_crud_load_all('password_policy');
  foreach ($infos as $info) {
    $config = unserialize($info->config);
    unset($config['username']['enabled']);
    $info->config = serialize($config);
    ctools_export_crud_save('password_policy', $info);
  }
}

/**
 * Delete not used item for table "password_policy_history".
 *
 * If upgrading from 1.x, this should have been already done in 1.x and is not
 * repeated here.
 */
function password_policy_update_7202() {
  if (db_field_exists('password_policy_history', 'hid')) {
    $query = db_select('password_policy_history', 'p');
    $query
      ->leftJoin('users', 'u', 'p.uid = u.uid');
    $query
      ->isNull('u.uid');
    $query
      ->fields('p', array(
      'uid',
      'hid',
    ));
    foreach ($query
      ->execute() as $record) {
      db_delete('password_policy_history')
        ->condition('uid', $record->uid)
        ->condition('hid', $record->hid)
        ->execute();
    }
  }
}

/**
 * Update history schema from 1.x.
 */
function password_policy_update_7203() {
  if (db_field_exists('password_policy_history', 'pid')) {

    // Create temporary index to allow renaming primary key.
    db_add_index('password_policy_history', 'temp', array(
      'pid',
    ));
    db_drop_primary_key('password_policy_history');
    db_change_field('password_policy_history', 'pid', 'hid', array(
      'description' => 'Primary Key: Unique history ID.',
      'type' => 'serial',
      'unsigned' => TRUE,
      'not null' => TRUE,
    ), array(
      'primary key' => array(
        'hid',
      ),
    ));
    db_drop_index('password_policy_history', 'temp');
  }
  db_change_field('password_policy_history', 'pass', 'pass', array(
    'description' => 'User\'s password (hashed).',
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
  ), array());
}

/**
 * Convert any D6 password hashes in history to D7 format.
 *
 * This may be a lengthy process, and is performed batch-wise.
 *
 * Code mostly copied from user_update_7000(), which updates user password
 * hashes upon D6 -> D7 migration.
 *
 * @see user_update_7000()
 */
function password_policy_update_7204(&$sandbox) {

  // Ignore Coder warning to minimize differences versus user_update_7000().
  // @ignore sniffer_commenting_inlinecomment_spacingbefore:function
  // @codingStandardsIgnoreStart
  $sandbox['#finished'] = 0;

  // Multi-part update.
  if (!isset($sandbox['password_from'])) {
    $sandbox['password_from'] = 0;
    $sandbox['password_count'] = db_query('SELECT COUNT(hid) FROM {password_policy_history}')
      ->fetchField();
  }
  else {
    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

    //  Hash again all current hashed passwords.
    $has_rows = FALSE;

    // Update this many per page load.
    $count = 1000;
    $result = db_query_range('SELECT hid, pass FROM {password_policy_history} ORDER BY hid', $sandbox['password_from'], $count);
    foreach ($result as $row) {
      $has_rows = TRUE;

      // If the $row->pass value is not a MD5 hash (a 32 character
      // hexadecimal string) then skip it.
      if (!preg_match('/^[0-9a-f]{32}$/', $row->pass)) {
        continue;
      }
      $new_hash = user_hash_password($row->pass);
      if ($new_hash) {

        // Indicate an updated password.
        $new_hash = 'U' . $new_hash;
        db_update('password_policy_history')
          ->fields(array(
          'pass' => $new_hash,
        ))
          ->condition('hid', $row->hid)
          ->execute();
      }
    }
    $sandbox['#finished'] = $sandbox['password_count'] == 0 ? 1 : $sandbox['password_from'] / $sandbox['password_count'];
    $sandbox['password_from'] += $count;
    if (!$has_rows) {
      $sandbox['#finished'] = 1;
      return t('User passwords in Password Policy history rehashed to improve security');
    }
  }

  // @codingStandardsIgnoreEnd
}

/**
 * Set value for new checkbox that enables/disables password expiration.
 */
function password_policy_update_7205() {
  ctools_include('export');
  $infos = ctools_export_crud_load_all('password_policy');
  foreach ($infos as $info) {
    $config = unserialize($info->config);
    if ($config['expire']['expire_limit']) {
      $config['expire']['expire_enabled'] = TRUE;
    }
    $info->config = serialize($config);
    ctools_export_crud_save('password_policy', $info);
  }
}

/**
 * Store password_policy config as big text.
 */
function password_policy_update_7206(&$sandbox) {
  db_change_field('password_policy', 'config', 'config', array(
    'type' => 'text',
    'not null' => FALSE,
    'description' => 'Configuration for Password Policy',
    'size' => 'big',
  ));
}

/**
 * Rename "symbol count" to "special characters count".
 */
function password_policy_update_7207() {
  ctools_include('export');
  $infos = ctools_export_crud_load_all('password_policy');
  foreach ($infos as $info) {
    $config = unserialize($info->config);
    if (isset($config['symbol_count'])) {
      $symbol_count = $config['symbol_count']['symbol_count'];
      $symbol_count_symbols = $config['symbol_count']['symbol_count_symbols'];
      unset($config['symbol_count']);
      $config['special_count'] = array(
        'special_count' => $symbol_count,
        'special_count_chars' => $symbol_count_symbols,
      );
      $info->config = serialize($config);
      ctools_export_crud_save('password_policy', $info);
    }
  }
}

/**
 * Store indicator of generated password in password_policy_history.
 */
function password_policy_update_7208() {
  db_add_field('password_policy_history', 'is_generated', array(
    'description' => 'Boolean indicating whether the password is generated.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
}

Functions

Namesort descending Description
password_policy_install Implements hook_install().
password_policy_schema Implements hook_schema().
password_policy_uninstall Implements hook_uninstall().
password_policy_update_7200 Set a flag to denote the time that the new force plugin was installed.
password_policy_update_7201 Update per correction of username constraint config ID.
password_policy_update_7202 Delete not used item for table "password_policy_history".
password_policy_update_7203 Update history schema from 1.x.
password_policy_update_7204 Convert any D6 password hashes in history to D7 format.
password_policy_update_7205 Set value for new checkbox that enables/disables password expiration.
password_policy_update_7206 Store password_policy config as big text.
password_policy_update_7207 Rename "symbol count" to "special characters count".
password_policy_update_7208 Store indicator of generated password in password_policy_history.