You are here

force_password_change.install in Force Password Change 7

File

force_password_change.install
View source
<?php

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

  // Create a row each user in the {force_password_change_users} table
  $query = db_insert('force_password_change_users')
    ->fields(array(
    'uid',
  ));
  $uids = db_query('SELECT uid FROM {users} WHERE uid > 0');
  foreach ($uids as $uid) {
    $query
      ->values(array(
      $uid->uid,
    ));
  }
  $query
    ->execute();

  // Create a row for each role in the {force_password_change_roles} table
  $query2 = db_insert('force_password_change_roles')
    ->fields(array(
    'rid',
  ));
  $rids = db_query('SELECT rid FROM {role} WHERE rid > 1');
  foreach ($rids as $rid) {
    $query2
      ->values(array(
      $rid->rid,
    ));
  }
  $query2
    ->execute();

  // Create a new column in the {users} table to indicate a required forced password change
  // This is added to the {users} table ather than the {force_password_change_users} table
  //  so that the value is loaded during the bootstrap process
  db_add_field('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
  // been required to change their password upon first time login.
  variable_set('force_password_change_installation_date', time());
}

/**
 * Implementation of hook_uninstall()
 */
function force_password_change_uninstall() {

  // Remove any values the module added to the {variables} table
  variable_del('force_password_change_installation_date');
  variable_del('force_password_change_change_password_url');
  variable_del('force_password_change_login_or_init');
  variable_del('force_password_change_pending_login_users');
  variable_del('force_password_change_expire_password');
  variable_del('force_password_change_first_login_change');
  variable_del('force_password_change_first_time_uids');

  // Remove the column that was added to the {users} table on installation
  db_drop_index('users', 'user_force_password');
  db_drop_field('users', 'force_password_change');
}

/**
 * Implementation of hook_schema()
 */
function force_password_change_schema() {

  // This table contains one row for each user, and holds stats
  // regarding their password changes and when they were last
  // forced to change their password.
  $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',
    ),
  );

  // This table contains one row for each role, and holds stats
  // regarding the last time the members of the role were forced
  // to change their password
  $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',
    ),
  );

  // This table contains data regarding the time period after which
  // passwords should expire for members in that role. For example,
  // autheticated users may be forced to change their password once
  // a month.
  $schema['force_password_change_expiry'] = array(
    'description' => t('Holds information related to the expiry of passwords by role'),
    'fields' => array(
      'rid' => array(
        'description' => t('The RID from the {role} table'),
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => NULL,
      ),
      'expiry' => array(
        'description' => t('The number of seconds after which a user will be forced to reset their password'),
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'description' => t('Allows for priorities to be applied to password expirations'),
        'type' => 'int',
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'rid',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_schema_alter()
 *
 * This contains the information for the column that is added
 * to the {users} table by this module.
 */
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()
 *
 * This function updates the user from Drupal 6 to Drupal 7. Names of variables in the {variable}
 * table have been namespaced in Drupal 7 so as to prevent conflicts with other modules. This update
 * converts the names of the variables to the Drupal 7 names
 */
function force_password_change_update_7000() {
  $query = db_update('variable')
    ->fields(array(
    'name' => 'force_password_change_change_password_url',
  ))
    ->condition('name', 'change_password_url')
    ->execute();
  $query = db_update('variable')
    ->fields(array(
    'name' => 'force_password_change_expire_password',
  ))
    ->condition('name', 'expire_password')
    ->execute();
  $query = db_update('variable')
    ->fields(array(
    'name' => 'force_password_change_first_login_change',
  ))
    ->condition('name', 'first_time_login_password_change')
    ->execute();
}