View source
<?php
namespace Drupal\tfa\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\user\Plugin\Block\UserLoginBlock;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TfaUserLoginBlock extends UserLoginBlock {
protected $tfaSettings;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_match);
$this->tfaSettings = $config_factory
->get('tfa.settings');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_route_match'), $container
->get('config.factory'));
}
protected function blockAccess(AccountInterface $account) {
$access = parent::blockAccess($account);
$tfaAccess = $this->tfaSettings
->get('enabled');
$route_name = $this->routeMatch
->getRouteName();
$disabled_route = in_array($route_name, [
'tfa.entry',
]);
if ($access
->isForbidden() || !$tfaAccess || $disabled_route) {
return AccessResult::forbidden();
}
return $access;
}
public function build() {
$form = \Drupal::formBuilder()
->getForm('Drupal\\tfa\\Form\\TfaLoginForm');
unset($form['name']['#attributes']['autofocus']);
unset($form['name']['#description']);
unset($form['name']['#attributes']['aria-describedby']);
unset($form['pass']['#description']);
unset($form['pass']['#attributes']['aria-describedby']);
$form['name']['#size'] = 15;
$form['pass']['#size'] = 15;
$form['#action'] = Url::fromRoute('<current>', [], [
'query' => $this
->getDestinationArray(),
'external' => FALSE,
])
->toString();
$items = [];
if (\Drupal::config('user.settings')
->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
$items['create_account'] = Link::fromTextAndUrl($this
->t('Create new account'), new Url('user.register', [], [
'attributes' => [
'title' => $this
->t('Create a new user account.'),
'class' => [
'create-account-link',
],
],
]));
}
$items['request_password'] = Link::fromTextAndUrl($this
->t('Reset your password'), new Url('user.pass', [], [
'attributes' => [
'title' => $this
->t('Send password reset instructions via email.'),
'class' => [
'request-password-link',
],
],
]));
return [
'user_login_form' => $form,
'user_links' => [
'#theme' => 'item_list',
'#items' => $items,
],
];
}
}