Skip to content

Commit

Permalink
Merge pull request #12 from MorganDawe/8.x-ir-121
Browse files Browse the repository at this point in the history
8.x ir 121
  • Loading branch information
Daniel Aitken authored Aug 31, 2020
2 parents c1718da + ab6cdcd commit cd50724
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 6 deletions.
10 changes: 9 additions & 1 deletion embargoes.module
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function embargoes_entity_type_alter(array &$entity_types) {
}

/**
* Implemens hook_file_download().
* Implements hook_file_download().
*/
function embargoes_file_download($uri) {
$files = \Drupal::entityTypeManager()
Expand Down Expand Up @@ -98,5 +98,13 @@ function embargoes_theme($existing, $type, $theme, $path) {
'embargo_info' => [],
],
],
'embargoes_notifications' => [
'template' => 'embargoes-notifications',
'variables' => [
'count' => NULL,
'embargo_info' => [],
'message' => NULL,
],
],
];
}
3 changes: 2 additions & 1 deletion src/Entity/EmbargoesEmbargoEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions src/Form/EmbargoesEmbargoEntityForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
11 changes: 9 additions & 2 deletions src/Form/EmbargoesNodeEmbargoesForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
8 changes: 8 additions & 0 deletions src/Form/EmbargoesSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down
230 changes: 230 additions & 0 deletions src/Plugin/Block/EmbargoesEmbargoNotificationBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

namespace Drupal\embargoes\Plugin\Block;

use Drupal\node\NodeInterface;
use Drupal\embargoes\EmbargoesEmbargoesServiceInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
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;

/**
* Provides a "Embargo Notifications" block.
*
* @Block(
* id="embargoes_embargo_notification_block",
* admin_label = @Translation("Embargo Notifications"),
* category = @Translation("Embargoes")
* )
*/
class EmbargoesEmbargoNotificationBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* The admin email address.
*
* @var string
*/
protected $adminMail;

/**
* The notification message.
*
* @var string
*/
protected $notificationMessage;

/**
* An entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityManager;

/**
* A route matching interface.
*
* @var \Drupal\Core\Routing\ResettableStackedRouteMatchInterface
*/
protected $routeMatch;

/**
* An embargoes service.
*
* @var \Drupal\embargoes\EmbargoesEmbargoesServiceInterface
*/
protected $embargoes;

/**
* {@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('config.factory'),
$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');
$this->notificationMessage = $config_factory->get('embargoes.settings')->get('embargo_notification_message');
$this->entityManager = $entity_manager;
$this->embargoes = $embargoes;
$this->routeMatch = $route_match;
}

/**
* {@inheritdoc}
*/
public function build() {
$node = $this->routeMatch->getParameter('node');
if ($node instanceof NodeInterface) {
$embargoes = $this->embargoes->getCurrentEmbargoesByNids([$node->id()]);
$num_embargoes = count($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) {

$embargo = $this->entityManager->getStorage('embargoes_embargo_entity')->load($embargo_id);
$embargo_info = [];

// Expiration string.
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;
}
}
if (!$embargo_info['user_exempt']) {
$contact_message = $t->translate(
$this->notificationMessage,
['@contact' => $this->adminMail]
);
}

$embargo_info['dom_id'] = Html::getUniqueId('embargo_notification');
$embargoes_info[] = $embargo_info;

array_push(
$cache_tags,
"config:embargoes.embargoes_embargo_entity.{$embargo->id()}"
);

}

return [
'#theme' => 'embargoes_notifications',
'#count' => $embargoes_count,
'#message' => $contact_message,
'#embargo_info' => $embargoes_info,
'#cache' => [
'tags' => $cache_tags,
],
];
}
}

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'));
}

}
26 changes: 26 additions & 0 deletions templates/embargoes-notifications.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{#
/**
* @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.
* - 'message': Configured message to request access.
*/
#}

<span id='embargoes_embargo_policy_block_preamble' class='embargoes_embargo_policy_block'>{{ count }}</span>

<ul class='embargoes_embargo_policy_block embargoes_embargo_policy_block_list'>
{% for info in embargo_info %}
<li id='{{ info.dom_id }}' class='embargoes_embargo_policy_block embargoes_embargo_policy_block_item'>
<strong id='embargoes_embargo_policy_block_expiration_label' class='embargoes_embargo_policy_block embargoes_embargo_policy_block_item_label'>{{ info.type_message }}</strong>
<strong id='embargoes_embargo_policy_block_expiration_msg' class='embargoes_embargo_policy_block embargoes_embargo_policy_block_item_label'>{{ info.message }}</strong>
<hr id='embargoes_embargo_policy_block_separator' class='embargoes_embargo_policy_block'>
</li>
{% endfor %}
</ul>

<p class="embargoes_embargo_policy_block embargoes_embargo_policy_block_access_message">{{ message }}</p>

0 comments on commit cd50724

Please sign in to comment.