You are here

ldap_user.install in Lightweight Directory Access Protocol (LDAP) 7.2

Install, update and uninstall functions for the LDAP User module.

File

ldap_user/ldap_user.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the LDAP User module.
 */

/**
 * Implements hook_requirements().
 */
function ldap_user_requirements($phase) {
  $requirements = [];
  if ($phase == 'runtime') {

    // Check for all $user->* fields created.
    $missing_fields = ldap_user_update_check_user_fields(TRUE);
    if (count($missing_fields)) {
      $requirements['ldap_user']['title'] = t('User Fields for LDAP User Module Missing');
      $requirements['ldap_user']['severity'] = REQUIREMENT_ERROR;
      $requirements['ldap_user']['value'] = NULL;
      $requirements['ldap_user']['description'] = t('Fields are added to the
        Drupal User entity for LDAP User module functionality.  These fields should
        have been created in LDAP User update 7203. The following userfields are missing:') . theme('item_list', [
        'items' => $missing_fields,
        'type' => 'ul',
        'title' => NULL,
      ]) . t('Rerun update 7203 to correct this; it will not write over destroy existing fields.');
    }
  }

  // Check that ldapauth not installed.
  return $requirements;
}

/**
 * Implements hook_install().
 */
function ldap_user_install() {
  foreach (ldap_user_user_entity_fields() as $field_id => $field_conf) {
    $field_info = field_info_field($field_id);
    if (!$field_info) {
      field_create_field($field_conf['field']);
      field_create_instance($field_conf['instance']);
    }
  }

  // Make ldap user weight slightly less than other ldap modules so it can load
  // user's associated ldap data first and save on ldap queries.
  db_query("UPDATE {system} SET weight = -1 WHERE name = 'ldap_user'");
  _ldap_user_install_update_authmap();
}

/**
 *
 */
function _ldap_user_install_update_authmap() {
  db_update('authmap')
    ->fields([
    'module' => 'ldap_user',
  ])
    ->condition('module', 'ldap_authentication')
    ->execute();
}

/**
 * Implements hook_uninstall().
 */
function ldap_user_uninstall() {
  foreach (ldap_user_user_entity_fields() as $field_id => $field_conf) {
    $instance = field_info_instance($field_conf['instance']['entity_type'], $field_conf['instance']['field_name'], $field_conf['instance']['bundle']);
    if ($instance) {
      field_delete_instance($instance);
    }
    $field_info = field_info_field($field_conf['field']['field_name']);
    if ($field_info) {
      field_delete_field($field_conf['field']['field_name']);
    }
  }
  variable_del('ldap_user_conf');
  variable_del('ldap_user_cron_last_orphan_checked');
  variable_del('ldap_user_cron_last_uid_checked');
}

/**
 * Implements hook_schema().
 */
function ldap_user_schema() {
}

/**
 * See http://api.drupal.org/api/drupal/modules%21field%21field.module/group/field/7.
 */
