You are here

function bakery_uncrumble in Bakery Single Sign-On System 7.2

Same name and namespace in other branches
  1. 6.2 bakery.module \bakery_uncrumble()
  2. 6 bakery.module \bakery_uncrumble()

Form to let users repair minor problems themselves.

1 string reference to 'bakery_uncrumble'
bakery_menu in ./bakery.module
Implements hook_menu().

File

./bakery.module, line 1808
Module file for the Bakery.

Code

function bakery_uncrumble($form, &$form_state) {
  $site_name = variable_get('site_name', 'Drupal');
  $cookie = _bakery_validate_cookie();

  // Analyze.
  $query = db_select('users', 'u')
    ->fields('u', array(
    'uid',
    'name',
    'mail',
  ))
    ->condition('u.uid', 0, '!=')
    ->condition('u.mail', '', '!=')
    ->where("LOWER(u.mail) = LOWER(:mail)", array(
    ':mail' => $cookie['mail'],
  ));
  $result = $query
    ->execute();
  $samemail = $result
    ->fetchObject();
  $query = db_select('users', 'u')
    ->fields('u', array(
    'uid',
    'name',
    'mail',
  ))
    ->condition('u.uid', 0, '!=')
    ->where("LOWER(u.name) = LOWER(:name)", array(
    ':name' => $cookie['name'],
  ));
  $result = $query
    ->execute();
  $samename = $result
    ->fetchObject();
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#value' => $cookie['name'],
    '#disabled' => TRUE,
    '#required' => TRUE,
  );
  $form['mail'] = array(
    '#type' => 'item',
    '#title' => t('Email address'),
    '#value' => $cookie['mail'],
    '#required' => TRUE,
  );
  $form['pass'] = array(
    '#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('Enter the password that accompanies your username.'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Repair account'),
    '#weight' => 2,
  );
  $help = '';
  $count = db_select('users', 'u')
    ->fields('u', array(
    'uid',
  ))
    ->condition('init', $cookie['init'], '=')
    ->countQuery()
    ->execute()
    ->fetchField();
  if ($count > 1) {
    drupal_set_message(t('Multiple accounts are associated with your master account. This must be fixed manually. <a href="@contact">Please contact the site administrator.</a>', array(
      '%email' => $cookie['mail'],
      '@contact' => variable_get('bakery_master', 'http://drupal.org/') . 'contact',
    )));
    $form['pass']['#disabled'] = TRUE;
    $form['submit']['#disabled'] = TRUE;
  }
  elseif ($samename && $samemail && $samename->uid != $samemail->uid) {
    drupal_set_message(t('Both an account with matching name and an account with matching email address exist, but they are different accounts. This must be fixed manually. <a href="@contact">Please contact the site administrator.</a>', array(
      '%email' => $cookie['mail'],
      '@contact' => variable_get('bakery_master', 'http://drupal.org/') . 'contact',
    )));
    $form['pass']['#disabled'] = TRUE;
    $form['submit']['#disabled'] = TRUE;
  }
  elseif ($samename) {
    $help = t("An account with a matching username was found. Repairing it will reset the email address to match your master account. If this is the correct account, please enter your %site password.", array(
      '%site' => $site_name,
    ));

    // This is a borderline information leak.
    // $form['mail']['#value'] = $samename->mail;
    $form['mail']['#value'] = t('<em>*hidden*</em>');
    $form['mail']['#description'] = t('Will change to %new.', array(
      '%new' => $cookie['mail'],
    ));
  }
  elseif ($samemail) {
    $help = t("An account with a matching email address was found. Repairing it will reset the username to match your master account. If this is the correct account, please enter your %site password.", array(
      '%site' => $site_name,
    ));
    $form['name']['#value'] = $samemail->name;
    $form['name']['#description'] = t('Will change to %new.', array(
      '%new' => $cookie['name'],
    ));
  }
  $form['help'] = array(
    '#weight' => -10,
    '#markup' => $help,
  );
  return $form;
}