1

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 Answer 1

0

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();
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.