function ldap_user_user_entity_fields() {
  $fields = [];
  $field_name = 'ldap_user_puid_sid';
  $fields[$field_name]['field'] = [
    'field_name' => $field_name,
    'type' => 'text',
    'entity_types' => [
      'user',
    ],
    'cardinality' => 1,
    'translatable' => 0,
    'locked' => 1,
    'module' => 'text',
    'no_ui' => 1,
    'settings' => [],
  ];
  $fields[$field_name]['instance'] = [
    'field_name' => $field_name,
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'LDAP Server ID that puid was derived from.  NULL if puid is independent of server configuration instance.',
    'description' => 'This field should not be edited.',
    'required' => 0,
    'default_value' => [],
    'settings' => [
      'text_processing' => 0,
    ],
    'widget' => [
      'type' => 'ldap_user_hidden',
      'settings' => [],
      'module' => 'ldap_user',
    ],
    'display' => [
      'default' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'full' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'teaser' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
    ],
  ];
  $field_name = 'ldap_user_puid';
  $fields[$field_name]['field'] = [
    'field_name' => $field_name,
    'type' => 'text',
    'entity_types' => [
      'user',
    ],
    'cardinality' => 1,
    'translatable' => 0,
    'locked' => 1,
    'module' => 'text',
    'no_ui' => 1,
    'settings' => [],
  ];
  $fields[$field_name]['instance'] = [
    'field_name' => $field_name,
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'Value of user\'s permanent unique id.  This should never change for a given ldap identified user.',
    'description' => 'This field should not be edited.',
    'required' => 0,
    'default_value' => [],
    'settings' => [
      'text_processing' => 0,
    ],
    'widget' => [
      'type' => 'ldap_user_hidden',
      'settings' => [],
      'module' => 'ldap_user',
    ],
    'display' => [
      'default' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'full' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'teaser' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
    ],
  ];
  $field_name = 'ldap_user_puid_property';
  $fields[$field_name]['field'] = [
    'field_name' => $field_name,
    'type' => 'text',
    'entity_types' => [
      'user',
    ],
    'cardinality' => 1,
    'translatable' => 0,
    'locked' => 1,
    'module' => 'text',
    'no_ui' => 1,
    'settings' => [],
  ];
  $fields[$field_name]['instance'] = [
    'field_name' => $field_name,
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'Property specified as user\'s puid.',
    'description' => 'This field should not be edited.',
    'required' => 0,
    'default_value' => [],
    'settings' => [
      'text_processing' => 0,
    ],
    'widget' => [
      'type' => 'ldap_user_hidden',
      'settings' => [],
      'module' => 'ldap_user',
    ],
    'display' => [
      'default' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'full' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'teaser' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
    ],
  ];
  $field_name = 'ldap_user_current_dn';
  $fields[$field_name]['field'] = [
    'field_name' => $field_name,
    'type' => 'text',
    'entity_types' => [
      'user',
    ],
    'cardinality' => 1,
    'translatable' => 0,
    'locked' => 1,
    'module' => 'text',
    'no_ui' => 1,
    'settings' => [],
  ];
  $fields[$field_name]['instance'] = [
    'field_name' => $field_name,
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'User LDAP DN',
    'description' => 'May change when user\'s DN changes. This field should not be edited.',
    'required' => 0,
    'default_value' => [],
    'settings' => [
      'text_processing' => 0,
    ],
    'widget' => [
      'type' => 'ldap_user_hidden',
      'settings' => [],
      'module' => 'ldap_user',
    ],
    'display' => [
      'default' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'full' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'teaser' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
    ],
  ];
  $field_name = 'ldap_user_prov_entries';
  $fields[$field_name]['field'] = [
    'field_name' => $field_name,
    'type' => 'text',
    'entity_types' => [
      'user',
    ],
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'translatable' => 0,
    'locked' => 1,
    'module' => 'text',
    'no_ui' => 1,
    'settings' => [],
  ];
  $fields[$field_name]['instance'] = [
    'field_name' => $field_name,
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'LDAP Entries that have been provisioned from this Drupal user.',
    'description' => 'This field should not be edited.',
    'required' => 0,
    'default_value' => NULL,
    'settings' => [
      'text_processing' => 0,
    ],
    'widget' => [
      'type' => 'ldap_user_hidden',
      'settings' => [],
      'module' => 'ldap_user',
    ],
    'display' => [
      'default' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'full' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'teaser' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
    ],
  ];
  $field_name = 'ldap_user_last_checked';
  $fields[$field_name]['field'] = [
    'field_name' => $field_name,
    'type' => 'number_integer',
    'entity_types' => [
      'user',
    ],
    'cardinality' => 1,
    'translatable' => 0,
    'locked' => 1,
    'module' => 'number',
    'no_ui' => 1,
    'settings' => [],
  ];
  $fields[$field_name]['instance'] = [
    'field_name' => $field_name,
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'Unix timestamp of when Drupal user was compard to ldap entry.  This could be for purposes of synching, deleteing drupal account, etc.',
    'description' => 'This field should not be edited.',
    'required' => 0,
    'default_value' => NULL,
    'settings' => [],
    'widget' => [
      'type' => 'ldap_user_hidden',
      'settings' => [],
      'module' => 'ldap_user',
    ],
    'display' => [
      'default' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'full' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'teaser' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
    ],
  ];
  $field_name = 'ldap_user_ldap_exclude';
  $fields[$field_name]['field'] = [
    'field_name' => $field_name,
    'type' => 'number_integer',
    'entity_types' => [
      'user',
    ],
    'cardinality' => 1,
    'translatable' => 0,
    'locked' => 1,
    'module' => 'number',
    'no_ui' => 1,
    'settings' => [],
  ];
  $fields[$field_name]['instance'] = [
    'field_name' => $field_name,
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'Whether to exclude the user from LDAP functionality',
    'description' => 'This field should not be edited.',
    'required' => 0,
    'default_value' => 0,
    'settings' => [],
    'widget' => [
      'type' => 'ldap_user_hidden',
      'settings' => [],
      'module' => 'ldap_user',
    ],
    'display' => [
      'default' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'full' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
      'teaser' => [
        'label' => 'hidden',
        'type' => 'hidden',
      ],
    ],
  ];
  return $fields;
}

