You are here

force_password_change.install in Force Password Change 6.2

File

force_password_change.install
View source
<?php

/**
 * Implementation of hook_install()
 */
function force_password_change_install() {

  // Install the module schema
  drupal_install_schema('force_password_change');

  // Create a row each user in the {force_password_change_users} table
  $query1 = 'INSERT INTO {force_password_change_users} (uid) VALUES ';
  $query2 = '';
  $arguments = array();
  $uids = db_query('SELECT uid FROM {users} WHERE uid > 0');
  while ($uid = db_fetch_array($uids)) {
    if (strlen($query2)) {
      $query2 .= ', ';
    }
    $query2 .= '(%d)';
    $arguments[] = $uid['uid'];
  }
  db_query($query1 . $query2, $arguments);

  // Create a row for each role in the {force_password_change_roles} table
  $query1 = 'INSERT INTO {force_password_change_roles} (rid) VALUES ';
  $query2 = '';
  $arguments = array();
  $rids = db_query('SELECT rid FROM {role} WHERE rid > 1');
  while ($rid = db_fetch_array($rids)) {
    if (strlen($query2)) {
      $query2 .= ', ';
    }
    $query2 .= '(%d)';
    $arguments[] = $rid['rid'];
  }
  db_query($query1 . $query2, $arguments);

  // Create a new column in the users table to indicate a required forced password change
  // This is added here rather than in the {force_password_change_users} table so that the value is loaded
  // during the bootstrap process
  $ret = array();
  db_add_field($ret, 'users', 'force_password_change', array(
    'type' => 'int',
    'length' => 1,
    'default' => 0,
  ), array(
    'indexes' => array(
      'user_force_password' => array(
        'uid',
        'force_password_change',
      ),
    ),
  ));

  // Set a variable indicating the module installation date. This is used in hook_user() to compare the users signup date with the module installation date
  // to see if they were required to change their password upon first time login. Users who signed up before this variable was set will of course not have
  // bee required to change their password upon first time login.
  variable_set('force_password_change_installation_date', time());
  return $ret;
}

/**
 * Implementation of hook_uninstall()
 */
function force_password_change_uninstall() {
  variable_del('force_password_change_installation_date');
  $ret = array();
  db_drop_index($ret, 'users', 'user_force_password');
  db_drop_field($ret, 'users', 'force_password_change');
  drupal_uninstall_schema('force_password_change');
  return $ret;
}

/**
 * Implementation of hook_schema()
 */
function force_password_change_schema() {
  $schema['force_password_change_users'] = array(
    'description' => t('Holds password change data for users'),
    'fields' => array(
      'uid' => array(
        'description' => t('The UID from the {users} table'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'last_password_change' => array(
        'description' => t('A UNIX timestamp referring to the date that the user last changed their password'),
        'type' => 'int',
        'unsigned' => TRUE,
        'length' => 10,
        'default' => NULL,
      ),
      'last_force' => array(
        'description' => t('A UNIX timestamp referring to the last date on which the user was forced to change their password'),
        'type' => 'int',
        'unsigned' => TRUE,
        'length' => 10,
        'default' => NULL,
      ),
    ),
    'primary key' => array(
      'uid',
    ),
  );
  $schema['force_password_change_roles'] = array(
    'description' => t('Holds the time of the last forced password change by role'),
    'fields' => array(
      'rid' => array(
        'description' => t('The RID from the {role} table'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'last_force' => array(
        'description' => t('A UNIX timestamp referring to the last date on which users in the role were forced to change their password'),
        'type' => 'int',
        'unsigned' => TRUE,
        'length' => 10,
      ),
    ),
    'primary key' => array(
      'rid',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_schema_alter()
 */
function force_password_change_schema_alter(&$schema) {
  $schema['users']['fields']['force_password_change'] = array(
    'type' => 'int',
    'length' => 1,
    'default' => 0,
  );
  $schema['users']['indexes']['user_force_password'] = array(
    'uid',
    'force_password_change',
  );
}

/**
 * Implementation of hook_update_N()
 */
function force_password_change_update_6200() {

  // Set a variable indicating the module installation date. This is used in hook_user() to compare the users signup date with the module installation date
  // to see if they were required to change their password upon first time login. Users who signed up before this variable was set will of course not have
  // bee required to change their password upon first time login.
  variable_set('force_password_change_installation_date', time());
  $ret = array();
  db_create_table($ret, 'force_password_change_users', array(
    'description' => t('Holds password change data for users'),
    'fields' => array(
      'uid' => array(
        'description' => t('The UID from the {users} table'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'last_password_change' => array(
        'description' => t('A UNIX timestamp referring to the date that the user last changed their password'),
        'type' => 'int',
        'unsigned' => TRUE,
        'length' => 10,
      ),
      'last_force' => array(
        'description' => t('A UNIX timestamp referring to the last date on which the user was forced to change their password'),
        'type' => 'int',
        'unsigned' => TRUE,
        'length' => 10,
      ),
    ),
    'primary_key' => array(
      'uid',
    ),
  ));
  db_create_table($ret, 'force_password_change_roles', array(
    'description' => t('Holds the time of the last forced password change by role'),
    'fields' => array(
      'rid' => array(
        'description' => t('The RID from the {role} table'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'last_force' => array(
        'description' => t('A UNIX timestamp referring to the last date on which users in the role were forced to change their password'),
        'type' => 'int',
        'unsigned' => TRUE,
        'length' => 10,
      ),
    ),
    'primary key' => array(
      'rid',
    ),
  ));
  return $ret;
}

/**
 * Implementation of hook_update_N()
 */
function force_password_change_update_6201() {

  // Create a row each user in the {force_password_change_users} table
  $query1 = 'INSERT INTO {force_password_change_users} (uid) VALUES ';
  $query2 = '';
  $arguments = array();
  $uids = db_query('SELECT uid FROM {users} WHERE uid > 0');
  while ($uid = db_fetch_array($uids)) {
    if (strlen($query2)) {
      $query2 .= ', ';
    }
    $query2 .= '(%d)';
    $arguments[] = $uid['uid'];
  }
  db_query($query1 . $query2, $arguments);

  // Create a row for each role in the {force_password_change_roles} table
  $query1 = 'INSERT INTO {force_password_change_roles} (rid) VALUES ';
  $query2 = '';
  $arguments = array();
  $rids = db_query('SELECT rid FROM {role} WHERE rid > 1');
  while ($rid = db_fetch_array($rids)) {
    if (strlen($query2)) {
      $query2 .= ', ';
    }
    $query2 .= '(%d)';
    $arguments[] = $rid['rid'];
  }
  db_query($query1 . $query2, $arguments);
  return array();
}