View source
<?php
namespace Drupal\email_registration\Plugin\Commerce\CheckoutPane;
use Drupal\commerce\CredentialsCheckFloodInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\Login;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Email;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\user\UserAuthInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ImmutableConfig;
class EmailRegistrationLogin extends Login {
public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, EntityTypeManagerInterface $entity_type_manager, CredentialsCheckFloodInterface $credentials_check_flood, AccountInterface $current_user, UserAuthInterface $user_auth, RequestStack $request_stack, ImmutableConfig $config) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager, $credentials_check_flood, $current_user, $user_auth, $request_stack);
$this->config = $config;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $checkout_flow, $container
->get('entity_type.manager'), $container
->get('commerce.credentials_check_flood'), $container
->get('current_user'), $container
->get('user.auth'), $container
->get('request_stack'), $container
->get('config.factory')
->get('email_registration.settings'));
}
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$pane_form = parent::buildPaneForm($pane_form, $form_state, $complete_form);
$login_with_username = $this->config
->get('login_with_username');
$pane_form['returning_customer']['name']['#title'] = $login_with_username ? $this
->t('Email address or username') : $this
->t('Email address');
$pane_form['returning_customer']['name']['#description'] = $login_with_username ? $this
->t('Enter your email address or username.') : $this
->t('Enter your email address.');
$pane_form['returning_customer']['name']['#element_validate'][] = 'email_registration_user_login_validate';
$pane_form['returning_customer']['name']['#type'] = $login_with_username ? 'textfield' : 'email';
$pane_form['returning_customer']['name']['#maxlength'] = Email::EMAIL_MAX_LENGTH;
$pane_form['returning_customer']['password']['#description'] = $this
->t('Enter the password that accompanies your email address.');
$complete_form['#cache']['tags'][] = 'config:email_registration.settings';
$pane_form['register']['name']['#type'] = 'value';
$pane_form['register']['name']['#value'] = 'email_registration_' . user_password();
$pane_form['register']['mail']['#title'] = $this
->t('Email');
return $pane_form;
}
public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
$subformId = $pane_form['#parents'][0];
$values = $form_state
->getValue($pane_form['#parents']);
$triggering_element = $form_state
->getTriggeringElement();
if ($triggering_element['#op'] === 'login') {
$mail = $values['returning_customer']['name'];
if (!empty($mail)) {
$user = user_load_by_mail($mail);
}
if (empty($user)) {
if (!$this->config
->get('login_with_username')) {
$user_input = $form_state
->getUserInput();
$query = isset($user_input[$subformId]['returning_customer']['name']) ? [
'name' => $user_input[$subformId]['returning_customer']['name'],
] : [];
$form_state
->setError($pane_form['returning_customer'], $this
->t('Unrecognized email address or password. <a href=":password">Forgot your password?</a>', [
':password' => Url::fromRoute('user.pass', [], [
'query' => $query,
])
->toString(),
]));
return;
}
}
else {
$username = $user
->getAccountName();
$form_state
->setValue([
$subformId,
'returning_customer',
'name',
], $username);
$name_element = $pane_form['returning_customer']['name'];
$password = trim($values['returning_customer']['password']);
$query = !empty($username) ? [
'name' => $username,
] : [];
$password_url = Url::fromRoute('user.pass', [], [
'query' => $query,
])
->toString();
if (user_is_blocked($username)) {
$form_state
->setError($name_element, $this
->t('The account with email address %mail has not been activated or is blocked.', [
'%mail' => $mail,
]));
return;
}
$uid = $this->userAuth
->authenticate($username, $password);
if (!$uid) {
$this->credentialsCheckFlood
->register($this->clientIp, $username);
if (!$this->config
->get('login_with_username')) {
$form_state
->setError($name_element, $this
->t('Unrecognized email address or password. <a href=":password">Forgot your password?</a>', [
':password' => $password_url,
]));
return;
}
else {
$form_state
->setError($name_element, $this
->t('Unrecognized username, email, or password. <a href=":url">Have you forgotten your password?</a>', [
':url' => $password_url,
]));
}
}
}
}
parent::validatePaneForm($pane_form, $form_state, $complete_form);
}
}