Skip to content

Instantly share code, notes, and snippets.

@ygerasimov
Last active July 11, 2024 13:51
Show Gist options
  • Save ygerasimov/b27db8b9cde8d6d779dcc03e12da106c to your computer and use it in GitHub Desktop.
Save ygerasimov/b27db8b9cde8d6d779dcc03e12da106c to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_entity_access().
*/
function HOOK_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
// Only allow access to the test page.
$testPageNid = 111111;
if ((int) $entity->id() !== $testPageNid) {
return AccessResult::neutral();
}
$allowlist = [
// Diffy.
'3.216.56.216',
// Me.
'22.222.22.222',
];
$requestIP = \Drupal::request()->getClientIp();
$isOnAllowlist = in_array($requestIP, $allowlist, TRUE);
return AccessResult::allowedIf($isOnAllowlist);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment