View source
<?php
namespace Drupal\bakery\Forms;
use Drupal\bakery\Kitchen;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Password\PasswordInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class BakeryUncrumbleForm extends FormBase implements ContainerInjectionInterface {
private $passwordHasher;
private $database;
private $kitchen;
public function __construct(PasswordInterface $password_hasher, Connection $connection, Kitchen $kitchen) {
$this->passwordHasher = $password_hasher;
$this->database = $connection;
$this->kitchen = $kitchen;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('password'), $container
->get('database'), $container
->get('bakery.kitchen'));
}
public function getFormId() {
return 'bakery_forms_uncrumble_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cookie = $this->kitchen
->taste(Kitchen::CHOCOLATE_CHIP);
$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,
]);
$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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$account = user_load_by_name($form_state
->getValue('name'));
if (!($account && $account
->id()) || $this->passwordHasher
->check($form_state
->getValue('pass'), $account
->getPassword())) {
$this
->logger('bakery')
->notice('Login attempt failed for %user while running uncrumble.', [
'%user' => $form_state
->getValue('name'),
]);
$form_state
->setErrorByName('pass', t('Sorry, unrecognized password. If you have forgotten your %site password, please <a href="@contact">contact the site administrator.</a>', [
'%site' => $this
->configFactory()
->get('system.site')
->get('name'),
'@contact' => $this
->configFactory()
->get('bakery.settings')
->get('bakery_master') . 'contact',
]));
}
else {
$form_state
->setValue('bakery_uncrumble_account', $account);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$account = $form_state
->getValue('bakery_uncrumble_account');
$form_state
->unsetValue('bakery_uncrumble_account');
$cookie = $this->kitchen
->taste(Kitchen::CHOCOLATE_CHIP);
$this->database
->update('users_field_data')
->fields([
'init' => $cookie['init'],
])
->condition('uid', $account
->id(), '=')
->execute();
$logger = $this
->logger('bakery');
$logger
->notice('uncrumble changed init field for uid %uid from %oldinit to %newinit', [
'%oldinit' => $account
->get('init')->value,
'%newinit' => $cookie['init'],
'%uid' => $account
->id(),
]);
$account
->setEmail($cookie['mail']);
$account
->save();
$logger
->notice('uncrumble updated name %name_old to %name_new, mail %mail_old to %mail_new on uid %uid.', [
'%name_old' => $account
->getAccountName(),
'%name_new' => $cookie['name'],
'%mail_old' => $account
->getEmail(),
'%mail_new' => $cookie['mail'],
'%uid' => $account
->id(),
]);
$this
->messenger()
->addMessage(t('Your account has been repaired.'));
$form_state
->setRedirect('user.page');
}
public function uncrumbleAccess(Request $request = NULL) {
$request = $request ?? \Drupal::request();
if ($this
->currentUser()
->isAnonymous() && $request
->getSession()
->get('BAKERY_CRUMBLED', FALSE)) {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
}