From acb654c2b6acc81f50a08ec635a4a5f21146fa85 Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Tue, 11 Aug 2020 11:22:08 -0300 Subject: [PATCH 1/5] Adding new embargoes block. --- embargoes.module | 7 + src/Entity/EmbargoesEmbargoEntity.php | 3 +- src/Form/EmbargoesSettingsForm.php | 8 + .../EmbargoesEmbargoNotificationBlock.php | 195 ++++++++++++++++++ templates/embargoes-notifications.html.twig | 22 ++ 5 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php create mode 100644 templates/embargoes-notifications.html.twig diff --git a/embargoes.module b/embargoes.module index 34bb8a8..0fe1948 100644 --- a/embargoes.module +++ b/embargoes.module @@ -72,5 +72,12 @@ function embargoes_theme($existing, $type, $theme, $path) { 'embargo_info' => [], ], ], + 'embargoes_notifications' => [ + 'template' => 'embargoes-notifications', + 'variables' => [ + 'count' => NULL, + 'embargo_info' => [], + ], + ], ]; } diff --git a/src/Entity/EmbargoesEmbargoEntity.php b/src/Entity/EmbargoesEmbargoEntity.php index ad50e31..0bfb32b 100644 --- a/src/Entity/EmbargoesEmbargoEntity.php +++ b/src/Entity/EmbargoesEmbargoEntity.php @@ -11,7 +11,8 @@ * @ConfigEntityType( * id = "embargoes_embargo_entity", * label = @Translation("Embargo"), - * handlers = { * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder", + * handlers = { + * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder", * "list_builder" = "Drupal\embargoes\Controller\EmbargoesEmbargoEntityListBuilder", * "form" = { * "add" = "Drupal\embargoes\Form\EmbargoesEmbargoEntityForm", diff --git a/src/Form/EmbargoesSettingsForm.php b/src/Form/EmbargoesSettingsForm.php index 638badb..1cdcad4 100644 --- a/src/Form/EmbargoesSettingsForm.php +++ b/src/Form/EmbargoesSettingsForm.php @@ -52,6 +52,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => $config->get('show_embargo_message'), ]; + $form['embargo_notification_message'] = [ + '#type' => 'textarea', + '#title' => $this->t('Embargo notification messsage.'), + '#description' => $this->t('Notification text displayed to the user when an object or its files are under embargo. Use the "@contact" string to include the configured contact email, if available.'), + '#default_value' => $config->get('embargo_notification_message'), + ]; + return $form; } @@ -63,6 +70,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $config->set('show_embargo_message', $form_state->getValue('show_embargo_message')); $config->set('add_contact_to_notifications', $form_state->getValue('add_contact_to_notifications')); $config->set('embargo_contact_email', $form_state->getValue('embargo_contact_email')); + $config->set('embargo_notification_message', $form_state->getValue('embargo_notification_message')); $config->save(); parent::submitForm($form, $form_state); } diff --git a/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php b/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php new file mode 100644 index 0000000..458e395 --- /dev/null +++ b/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php @@ -0,0 +1,195 @@ +get('current_route_match'), + $container->get('config.factory'), + $container->get('embargoes.embargoes'), + $container->get('entity_type.manager') + ); + } + +// /** +// * {@inheritdoc} +// */ +// public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { +// return new static( +// $configuration, +// $plugin_id, +// $plugin_definition, +// $container->get('current_route_match'), +// $container->get('embargoes.embargoes'), +// $container->get('entity_type.manager')); +// } + + public function __construct(array $configuration, $plugin_id, $plugin_definition, ResettableStackedRouteMatchInterface $route_match, ConfigFactoryInterface $config_factory, EmbargoesEmbargoesServiceInterface $embargoes, EntityTypeManagerInterface $entity_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->adminMail = $config_factory->get('embargoes.settings')->get('embargo_contact_email'); + $this->notificationMessage = $config_factory->get('embargoes.settings')->get('embargo_notification_message'); + $this->entityManager = $entity_manager; + $this->embargoes = $embargoes; + $this->routeMatch = $route_match; + } +// /** +// * Constructs an embargoes policies block. +// * +// * @param array $configuration +// * Block configuration. +// * @param string $plugin_id +// * The plugin ID. +// * @param mixed $plugin_definition +// * The plugin definition. +// * @param \Drupal\Core\Routing\ResettableStackedRouteMatchInterface $route_match +// * A route matching interface. +// * @param \Drupal\embargoes\EmbargoesEmbargoesServiceInterface $embargoes +// * An embargoes management service. +// * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager +// * An entity type manager. +// */ +// public function __construct(array $configuration, $plugin_id, $plugin_definition, ResettableStackedRouteMatchInterface $route_match, EmbargoesEmbargoesServiceInterface $embargoes, EntityTypeManagerInterface $entity_manager) { +// parent::__construct($configuration, $plugin_id, $plugin_definition); +// $this->routeMatch = $route_match; +// $this->embargoes = $embargoes; +// $this->entityManager = $entity_manager; +// } + + /** + * {@inheritdoc} + */ + public function build() { + $node = $this->routeMatch->getParameter('node'); + if ($node instanceof NodeInterface) { + $embargoes = $this->embargoes->getCurrentEmbargoesByNids([$node->id()]); + + $num_embargoes = count($embargoes); + ksm($num_embargoes); + if ($num_embargoes > 0) { + $t = $this->getStringTranslation(); + $embargoes_info = []; + $cache_tags = [ + "node:{$node->id()}", + ]; + $embargoes_count = $t->formatPlural( + $num_embargoes, + 'This resource is under 1 embargo:', + 'This resource is under @count embargoes:' + ); +// + foreach ($embargoes as $embargo_id) { + ksm($embargoes); + $embargo = $this->entityManager->getStorage('embargoes_embargo_entity')->load($embargo_id); + $embargo_info = []; + // Expiration string. +// if (!$embargo->getExpirationType()) { +// $embargo_info['expiration'] = $t->translate('Duration: Indefinite'); +// } +// else { +// $embargo_info['expiration'] = $t->translate('Duration: Until @duration', [ +// '@duration' => $embargo->getExpirationDate(), +// ]); +// } +// // Embargo type string. +// if (!$embargo->getEmbargoType()) { +// $embargo_info['type'] = $t->translate('Disallow Access To: Resource Files'); +// } +// else { +// $embargo_info['type'] = $t->translate('Disallow Access To: Resource'); +// } +// // Exempt IP string. +// if (!($embargo->getExemptIps())) { +// $embargo_info['exempt_ips'] = ''; +// } +// else { +// $embargo_info['exempt_ips'] = $t->translate('Allowed Networks: @network', [ +// '@network' => $this->entityManager->getStorage('embargoes_ip_range_entity')->load($embargo->getExemptIps())->label(), +// ]); +// } + $embargo_info['type'] = $t->translate('embargo info'); + $embargo_info['message'] = $this->notificationMessage; + $embargoes_info[] = $embargo_info; + + $cache_tags[] = "embargoes_embargo_entity:{$embargo->id()}"; + } +// + return [ + '#theme' => 'embargoes_notifications', + '#count' => $t->translate('Test count'), + '#embargo_info' => $embargoes_info, + '#cache' => [ + 'tags' => $cache_tags, + ], + ]; + } + } + + return []; + } + +} diff --git a/templates/embargoes-notifications.html.twig b/templates/embargoes-notifications.html.twig new file mode 100644 index 0000000..f1fae12 --- /dev/null +++ b/templates/embargoes-notifications.html.twig @@ -0,0 +1,22 @@ +{# + /** + * @file + * Formats an IP restricted page. + * + * Available variables: + * - 'count': the count to display + * - 'embargoes_info': an indexed array containing any information required to + * display each individual embargo. + */ +#} + +{{ count }} + +{% for info in embargoes_info %} +
+ +{% endfor %} From 41debf33a46ea21d9250954b1316cd3c06757dc1 Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Thu, 13 Aug 2020 08:01:46 -0300 Subject: [PATCH 2/5] Adding new embargoes notification block. --- embargoes.module | 27 +-- src/EventSubscriber/IpRedirectAttacher.php | 35 ++-- .../EmbargoesEmbargoNotificationBlock.php | 185 +++++++++++------- templates/embargoes-notifications.html.twig | 14 +- 4 files changed, 140 insertions(+), 121 deletions(-) diff --git a/embargoes.module b/embargoes.module index 85cb0fe..8da2c0b 100644 --- a/embargoes.module +++ b/embargoes.module @@ -8,7 +8,6 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Session\AccountInterface; -use Drupal\embargoes\Access\EmbargoesFileAccessHandler; use Drupal\node\NodeInterface; /** @@ -46,31 +45,6 @@ function embargoes_media_view(array &$build, EntityInterface $media, EntityViewD \Drupal::service('embargoes.media_access')->setEmbargoMessage($media); } -/** - * Implements hook_entity_type_alter(). - */ -function embargoes_entity_type_alter(array &$entity_types) { - if (isset($entity_types['file'])) { - $entity_types['file']->setHandlerClass('access', EmbargoesFileAccessHandler::class); - } -} - -/** - * Implemens hook_file_download(). - */ -function embargoes_file_download($uri) { - $files = \Drupal::entityTypeManager() - ->getStorage('file') - ->loadByProperties(['uri' => $uri]); - $file = reset($files); - if ($file instanceof EntityInterface) { - $access = \Drupal::service('embargoes.file_access')->isActivelyEmbargoed($file, \Drupal::currentUser()); - if ($access->isForbidden()) { - return -1; - } - } -} - /** * Implements hook_theme(). */ @@ -103,6 +77,7 @@ function embargoes_theme($existing, $type, $theme, $path) { 'variables' => [ 'count' => NULL, 'embargo_info' => [], + 'message' => NULL, ], ], ]; diff --git a/src/EventSubscriber/IpRedirectAttacher.php b/src/EventSubscriber/IpRedirectAttacher.php index 79a6df8..aa67e31 100644 --- a/src/EventSubscriber/IpRedirectAttacher.php +++ b/src/EventSubscriber/IpRedirectAttacher.php @@ -3,10 +3,8 @@ namespace Drupal\embargoes\EventSubscriber; use Drupal\embargoes\Access\EmbargoedAccessInterface; -use Drupal\file\FileInterface; -use Drupal\node\NodeInterface; -use Drupal\media\MediaInterface; use Drupal\Core\Session\AccountInterface; +use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\KernelEvents; @@ -62,21 +60,28 @@ public function __construct(EmbargoedAccessInterface $node_access, EmbargoedAcce * The initial response. */ public function attachIpRedirect(GetResponseEvent $response) { + $route_name = $response->getRequest()->attributes->get(RouteObjectInterface::ROUTE_NAME); $redirect_url = NULL; - // Cycle through all attributes; the first one we get back that's restricted - // means redirection is necessary. - foreach ($response->getRequest()->attributes->all() as $attribute) { - if ($attribute instanceof NodeInterface) { - $redirect_url = $this->nodeAccess->getIpEmbargoedRedirectUrl($attribute, $this->user); - break; + // Redirect for nodes. + if (substr($route_name, 0, 11) == 'entity.node') { + $node = $response->getRequest()->attributes->get('node'); + if ($node) { + $redirect_url = $this->nodeAccess->getIpEmbargoedRedirectUrl($node, $this->user); + } - if ($attribute instanceof MediaInterface) { - $redirect_url = $this->mediaAccess->getIpEmbargoedRedirectUrl($attribute, $this->user); - break; + } + // Redirect for media. + elseif (substr($route_name, 0, 12) == 'entity.media') { + $media = $response->getRequest()->attributes->get('media'); + if ($media) { + $redirect_url = $this->mediaAccess->getIpEmbargoedRedirectUrl($media, $this->user); } - if ($attribute instanceof FileInterface) { - $redirect_url = $this->fileAccess->getIpEmbargoedRedirectUrl($attribute, $this->user); - break; + } + // Redirect for files. + elseif (substr($route_name, 0, 11) == 'entity.file') { + $file = $response->getRequest()->attributes->get('file'); + if ($file) { + $redirect_url = $this->fileAccess->getIpEmbargoedRedirectUrl($file, $this->user); } } if ($redirect_url) { diff --git a/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php b/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php index 458e395..ce2423f 100644 --- a/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php +++ b/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php @@ -9,6 +9,8 @@ use Drupal\Core\Routing\ResettableStackedRouteMatchInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Component\Utility\Html; +use Drupal\Core\Cache\Cache; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -22,13 +24,6 @@ */ class EmbargoesEmbargoNotificationBlock extends BlockBase implements ContainerFactoryPluginInterface { -// /** -// * An entity type manager. -// * -// * @var \Drupal\Core\Entity\EntityTypeManagerInterface -// */ -// protected $entityManager; - /** * The admin email address. * @@ -64,6 +59,9 @@ class EmbargoesEmbargoNotificationBlock extends BlockBase implements ContainerFa */ protected $embargoes; + /** + * {@inheritdoc} + */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, @@ -76,19 +74,24 @@ public static function create(ContainerInterface $container, array $configuratio ); } -// /** -// * {@inheritdoc} -// */ -// public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { -// return new static( -// $configuration, -// $plugin_id, -// $plugin_definition, -// $container->get('current_route_match'), -// $container->get('embargoes.embargoes'), -// $container->get('entity_type.manager')); -// } - + /** + * Construct embargo notification block. + * + * @param array $configuration + * Block configuration. + * @param string $plugin_id + * The plugin ID. + * @param mixed $plugin_definition + * The plugin definition. + * @param \Drupal\Core\Routing\ResettableStackedRouteMatchInterface $route_match + * A route matching interface. + * @param Drupal\Core\Config\ConfigFactoryInterface $config_factory + * A configuration factory interface. + * @param \Drupal\embargoes\EmbargoesEmbargoesServiceInterface $embargoes + * An embargoes management service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager + * An entity type manager. + */ public function __construct(array $configuration, $plugin_id, $plugin_definition, ResettableStackedRouteMatchInterface $route_match, ConfigFactoryInterface $config_factory, EmbargoesEmbargoesServiceInterface $embargoes, EntityTypeManagerInterface $entity_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->adminMail = $config_factory->get('embargoes.settings')->get('embargo_contact_email'); @@ -97,28 +100,6 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->embargoes = $embargoes; $this->routeMatch = $route_match; } -// /** -// * Constructs an embargoes policies block. -// * -// * @param array $configuration -// * Block configuration. -// * @param string $plugin_id -// * The plugin ID. -// * @param mixed $plugin_definition -// * The plugin definition. -// * @param \Drupal\Core\Routing\ResettableStackedRouteMatchInterface $route_match -// * A route matching interface. -// * @param \Drupal\embargoes\EmbargoesEmbargoesServiceInterface $embargoes -// * An embargoes management service. -// * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager -// * An entity type manager. -// */ -// public function __construct(array $configuration, $plugin_id, $plugin_definition, ResettableStackedRouteMatchInterface $route_match, EmbargoesEmbargoesServiceInterface $embargoes, EntityTypeManagerInterface $entity_manager) { -// parent::__construct($configuration, $plugin_id, $plugin_definition); -// $this->routeMatch = $route_match; -// $this->embargoes = $embargoes; -// $this->entityManager = $entity_manager; -// } /** * {@inheritdoc} @@ -127,60 +108,90 @@ public function build() { $node = $this->routeMatch->getParameter('node'); if ($node instanceof NodeInterface) { $embargoes = $this->embargoes->getCurrentEmbargoesByNids([$node->id()]); - $num_embargoes = count($embargoes); - ksm($num_embargoes); + if ($num_embargoes > 0) { $t = $this->getStringTranslation(); $embargoes_info = []; $cache_tags = [ "node:{$node->id()}", + "extensions", + "env", ]; $embargoes_count = $t->formatPlural( $num_embargoes, 'This resource is under 1 embargo:', 'This resource is under @count embargoes:' ); -// + + $contact_message = ""; foreach ($embargoes as $embargo_id) { - ksm($embargoes); + $embargo = $this->entityManager->getStorage('embargoes_embargo_entity')->load($embargo_id); $embargo_info = []; + // Expiration string. -// if (!$embargo->getExpirationType()) { -// $embargo_info['expiration'] = $t->translate('Duration: Indefinite'); -// } -// else { -// $embargo_info['expiration'] = $t->translate('Duration: Until @duration', [ -// '@duration' => $embargo->getExpirationDate(), -// ]); -// } -// // Embargo type string. -// if (!$embargo->getEmbargoType()) { -// $embargo_info['type'] = $t->translate('Disallow Access To: Resource Files'); -// } -// else { -// $embargo_info['type'] = $t->translate('Disallow Access To: Resource'); -// } -// // Exempt IP string. -// if (!($embargo->getExemptIps())) { -// $embargo_info['exempt_ips'] = ''; -// } -// else { -// $embargo_info['exempt_ips'] = $t->translate('Allowed Networks: @network', [ -// '@network' => $this->entityManager->getStorage('embargoes_ip_range_entity')->load($embargo->getExemptIps())->label(), -// ]); -// } - $embargo_info['type'] = $t->translate('embargo info'); - $embargo_info['message'] = $this->notificationMessage; + if (!$embargo->getExpirationType()) { + $embargo_info['expiration'] = $t->translate('Indefinitely'); + $embargo_info['has_duration'] = FALSE; + } + else { + $embargo_info['expiration'] = $t->translate('Expires @duration', [ + '@duration' => $embargo->getExpirationDate(), + ]); + $embargo_info['has_duration'] = TRUE; + } + + // Embargo type string, including a message for the given type. + if (!$embargo->getEmbargoType()) { + $embargo_info['type'] = 'Files'; + $embargo_info['type_message'] = $t->translate('Access to all associated files of this resource is restricted'); + } + else { + $embargo_info['type'] = 'Node'; + $embargo_info['type_message'] = $t->translate('Access to this resource and all associated files is restricted'); + } + + // Exempt IP string. + if (!($embargo->getExemptIps())) { + $embargo_info['exempt_ips'] = ''; + } + else { + $embargo_info['exempt_ips'] = $t->translate('Allowed Networks: @network', [ + '@network' => $this->entityManager->getStorage('embargoes_ip_range_entity')->load($embargo->getExemptIps())->label(), + ]); + } + + // Determine if given user is exempt or not. If not, prepare a message + // the user can use to request access. + $exempt_users = $embargo->getExemptUsers(); + $embargo_info['user_exempt'] = FALSE; + foreach ($exempt_users as $user) { + if ($user['target_id'] == \Drupal::currentUser()->id()) { + $embargo_info['user_exempt'] = TRUE; + } + else { + $contact_message = $t->translate( + $this->notificationMessage, + ['@contact' => $this->adminMail] + ); + } + } + + $embargo_info['dom_id'] = Html::getUniqueId('embargo_notification'); $embargoes_info[] = $embargo_info; - $cache_tags[] = "embargoes_embargo_entity:{$embargo->id()}"; + array_push( + $cache_tags, + "config:embargoes.embargoes_embargo_entity.{$embargo->id()}" + ); + } -// + return [ '#theme' => 'embargoes_notifications', - '#count' => $t->translate('Test count'), + '#count' => $embargoes_count, + '#message' => $contact_message, '#embargo_info' => $embargoes_info, '#cache' => [ 'tags' => $cache_tags, @@ -192,4 +203,28 @@ public function build() { return []; } + /** + * {@inheritdoc} + */ + public function getCacheTags() { + // When the given node changes (route), the block should rebuild. + if ($node = \Drupal::routeMatch()->getParameter('node')) { + return Cache::mergeTags( + parent::getCacheTags(), + array('node:' . $node->id()) + ); + } + + // Return default tags, if not on a node page. + return parent::getCacheTags(); + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + // Ensure that with every new node/route, this block will be rebuilt. + return Cache::mergeContexts(parent::getCacheContexts(), array('route')); + } + } diff --git a/templates/embargoes-notifications.html.twig b/templates/embargoes-notifications.html.twig index f1fae12..1f72fe4 100644 --- a/templates/embargoes-notifications.html.twig +++ b/templates/embargoes-notifications.html.twig @@ -7,16 +7,20 @@ * - 'count': the count to display * - 'embargoes_info': an indexed array containing any information required to * display each individual embargo. + * - 'message': Configured message to request access. */ #} {{ count }} -{% for info in embargoes_info %} -
{% endfor %} + + +

{{ message }}

From 246a6150cf678805af9fe0f2759fa6c3866fe733 Mon Sep 17 00:00:00 2001 From: MorganDawe Date: Thu, 13 Aug 2020 08:11:36 -0300 Subject: [PATCH 3/5] Bringing up to date with head. --- embargoes.module | 26 ++++++++++++++++ src/EventSubscriber/IpRedirectAttacher.php | 35 ++++++++++------------ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/embargoes.module b/embargoes.module index 8da2c0b..0fbc013 100644 --- a/embargoes.module +++ b/embargoes.module @@ -8,6 +8,7 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\embargoes\Access\EmbargoesFileAccessHandler; use Drupal\node\NodeInterface; /** @@ -45,6 +46,31 @@ function embargoes_media_view(array &$build, EntityInterface $media, EntityViewD \Drupal::service('embargoes.media_access')->setEmbargoMessage($media); } +/** + * Implements hook_entity_type_alter(). + */ +function embargoes_entity_type_alter(array &$entity_types) { + if (isset($entity_types['file'])) { + $entity_types['file']->setHandlerClass('access', EmbargoesFileAccessHandler::class); + } +} + +/** + * Implements hook_file_download(). + */ +function embargoes_file_download($uri) { + $files = \Drupal::entityTypeManager() + ->getStorage('file') + ->loadByProperties(['uri' => $uri]); + $file = reset($files); + if ($file instanceof EntityInterface) { + $access = \Drupal::service('embargoes.file_access')->isActivelyEmbargoed($file, \Drupal::currentUser()); + if ($access->isForbidden()) { + return -1; + } + } +} + /** * Implements hook_theme(). */ diff --git a/src/EventSubscriber/IpRedirectAttacher.php b/src/EventSubscriber/IpRedirectAttacher.php index aa67e31..79a6df8 100644 --- a/src/EventSubscriber/IpRedirectAttacher.php +++ b/src/EventSubscriber/IpRedirectAttacher.php @@ -3,8 +3,10 @@ namespace Drupal\embargoes\EventSubscriber; use Drupal\embargoes\Access\EmbargoedAccessInterface; +use Drupal\file\FileInterface; +use Drupal\node\NodeInterface; +use Drupal\media\MediaInterface; use Drupal\Core\Session\AccountInterface; -use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\KernelEvents; @@ -60,28 +62,21 @@ public function __construct(EmbargoedAccessInterface $node_access, EmbargoedAcce * The initial response. */ public function attachIpRedirect(GetResponseEvent $response) { - $route_name = $response->getRequest()->attributes->get(RouteObjectInterface::ROUTE_NAME); $redirect_url = NULL; - // Redirect for nodes. - if (substr($route_name, 0, 11) == 'entity.node') { - $node = $response->getRequest()->attributes->get('node'); - if ($node) { - $redirect_url = $this->nodeAccess->getIpEmbargoedRedirectUrl($node, $this->user); - + // Cycle through all attributes; the first one we get back that's restricted + // means redirection is necessary. + foreach ($response->getRequest()->attributes->all() as $attribute) { + if ($attribute instanceof NodeInterface) { + $redirect_url = $this->nodeAccess->getIpEmbargoedRedirectUrl($attribute, $this->user); + break; } - } - // Redirect for media. - elseif (substr($route_name, 0, 12) == 'entity.media') { - $media = $response->getRequest()->attributes->get('media'); - if ($media) { - $redirect_url = $this->mediaAccess->getIpEmbargoedRedirectUrl($media, $this->user); + if ($attribute instanceof MediaInterface) { + $redirect_url = $this->mediaAccess->getIpEmbargoedRedirectUrl($attribute, $this->user); + break; } - } - // Redirect for files. - elseif (substr($route_name, 0, 11) == 'entity.file') { - $file = $response->getRequest()->attributes->get('file'); - if ($file) { - $redirect_url = $this->fileAccess->getIpEmbargoedRedirectUrl($file, $this->user); + if ($attribute instanceof FileInterface) { + $redirect_url = $this->fileAccess->getIpEmbargoedRedirectUrl($attribute, $this->user); + break; } } if ($redirect_url) { From e10507b36977c6558c08ef8b214f78ade0a657ac Mon Sep 17 00:00:00 2001 From: JojoVes Date: Fri, 14 Aug 2020 14:03:48 -0300 Subject: [PATCH 4/5] fix issue of setting expiration date for indefinite embargoes This can happen when switching something from scheduled to indefinite without manually clearing the expiration date before toggling the indefinite radio button in the form. Wasn't breaking anything, but it made the embargoes page look misleading. --- src/Form/EmbargoesEmbargoEntityForm.php | 11 +++++++++-- src/Form/EmbargoesNodeEmbargoesForm.php | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Form/EmbargoesEmbargoEntityForm.php b/src/Form/EmbargoesEmbargoEntityForm.php index ddc75a6..42aca26 100644 --- a/src/Form/EmbargoesEmbargoEntityForm.php +++ b/src/Form/EmbargoesEmbargoEntityForm.php @@ -189,8 +189,15 @@ public function form(array $form, FormStateInterface $form_state) { public function save(array $form, FormStateInterface $form_state) { $embargo = $this->entity; $embargo->setEmbargoType($form_state->getValue('embargo_type')); - $embargo->setExpirationType($form_state->getValue('expiry_type')); - $embargo->setExpirationDate($form_state->getValue('expiration_date')); + $expiry_type = $form_state->getValue('expiry_type'); + $embargo->setExpirationType($expiry_type); + // Clear expiry date for indefinite embargoes. + if ($expiry_type === '0') { + $embargo->setExpirationDate(''); + } + else { + $embargo->setExpirationDate($form_state->getValue('expiration_date')); + } $embargo->setExemptIps($form_state->getValue('exempt_ips')); $embargo->setExemptUsers($form_state->getValue('exempt_users')); $embargo->setAdditionalEmails($form_state->getValue('additional_emails')); diff --git a/src/Form/EmbargoesNodeEmbargoesForm.php b/src/Form/EmbargoesNodeEmbargoesForm.php index 89e3b95..327417c 100644 --- a/src/Form/EmbargoesNodeEmbargoesForm.php +++ b/src/Form/EmbargoesNodeEmbargoesForm.php @@ -201,8 +201,15 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } $embargo->setEmbargoType($form_state->getValue('embargo_type')); - $embargo->setExpirationType($form_state->getValue('expiry_type')); - $embargo->setExpirationDate($form_state->getValue('expiry_date')); + $expiry_type = $form_state->getValue('expiry_type'); + $embargo->setExpirationType($expiry_type); + // Clear expiry date for indefinite embargoes. + if ($expiry_type === '0') { + $embargo->setExpirationDate(''); + } + else { + $embargo->setExpirationDate($form_state->getValue('expiry_date')); + } $embargo->setExemptIps($form_state->getValue('exempt_ips')); $embargo->setExemptUsers($form_state->getValue('exempt_users')); $embargo->setAdditionalEmails($form_state->getValue('additional_emails')); From ab6cdcd368862176489bca513fd8a8a67cf6793d Mon Sep 17 00:00:00 2001 From: JojoVes Date: Tue, 18 Aug 2020 15:16:15 -0300 Subject: [PATCH 5/5] make sure the contact message is added for nodes that don't have any exempt users before this change, if viewing an embargoed object that didn't have any exempt users, then the foreach loop wouldn't be run for the empty array and the message wouldn't be added. --- .../Block/EmbargoesEmbargoNotificationBlock.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php b/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php index ce2423f..263ca5c 100644 --- a/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php +++ b/src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php @@ -170,12 +170,12 @@ public function build() { if ($user['target_id'] == \Drupal::currentUser()->id()) { $embargo_info['user_exempt'] = TRUE; } - else { - $contact_message = $t->translate( - $this->notificationMessage, - ['@contact' => $this->adminMail] - ); - } + } + if (!$embargo_info['user_exempt']) { + $contact_message = $t->translate( + $this->notificationMessage, + ['@contact' => $this->adminMail] + ); } $embargo_info['dom_id'] = Html::getUniqueId('embargo_notification');