Drupal 9 - Allow access to a page only from a specific domain

193 views Asked by At

How to allow access to a Drupal file/page only from a specific domain?

I have tried php filter module but enabling this module can cause security and performance issues as it allows users to execute PHP code on your site.

1

There are 1 answers

0
Sharklalu On

You can add an AccessChecker on your specific route and check if the domain is the correct one.

In your_module.services.yml :

  your_module.domain_access_checker:
    class: Drupal\your_module\Access\DomainAccessChecker
    tags:
      - { name: access_check, applies_to: _acces_domain }

In your_module.routing.yml:

your_module.mypage:
  path: '/my-path'
  defaults:
    _title: 'My page'
    _controller: '\Drupal\your_module\Controller\MyController::show'
  requirements:
    _acces_domain : 'TRUE'

And finaly in your DomainAccessChecker.php:

  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
    $domainIsCorrect = your_code_here;
    if ($domainIsCorrect) {
      return AccessResult::allowed();
    }
    return AccessResult::forbidden();
  }