You are here

function _domain_integration_login_restrict_validate_user_login in Domain Integration (Drupal 7) 8

Same name and namespace in other branches
  1. 7 modules/domain_integration_login_restrict/domain_integration_login_restrict.module \_domain_integration_login_restrict_validate_user_login()

Validates if the user is assigned to the current domain.

If validation fails the error message will show valid domains for a user by default. To show Drupal's default error message, without 'leaking' domains you can toggle variable 'domain_integration_login_restrict_form_error_hide_domains' to TRUE.

1 string reference to '_domain_integration_login_restrict_validate_user_login'
domain_integration_login_restrict_form_alter in modules/domain_integration_login_restrict/domain_integration_login_restrict.module
Implements hook_form_alter().

File

modules/domain_integration_login_restrict/domain_integration_login_restrict.module, line 34
Domain Integration Login Restrict.

Code

function _domain_integration_login_restrict_validate_user_login($form, &$form_state) {

  // Load user domains and return if it fails.
  $values = $form_state['values'];
  $user = user_load_by_name($values['name']);
  if (!$user || $user->uid == 0) {

    // Validation is handled by Drupal core.
    return;
  }

  // Check if user is admin.
  $admin_role = variable_get('user_admin_role', 0);
  if ($user->uid == 1 || in_array($admin_role, array_keys($user->roles))) {

    // Validation is handled by Drupal core.
    return;
  }

  // Check if user is assigned to current domain.
  $current_domain = domain_get_domain();
  $user_domains = $user->domain_user;
  $user_is_assigned_to_current_domain = in_array($current_domain['domain_id'], $user_domains);

  // Set errors if validation failed.
  if (!$user_is_assigned_to_current_domain) {

    // Drupal's default form error.
    if (variable_get('domain_integration_login_restrict_form_error_hide_domains')) {

      // This is exactly the same error that user.module will show when a login
      // attempt fails.
      form_set_error('name', t('Sorry, unrecognized username or password. <a href="@password">Have you forgotten your password?</a>', array(
        '@password' => url('user/password', array(
          'query' => array(
            'name' => $form_state['values']['name'],
          ),
        )),
      )));
    }
    else {
      $error_message = t('Sorry, you cannot login on this domain. You username is assigned to: ');
      $counter = 0;
      foreach ($user_domains as &$domain) {
        $counter = $counter + 1;
        $domain = domain_load($domain);
        if ($counter > 1) {
          $error_message .= ", ";
        }
        $error_message .= "<a href='" . $domain['path'] . "'>" . $domain['sitename'] . "</a>";
      }
      $error_message .= ".";
      form_set_error('name', $error_message);
    }

    // The watchdog message does contain extra info about the domain.
    watchdog('domain_integration_login_restrict', 'Login attempt failed for %user on domain %domain_sitename.', array(
      '%user' => $form_state['values']['name'],
      '%domain_sitename' => $current_domain['sitename'],
    ));
  }
}