You are here

public function BakeryUncrumbleForm::buildForm in Bakery Single Sign-On System 8.2

Form to let users repair minor problems themselves.

Overrides FormInterface::buildForm

File

src/Forms/BakeryUncrumbleForm.php, line 68

Class

BakeryUncrumbleForm
Contribute form.

Namespace

Drupal\bakery\Forms

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $cookie = $this->kitchen
    ->taste(Kitchen::CHOCOLATE_CHIP);

  // Analyze.
  $query = $this->database
    ->select('users_field_data', 'u')
    ->fields('u', [
    'uid',
    'name',
    'mail',
  ])
    ->condition('u.uid', 0, '!=')
    ->condition('u.mail', '', '!=')
    ->where("LOWER(u.mail) = LOWER(:mail)", [
    ':mail' => $cookie['mail'],
  ]);
  $result = $query
    ->execute();
  $samemail = $result
    ->fetchObject();
  $query = $this->database
    ->select('users_field_data', 'u')
    ->fields('u', [
    'uid',
    'name',
    'mail',
  ])
    ->condition('u.uid', 0, '!=')
    ->where("LOWER(u.name) = LOWER(:name)", [
    ':name' => $cookie['name'],
  ]);
  $result = $query
    ->execute();
  $samename = $result
    ->fetchObject();
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#value' => $cookie['name'],
    '#disabled' => TRUE,
    '#required' => TRUE,
  ];
  $form['mail'] = [
    '#type' => 'item',
    '#title' => t('Email address'),
    '#value' => $cookie['mail'],
    '#required' => TRUE,
  ];
  $form['pass'] = [
    '#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('Enter the password that accompanies your username.'),
    '#required' => TRUE,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Repair account'),
    '#weight' => 2,
  ];
  $help = '';
  $count = $this->database
    ->select('users_field_data', 'u')
    ->fields('u', [
    'uid',
  ])
    ->condition('init', $cookie['init'], '=')
    ->countQuery()
    ->execute()
    ->fetchField();
  if ($count > 1) {
    $this
      ->messenger()
      ->addMessage(t('Multiple accounts are associated with your master account. This must be fixed manually. <a href="@contact">Please contact the site administrator.</a>', [
      '%email' => $cookie['mail'],
      '@contact' => $this
        ->configFactory()
        ->get('bakery.settings')
        ->get('bakery_master') . 'contact',
    ]));
    $form['pass']['#disabled'] = TRUE;
    $form['submit']['#disabled'] = TRUE;
  }
  elseif ($samename && $samemail && $samename->uid != $samemail->uid) {
    $this
      ->messenger()
      ->addMessage(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>', [
      '%email' => $cookie['mail'],
      '@contact' => $this
        ->configFactory()
        ->get('bakery.settings')
        ->get('bakery_master') . 'contact',
    ]));
    $form['pass']['#disabled'] = TRUE;
    $form['submit']['#disabled'] = TRUE;
  }
  elseif ($samename) {
    $site_name = $this
      ->configFactory()
      ->get('system.site')
      ->get('name');
    $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.", [
      '%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.', [
      '%new' => $cookie['mail'],
    ]);
  }
  elseif ($samemail) {
    $site_name = $this
      ->configFactory()
      ->get('system.site')
      ->get('name');
    $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.", [
      '%site' => $site_name,
    ]);
    $form['name']['#value'] = $samemail->name;
    $form['name']['#description'] = t('Will change to %new.', [
      '%new' => $cookie['name'],
    ]);
  }
  $form['help'] = [
    '#weight' => -10,
    '#markup' => $help,
  ];
  return $form;
}