You are here

public function LoginFirewall::execute in Restrict Login or Role Access by IP Address 8.4

Checks that login is allowed, and takes appropriate action if not.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user to check if login is allowed.

Overrides LoginFirewallInterface::execute

File

src/LoginFirewall.php, line 41

Class

LoginFirewall
Class LoginFirewall.

Namespace

Drupal\restrict_by_ip

Code

public function execute(AccountInterface $account) {
  if ($account
    ->isAuthenticated() && !$this
    ->isLoginAllowed($account)) {
    $user_ip = $this->ipTools
      ->getUserIP();

    // Log the error with the ip address.
    $this->logger
      ->notice(t('Login denied from @ip for %name.', [
      '%name' => $account
        ->getAccountName(),
      '@ip' => $user_ip,
    ]));
    user_logout();

    // Redirect after logout.
    $path = $this->config
      ->get('error_page');
    $options = [
      'absolute' => TRUE,
    ];
    if ($path) {
      $redirect = $this->urlGenerator
        ->assemble('base:' . $path, $options);
    }
    else {
      $redirect = Url::fromRoute('<current>', [], $options)
        ->toString();
    }
    $response = new RedirectResponse($redirect, RedirectResponse::HTTP_FOUND);
    $response
      ->send();
  }
}