You are here

force_password_change.install in Force Password Change 8

File

force_password_change.install
View source
<?php

use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;

/**
 * Implementation of hook_install()
 */
function force_password_change_install() {
  $connection = \Drupal::database();

  // Create a row for each role in the {force_password_change_roles} table
  $query = $connection
    ->insert('force_password_change_roles')
    ->fields([
    'rid',
  ]);
  $roles = Role::loadMultiple();
  unset($roles[RoleInterface::ANONYMOUS_ID]);
  $rids = array_keys($roles);
  foreach ($rids as $rid) {
    $query
      ->values([
      $rid,
    ]);
  }
  $query
    ->execute();

  // 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.
  \Drupal::configFactory()
    ->getEditable('force_password_change.settings')
    ->set('installation_date', REQUEST_TIME)
    ->save();
}

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

  // 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'] = [
    'description' => 'Holds the time of the last forced password change by role',
    'fields' => [
      'rid' => [
        'description' => 'The role ID from table',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'last_force' => [
        'description' => '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' => [
      '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'] = [
    'description' => t('Holds information related to the expiry of passwords by role'),
    'fields' => [
      'rid' => [
        'description' => 'The Role ID',
        'type' => 'varchar',
        'not null' => TRUE,
        'length' => 128,
      ],
      'expiry' => [
        'description' => 'The number of seconds after which a user will be forced to reset their password',
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => 0,
      ],
      'weight' => [
        'description' => 'Allows for priorities to be applied to password expirations',
        'type' => 'int',
        'default' => 0,
      ],
    ],
    'primary key' => [
      'rid',
    ],
  ];
  $schema['force_password_change_uids'] = [
    'description' => 'Stores various lists of UIDs for the Force Password Change module',
    'fields' => [
      'category' => [
        'description' => 'The category to which the UID belongs',
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
      ],
      'uid' => [
        'description' => 'The User ID',
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'category',
      'uid',
    ],
  ];
  return $schema;
}
function force_password_change_update_8001() {
  \Drupal::database()
    ->schema()
    ->dropTable('force_password_change_users');
}
function force_password_change_update_8002() {
  \Drupal::database()
    ->schema()
    ->createTable('force_password_change_uids', [
    'description' => 'Stores various lists of UIDs for the Force Password Change module',
    'fields' => [
      'category' => [
        'description' => 'The category to which the UID belongs',
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
      ],
      'uid' => [
        'description' => 'The User ID',
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'category',
      'uid',
    ],
  ]);
}