You are here

simplesamlphp_auth.install in simpleSAMLphp Authentication 8.3

The install file for the simplesamlphp_auth module.

File

simplesamlphp_auth.install
View source
<?php

/**
 * @file
 * The install file for the simplesamlphp_auth module.
 */
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings;

/**
 * Implements hook_install().
 */
function simplesamlphp_auth_install() {
  user_role_revoke_permissions(AccountInterface::AUTHENTICATED_ROLE, [
    'change own password',
  ]);

  // Disable the open registration to the site and store the original setting.
  $user_settings = \Drupal::configFactory()
    ->getEditable('user.settings');
  $config = \Drupal::configFactory()
    ->getEditable('simplesamlphp_auth.settings');
  $config
    ->set('user_register_original', $user_settings
    ->get('register'));
  $user_settings
    ->set('register', 'admin_only');
  $user_settings
    ->save();

  // Inform the user about disabling the open registration.
  \Drupal::messenger()
    ->addMessage(t('The SimpleSAMLphp Authentication module disabled the user registration. You can manually enable it again in the <a href=":user_settings_url">Account settings</a>.', [
    ':user_settings_url' => Url::fromRoute('entity.user.admin_form')
      ->toString(),
  ]), 'warning');
  $config
    ->save();
}

/**
 * Implements hook_uninstall().
 */
function simplesamlphp_auth_uninstall() {

  // Restore the original user registration directive.
  $user_settings = \Drupal::configFactory()
    ->getEditable('user.settings');
  $config = \Drupal::config('simplesamlphp_auth.settings');
  $user_settings
    ->set('register', $config
    ->get('user_register_original'));
  $user_settings
    ->save();
}

/**
 * Implements hook_requirements().
 */
function simplesamlphp_auth_requirements($phase) {
  $requirements = [];
  if ($phase == 'install') {
    simplesamlphp_auth_check_library();
    if (!class_exists('SimpleSAML\\Configuration')) {
      $requirements['simplesamlphp_library'] = [
        'description' => t('SimpleSAMLphp module requires the simplesamlphp library, version 1.18.2 or later. See README file for installation instructions.'),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }
  if ($phase == 'runtime') {
    $config = \Drupal::config('simplesamlphp_auth.settings');
    if (!$config
      ->get('activate')) {
      $requirements['simplesamlphp_auth'] = [
        'severity' => REQUIREMENT_INFO,
        'title' => 'simpleSAMLphp_auth',
        'value' => t('SimpleSAMLphp authentication is NOT activated'),
        'description' => t('It can be activated on the <a href=":config_page">configuration page</a>.', [
          ':config_page' => Url::fromRoute('simplesamlphp_auth.admin_settings')
            ->toString(),
        ]),
      ];
    }
  }
  return $requirements;
}

/**
 * Check if the SimpleSAMLphp library can be found.
 *
 * Fallback for when the library was not found via Composer.
 */
function simplesamlphp_auth_check_library() {
  if ($dir = Settings::get('simplesamlphp_dir')) {
    include_once $dir . '/lib/_autoload.php';
  }
}

/**
 * Rebuild simplesamlphp_auth_event_subscriber service.
 */
function simplesamlphp_auth_update_8001() {
  \Drupal::service('kernel')
    ->invalidateContainer();
}

/**
 * Rebuild router for changed admin form.
 */
function simplesamlphp_auth_update_8002() {
  \Drupal::service('router.builder')
    ->rebuild();
}

/**
 * Provide a default value for the new login_link_show configuration option.
 */
function simplesamlphp_auth_update_8301() {
  \Drupal::configFactory()
    ->getEditable('simplesamlphp_auth.settings')
    ->set('login_link_show', TRUE)
    ->save(TRUE);
}

/**
 * Remove Site default language from simplesamlphp_auth configuration.
 */
function simplesamlphp_auth_update_8302() {
  \Drupal::configFactory()
    ->getEditable('simplesamlphp_auth.settings')
    ->clear('default_langcode')
    ->save(TRUE);
}

Functions

Namesort descending Description
simplesamlphp_auth_check_library Check if the SimpleSAMLphp library can be found.
simplesamlphp_auth_install Implements hook_install().
simplesamlphp_auth_requirements Implements hook_requirements().
simplesamlphp_auth_uninstall Implements hook_uninstall().
simplesamlphp_auth_update_8001 Rebuild simplesamlphp_auth_event_subscriber service.
simplesamlphp_auth_update_8002 Rebuild router for changed admin form.
simplesamlphp_auth_update_8301 Provide a default value for the new login_link_show configuration option.
simplesamlphp_auth_update_8302 Remove Site default language from simplesamlphp_auth configuration.