Skip to content

Commit

Permalink
Add in config to allow the anonymous check to be skipped.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Feb 16, 2024
1 parent f331e37 commit a9d67f9
Showing 1 changed file with 68 additions and 5 deletions.
73 changes: 68 additions & 5 deletions src/Plugin/search_api/processor/HOCRField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Drupal\islandora_hocr\Plugin\search_api\processor;

use Drupal\Core\Access\AccessibleInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\file\FileInterface;
use Drupal\islandora_hocr\Plugin\search_api\processor\Property\HOCRFieldProperty;
Expand All @@ -29,7 +33,7 @@
* hidden = true,
* )
*/
class HOCRField extends ProcessorPluginBase {
class HOCRField extends ProcessorPluginBase implements PluginFormInterface {

use PluginFormTrait;

Expand All @@ -42,6 +46,13 @@ class HOCRField extends ProcessorPluginBase {
*/
protected EntityTypeManagerInterface $entityTypeManager;

/**
* Anonymous user session, if we are not skipping the anonymous access check.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected AccountInterface $anonymousSession;

/**
* {@inheritDoc}
*/
Expand All @@ -50,6 +61,10 @@ public static function create(ContainerInterface $container, array $configuratio

$instance->entityTypeManager = $container->get('entity_type.manager');

if (!$instance->configuration['skip_anon_check']) {
$instance->anonymousSession = new AnonymousUserSession();
}

return $instance;
}

Expand Down Expand Up @@ -152,15 +167,13 @@ protected function getFile(NodeInterface $node) : ?FileInterface {

$media = $query->execute();

$anonymous = new AnonymousUserSession();

foreach ($media as $medium) {
/** @var \Drupal\media\MediaInterface $entity */
$entity = $media_storage->load($medium);
if (!$entity) {
continue;
}
elseif (!$entity->access('view', $anonymous, FALSE)) {
elseif (!$this->checkEntityAccess($entity)) {
continue;
}

Expand All @@ -171,7 +184,7 @@ protected function getFile(NodeInterface $node) : ?FileInterface {
/** @var \Drupal\file\FileInterface $file */
$file = $this->entityTypeManager->getStorage('file')->load($fid);

if (!$file->access('view', $anonymous, FALSE)) {
if (!$this->checkEntityAccess($file)) {
continue;
}

Expand All @@ -183,4 +196,54 @@ protected function getFile(NodeInterface $node) : ?FileInterface {
return NULL;
}

/**
* Check access for the given entity.
*
* @param \Drupal\Core\Access\AccessibleInterface $entity
* The entity of which to check access.
*
* @return bool
* Short-circuits to TRUE if configured to "skip_anon_check"; otherwise,
* the result of an access check against the entity.
*/
protected function checkEntityAccess(AccessibleInterface $entity) : bool {
if ($this->configuration['skip_anon_check']) {
return TRUE;
}

return $entity->access('view', $this->anonymousSession, FALSE);
}

/**
* {@inheritDoc}
*/
public function defaultConfiguration() {
return [
'skip_anon_check' => FALSE,
] + parent::defaultConfiguration();
}

/**
* {@inheritDoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['skip_anon_check'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip anonymous user check'),
'#description' => $this->t('Allow the indexing of potentially access-controlled content into this index.'),
'#default_value' => $this->configuration['skip_anon_check'],
];

return $form;
}

/**
* {@inheritDoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->setConfiguration([
'skip_anon_check' => $form_state->getValue('skip_anon_check'),
]);
}

}

0 comments on commit a9d67f9

Please sign in to comment.