You are here

shield.install in Shield 8

Same filename and directory in other branches
  1. 6 shield.install
  2. 7 shield.install

Contains install and update related functions for Shield.

File

shield.install
View source
<?php

/**
 * @file
 * Contains install and update related functions for Shield.
 */

/**
 * Update allow_cli config to boolean value.
 */
function shield_update_8001() {
  $config = \Drupal::configFactory()
    ->getEditable('shield.settings');
  $allow_cli = (bool) $config
    ->get('allow_cli');
  $config
    ->set('allow_cli', $allow_cli);
  $config
    ->save(TRUE);
}

/**
 * Convert user and pass config to credential provider config.
 */
function shield_update_8002() {
  $config = \Drupal::configFactory()
    ->getEditable('shield.settings');
  $config
    ->set('credential_provider', 'shield')
    ->set('credentials.shield.user', $config
    ->get('user'))
    ->set('credentials.shield.pass', $config
    ->get('pass'))
    ->clear('user')
    ->clear('pass')
    ->save(TRUE);
}

/**
 * Enable shield conditionally after shield_enable option was added.
 */
function shield_update_8003() {

  // Note: Get the currently effective value, instead of getEditable(), to make
  // sure not to leave a site unprotected if config overridden in settings.php.
  $config = \Drupal::configFactory()
    ->get('shield.settings');

  // Shield is considered to be enabled if user value is not empty, find
  // the user value based on credential_provider.
  switch ($config
    ->get('credential_provider')) {
    case 'shield':
      $user = $config
        ->get('credentials.shield.user');
      break;
    case 'key':
      $user = $config
        ->get('credentials.key.user');
      break;
    case 'multikey':
      $storage = \Drupal::entityTypeManager()
        ->getStorage('key');
      $user_pass_key = $storage
        ->load($config
        ->get('credentials.multikey.user_pass_key'));
      if ($user_pass_key) {
        $values = $user_pass_key
          ->getKeyValues();
        $user = $values['username'];
      }
      break;
  }
  if (!empty($user)) {
    $config = \Drupal::configFactory()
      ->getEditable('shield.settings');
    $config
      ->set('shield_enable', TRUE)
      ->save();
  }
}

Functions

Namesort descending Description
shield_update_8001 Update allow_cli config to boolean value.
shield_update_8002 Convert user and pass config to credential provider config.
shield_update_8003 Enable shield conditionally after shield_enable option was added.