Skip to content

Zugriffskontrolle (hook_entity_access)

Willi edited this page Apr 10, 2018 · 1 revision
/**
 * Implements hook_entity_access().
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 * @param $operation
 * @param \Drupal\Core\Session\AccountInterface $account
 *
 * @return \Drupal\Core\Access\AccessResultForbidden|\Drupal\Core\Access\AccessResultNeutral
 */
function my_module_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  // drupal Users always have access
  if(in_array('authenticated', $account->getRoles())) {
    return AccessResult::neutral();
  }

  if ($entity->getEntityTypeId() == '<some_entity_type>' && $operation == 'delete') {
    // gets parameter from request
    if ($token = \Drupal::request()->get('token')) {
      if ($token == '<some_value>') {
        return AccessResult::neutral();
      }
    }
    return AccessResult::forbidden();
  }
}