You are here

public function PageController::accessDeniedPage in Restrict IP 8

Same name and namespace in other branches
  1. 8.2 src/Controller/PageController.php \Drupal\restrict_ip\Controller\PageController::accessDeniedPage()
  2. 3.x src/Controller/PageController.php \Drupal\restrict_ip\Controller\PageController::accessDeniedPage()

*

Overrides PageControllerInterface::accessDeniedPage

1 string reference to 'PageController::accessDeniedPage'
restrict_ip.routing.yml in ./restrict_ip.routing.yml
restrict_ip.routing.yml

File

src/Controller/PageController.php, line 97

Class

PageController

Namespace

Drupal\restrict_ip\Controller

Code

public function accessDeniedPage() {
  if (!isset($_SESSION['restrict_ip']) || !$_SESSION['restrict_ip']) {
    return new RedirectResponse(Url::fromRoute('<front>')
      ->toString());
  }
  $config = $this->configFactory
    ->get('restrict_ip.settings');
  $page['access_denied'] = [
    '#markup' => $this
      ->t('The page you are trying to access cannot be accessed from your IP address.'),
    '#prefix' => '<p>',
    '#suffix' => '</p>',
  ];
  $contact_mail = $config
    ->get('mail_address');
  if (strlen($contact_mail)) {
    $contact_mail = str_replace('@', '[at]', $contact_mail);
    $mail_markup = new FormattableMarkup('<span id="restrict_ip_contact_mail">@address</span>', [
      '@address' => $contact_mail,
    ]);
    $page['contact_us'] = [
      '#prefix' => '<p>',
      '#suffix' => '</p>',
      '#markup' => $this
        ->t('If you feel this is in error, please contact an administrator at @email.', [
        '@email' => $mail_markup,
      ]),
      '#attached' => [
        'library' => [
          'restrict_ip/mail_fixer',
        ],
      ],
    ];
  }
  if ($config
    ->get('allow_role_bypass')) {
    if ($this->currentUser
      ->isAuthenticated()) {
      $url = Url::fromRoute('user.logout');
      $link = Link::fromTextAndUrl($this
        ->t('Logout'), $url);
      $page['logout_link'] = [
        '#markup' => $link
          ->toString(),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      ];
    }
    elseif ($config
      ->get('bypass_action') === 'provide_link_login_page') {
      $url = Url::fromRoute('user.login');
      $link = Link::fromTextAndUrl($this
        ->t('Sign in'), $url);
      $page['login_link'] = [
        '#markup' => $link
          ->toString(),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      ];
    }
  }
  $this->moduleHandler
    ->alter('restrict_ip_access_denied_page', $page);
  return $page;
}