You are here

shib_auth.install in Shibboleth Authentication 6.4

This is the install file of the Shibboleth authentication module for Drupal system

File

shib_auth.install
View source
<?php

/**
 * @file
 * This is the install file of the Shibboleth authentication module for Drupal system
 *
 */

/**
 * Implementation of hook_install().
 *
 * Hook to install module, and enable it as a block
 */
function shib_auth_install() {
  drupal_install_schema('shib_auth');

  // Place block into the 'blocks' table manually
  foreach (list_themes() as $key => $theme) {
    if ($theme->status) {

      // Delete block from database for version changes
      $block_query = db_query("SELECT * FROM {blocks} WHERE module = 'shib_auth'");
      if (!db_fetch_object($block_query)) {
        db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta, cache, region) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, '%s', %d, '%s')", 0, '', 0, '', 'shib_auth', $theme->name, TRUE, 0, 0, BLOCK_NO_CACHE, 'left');
      }
    }
  }
  cache_clear_all(NULL, 'cache_block');
  drupal_set_message(st('Shibboleth authentication module installed successfully.'));
}

/**
 * Implementation of hook_uninstall().
 *
 * Hook to uninstall variables and the block, witch were created and used by the module
 */
function shib_auth_uninstall() {

  // Drop variables.
  $variables = array(
    'shib_auth_link_text',
    'shib_auth_handler_url',
    'shib_auth_handler_protocol',
    'shib_auth_wayf_uri',
    'shib_auth_full_handler_url',
    'shib_auth_full_logout_url',
    'shib_auth_username_variable',
    'shib_auth_email_variable',
    'shib_auth_auto_destroy_session',
    'shib_auth_logout_url',
    'shib_auth_login_url',
    'shib_auth_debug_state',
    'shib_auth_debug_url',
    'shib_auth_enable_custom_mail',
    'shib_auth_define_username',
    'shib_auth_account_linking',
    'shib_auth_is_passive',
    'shib_auth_forceauthn',
    'shib_auth_terms_accept',
    'shib_auth_terms_url',
    'shib_auth_terms_ver',
    'shib_auth_force_https',
    'shib_auth_version',
  );
  foreach ($variables as $variable) {
    variable_del($variable);
  }

  // Remove block from the 'blocks' table manually
  db_query("DELETE FROM {blocks} WHERE module = 'shib_auth'");
  cache_clear_all(NULL, 'cache_block');
  drupal_uninstall_schema('shib_auth');
  drupal_set_message(t('The Shibboleth authentication module was uninstalled successfully.'));
}

/**
 * It's needed because db_table_exists() does not escape sql wildcard character '_'
 */
function _shib_auth_db_table_exists($table) {
  $table = preg_replace('/_/', '\\_', $table);
  return (bool) db_fetch_object(db_query("SHOW TABLES LIKE '{" . $table . "}'"));
}

/**
 * Implementation of hook_update().
 *
 * Hook to update module from older revisions
 * @return an array with the executed SQL commands
 */
function shib_auth_update_6400() {

  // NON-DESTRUCTIVE PART OF THE UPDATE -- LET IT RUN MULTIPLE TIMES
  $ret = array();
  $schema = shib_auth_schema();
  if (!_shib_auth_db_table_exists('shib_auth')) {
    db_create_table($ret, 'shib_auth', $schema['shib_auth']);
  }
  else {
    db_add_field($ret, 'shib_auth', 'sticky', array(
      'type' => 'int',
      'size' => 'tiny',
      'not null' => FALSE,
      'default' => 0,
    ));
  }
  if (!_shib_auth_db_table_exists('shib_authmap')) {
    db_create_table($ret, 'shib_authmap', $schema['shib_authmap']);
  }

  /* Hack to determine if we're already running version 4.x
     (the 'shib_auth_version' was not set in versions before 4.x) */
  if (variable_get('shib_auth_version', NULL)) {

    //ABORT
    return $ret;
  }

  /* shib_auth module version */
  variable_set('shib_auth_version', "4.0");

  //DESTRUCTIVE PART OF THE UPDATE -- this must not be run multiple times
  variable_set('shib_auth_full_logout_url', variable_get('shib_auth_handler_protocol', 'https') . '://' . $_SERVER['HTTP_HOST'] . variable_get('shib_auth_handler_url', '/Shibboleth.sso') . '/Logout');
  variable_set('shib_auth_full_handler_url', variable_get('shib_auth_handler_protocol', 'https') . '://' . $_SERVER['HTTP_HOST'] . variable_get('shib_auth_handler_url', '/Shibboleth.sso') . variable_get('shib_auth_wayf_uri', '/DS'));
  variable_set('shib_auth_enable_custom_mail', variable_get('shib_auth_mail_shib_only', FALSE));
  variable_del('shib_auth_mail_shib_only');
  variable_set('shib_auth_email_variable', variable_get('shib_auth_username_email', 'HTTP_MAIL'));
  variable_del('shib_auth_username_email');
  variable_set('shib_auth_logout_url', variable_get('shib_logout_url', $_SERVER['HTTP_HOST']));
  variable_del('shib_logout_url');
  variable_set('shib_auth_link_text', variable_get('auth_link_text', 'Shibboleth Login'));
  variable_del('auth_link_text');

  //copy the elements of authmap to the database shib_auth module uses, and left idp field empty
  $entries = db_query("SELECT * FROM {authmap} WHERE module='shib_auth'");
  while ($entry = db_fetch_array($entries)) {
    if ($entry['uid'] > 1) {
      $ret[] = update_sql("INSERT INTO {shib_authmap} (uid, targeted_id, created) VALUES  ('" . $entry['uid'] . "',\n                                                                                           '" . $entry['authname'] . "',\n                                                                                           '" . date("Y-m-d H:i:s") . "')");
    }
  }

  //update shib elements to
  $entries = db_query("SELECT * FROM {shib_auth}");
  while ($entry = db_fetch_array($entries)) {
    $old_role = unserialize(urldecode($entry['role']));
    if ($old_role) {
      $new_role = serialize(array_keys($old_role));
      db_query("UPDATE {shib_auth} SET role='%s' WHERE id=%d", $new_role, $entry['id']);
    }
  }
  return $ret;
}