/**
 * Update authmap table, changing ldap_authentication to ldap_user, disable ldap_profile.
 */
function ldap_user_update_7201() {
  if (module_exists('ldap_profile')) {
    module_disable([
      'ldap_profile',
      TRUE,
    ]);
  }
  _ldap_user_install_update_authmap();
}

/**
 * Attach ldap_user_last_checked field to user entity to track reconciliation of ldap and drupal accounts.
 */
function ldap_user_update_7202() {
  return t('ldap_user_last_checked field added to user entity.');
}

/**
 * Make sure all user entity fields are created.
 */
function ldap_user_update_7203() {
  if (module_exists('ldap_groups')) {
    module_disable([
      'ldap_groups',
      TRUE,
    ]);
  }
  $fields_added = ldap_user_update_check_user_fields();
  if (count($fields_added)) {
    $fields = join(', ', $fields_added);
    return t('%fields added to user entity.', [
      '%fields' => $fields,
    ]);
  }
  else {
    return t('no fields needed to be added to user entity.');
  }
}

/**
 * Make sure all user entity field instances are created.
 */
function ldap_user_update_7204() {
  $fields_added = ldap_user_update_check_user_fields();
  if (count($fields_added)) {
    $fields = join(', ', $fields_added);
    return t('%fields added to user entity.', [
      '%fields' => $fields,
    ]);
  }
  else {
    return t('no fields needed to be added to user entity.');
  }
}

/**
 * Make sure all user entity field instances are created.
 */
function ldap_user_update_7205() {
  return ldap_user_update_7204();
}

/**
 *
 */
function ldap_user_update_check_user_fields($query = FALSE) {
  if (!$query) {
    module_enable([
      'number',
    ]);
  }
  $fields_added = [];
  foreach (ldap_user_user_entity_fields() as $field_id => $field_conf) {
    $field_info = field_info_field($field_id);
    if (!$field_info) {
      $fields_added[] = $field_id;
      if (!$query) {
        field_create_field($field_conf['field']);
      }
    }
    $field_instance_info = field_info_instance('user', $field_id, 'user');
    if (!$field_instance_info) {
      $fields_added[] = $field_id . " instance";
      if (!$query) {
        field_create_instance($field_conf['instance']);
      }
    }
  }
  return $fields_added;
}

/**
 * Set an appropriate default value for accountsWithSameEmail.
 */
function ldap_user_update_7206() {
  $message = NULL;
  $ldap_user_conf = ldap_user_conf('admin', TRUE);

  // If the configuration variable is not set, set a reasonable default and
  // advise the administrator.
  if (module_exists('sharedemail')) {
    $message = t('The module "sharedemail" was detected. LDAP User configuration has been set so that user accounts can be provisioned regardless of whether an existing user account has the same email address.');
    $ldap_user_conf->accountsWithSameEmail = LDAP_USER_ACCOUNTS_WITH_SAME_EMAIL_ENABLED;
  }
  else {

    // The "sharedemail" module is not enabled; do not enable this setting.
    // Do not mention anything in the hook_update_N output.
    $ldap_user_conf->accountsWithSameEmail = LDAP_USER_ACCOUNTS_WITH_SAME_EMAIL_DISABLED;
  }
  $ldap_user_conf
    ->save();
  return $message;
}

Functions

Namesort descending Description
ldap_user_install Implements hook_install().
ldap_user_requirements Implements hook_requirements().
ldap_user_schema Implements hook_schema().
ldap_user_uninstall Implements hook_uninstall().
ldap_user_update_7201 Update authmap table, changing ldap_authentication to ldap_user, disable ldap_profile.
ldap_user_update_7202 Attach ldap_user_last_checked field to user entity to track reconciliation of ldap and drupal accounts.
ldap_user_update_7203 Make sure all user entity fields are created.
ldap_user_update_7204 Make sure all user entity field instances are created.
ldap_user_update_7205 Make sure all user entity field instances are created.
ldap_user_update_7206 Set an appropriate default value for accountsWithSameEmail.
ldap_user_update_check_user_fields
ldap_user_user_entity_fields See http://api.drupal.org/api/drupal/modules%21field%21field.module/group/fi....
_ldap_user_install_update_authmap