You are here

role_expire.install in Role Expire 2.x

Same filename and directory in other branches
  1. 8 role_expire.install
  2. 6 role_expire.install
  3. 7 role_expire.install

Role expire install.

File

role_expire.install
View source
<?php

/**
 * @file
 * Role expire install.
 */
use Drupal\Core\Database\Database;

/**
 * Implements hook_schema().
 */
function role_expire_schema() {
  $schema = array();
  $schema['role_expire'] = array(
    'description' => t('Expiration time for the user roles.'),
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => t('User ID connected with expiration time.'),
      ),
      'rid' => array(
        'type' => 'varchar',
        'not null' => TRUE,
        'length' => 128,
        'description' => 'The role ID assigned to the user.',
      ),
      'expiry_timestamp' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('Role expiration timestamp.'),
      ),
    ),
    'primary key' => array(
      'uid',
      'rid',
    ),
  );

  // Moved to configuration since 8.x-1.5.

  /*$schema['role_expire_length'] = array(
      'description' => t('Length in days to assign each role by default.'),
      'fields' => array(
        'rid' => array(
          'type' => 'varchar',
          'not null' => TRUE,
          'length' => 128,
          'description' => 'The role ID assigned to the user.',
        ),
        'duration' => array(
          'type' => 'text',
          'size' => 'small',
          'not null' => TRUE,
          'description' => t('A strtotime-compatible default duration string.')
        ),
      ),
      'primary key' => array('rid'),
    );*/
  return $schema;
}

/**
 * Update schema for default role_expire_length table. Convert integer days to text "# days".
 */
function role_expire_update_8101() {
  $connection = Database::getConnection();

  // Fetch out all the current durations.
  $result = $connection
    ->query('SELECT rid,duration FROM {role_expire_length}');
  $durations = [];
  while ($row = $result
    ->fetchObject()) {
    $durations[$row->rid] = $row->duration;
  }

  // Convert the original duration column from type int to type text.
  $field = array(
    'type' => 'text',
    'size' => 'small',
    'not null' => TRUE,
    'description' => t('A strtotime-compatible default duration string.'),
  );
  $connection
    ->schema()
    ->changeField('role_expire_length', 'duration', 'duration', $field);

  // Append "days" since that's all it could've been before this revision.
  if (!empty($durations)) {
    foreach ($durations as $rid => $duration) {
      $connection
        ->update('role_expire_length')
        ->fields([
        'duration' => "{$duration} days",
      ])
        ->condition('rid', $rid)
        ->execute();
    }
  }
}

/**
 * Migrate default durations table to configuration.
 */
function role_expire_update_8102() {
  $connection = Database::getConnection();

  // Fetch out all the current durations.
  $result = $connection
    ->query('SELECT rid,duration FROM {role_expire_length}');
  $durations = [];
  while ($row = $result
    ->fetchObject()) {
    $durations[$row->rid] = $row->duration;
  }

  // Save current configuration.
  if (!empty($durations)) {
    \Drupal::configFactory()
      ->getEditable('role_expire.config')
      ->set('role_expire_default_duration_roles', $durations)
      ->save();
  }

  // Drop current database table.
  $connection
    ->schema()
    ->dropTable('role_expire_length');
}

/**
 * Automatically assign new roles to users with administer role expire role to maintain access for old installs.
 */
function role_expire_update_8201(&$sandbox) {
  $roles = \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->loadMultiple();
  foreach ($roles as $r) {
    if ($r
      ->hasPermission('administer role expire')) {
      $r
        ->grantPermission('edit users role expire');
      $r
        ->grantPermission('edit role expire default duration');
      $r
        ->save();
    }
  }
}

Functions

Namesort descending Description
role_expire_schema Implements hook_schema().
role_expire_update_8101 Update schema for default role_expire_length table. Convert integer days to text "# days".
role_expire_update_8102 Migrate default durations table to configuration.
role_expire_update_8201 Automatically assign new roles to users with administer role expire role to maintain access for old installs.