/**
 * Implementation of hook_update().
 *
 * Hook to update module from older revisions
 * @return an array with the executed SQL commands
 */
function shib_auth_update_6401() {

  // This update is a helper to rebuild menu tree, because of the new menu item
  // This is useful for those, who are updating from a previous 4.x release
  menu_rebuild();

  // For some reason, update.php insists to have a returned array. Let it be then.
  $returned = array();
  return $returned;
}
function shib_auth_update_6402() {

  // This update is useful for those, who already linked existing accounts to shibboleth with versions prior to RC4, but link didn't create authmap entry
  $ret = array();

  //copy the elements of shib_authmap to authmap, which isn't there already
  $entries = db_query("SELECT uid, name FROM {users} WHERE (uid) IN\n  (SELECT DISTINCT uid\nFROM {shib_authmap}\nWHERE (uid) NOT IN\n  (SELECT uid FROM {authmap})\n)");
  while ($entry = db_fetch_array($entries)) {
    if ($entry['uid'] > 1) {
      $ret[] = update_sql("INSERT INTO {authmap} (uid, authname, module) VALUES  ('" . $entry['uid'] . "',\n      '" . $entry['name'] . "',\n      'shib_auth')");
    }
  }
  return $ret;
}
function shib_auth_update_6403() {

  // This update is a helper to rebuild menu tree, because of the modified menu item
  menu_rebuild();

  // For some reason, update.php insists to have a returned array. Let it be then.
  $returned = array();
  return $returned;
}

/**
 * Implementation of hook_schema().
 *
 * This is how the schema of the module will look like
 */
function shib_auth_schema() {
  $schema['shib_authmap'] = array(
    'description' => t('Stores shibboleth authentication mapping from persistend IDs to drupal users.'),
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => t("The id of the authentication mapping rule"),
      ),
      'uid' => array(
        'type' => 'int',
        'size' => 'small',
        'default' => -1,
        'description' => t("The user id, which the persistent ID will be mapped to"),
      ),
      'targeted_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
        'not null' => TRUE,
        'description' => t("The persistent ID, which will be mapped to a drupal uid"),
      ),
      'idp' => array(
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
        'description' => t("The IdP, the user got her creditentials"),
      ),
      'consentver' => array(
        'type' => 'varchar',
        'length' => 16,
        'default' => '',
        'description' => t("The version of terms and conditions, which was accepted by the user"),
      ),
      'created' => array(
        'type' => 'datetime',
        'description' => t("Date and time the mapping was created"),
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'targeted_id' => array(
        'targeted_id',
      ),
    ),
  );
  $schema['shib_auth'] = array(
    'description' => t('Stores shibboleth authentication rules'),
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => t("The id of the Shibboleth role assignment rule"),
      ),
      'field' => array(
        'type' => 'varchar',
        'length' => 128,
        'default' => '',
        'description' => t("The observed server variable."),
      ),
      'regexpression' => array(
        'type' => 'varchar',
        'length' => 128,
        'default' => '',
        'description' => t("The joint regular expression."),
      ),
      'role' => array(
        'type' => 'varchar',
        'length' => 128,
        'default' => '',
        'description' => t("The assigned role."),
      ),
      'sticky' => array(
        'type' => 'int',
        'length' => 1,
        'default' => 0,
        'description' => t("Sticky status"),
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  return $schema;
}

Functions

Namesort descending Description
shib_auth_install Implementation of hook_install().
shib_auth_schema Implementation of hook_schema().
shib_auth_uninstall Implementation of hook_uninstall().
shib_auth_update_6400 Implementation of hook_update().
shib_auth_update_6401 Implementation of hook_update().
shib_auth_update_6402
shib_auth_update_6403
_shib_auth_db_table_exists It's needed because db_table_exists() does not escape sql wildcard character '_'