Skip to content
Snippets Groups Projects
Commit 8973935e authored by Viktor Holovachek's avatar Viktor Holovachek Committed by Adriano Cori
Browse files

3398281 - Add permission to view links

parent 62332ad1
No related branches found
Tags 2.0.9
No related merge requests found
......@@ -7,3 +7,6 @@ prevnext.settings:
label: 'Enabled Node Types'
sequence:
type: string
prevnext_premission_check:
type: boolean
label: 'Use permission check to view PrevNext links'
......@@ -80,9 +80,15 @@ function prevnext_entity_extra_field_info() {
* Implements hook_ENTITY_TYPE_view().
*/
function prevnext_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
$config = \Drupal::config('prevnext.settings');
if ($config->get('prevnext_premission_check') &&
!\Drupal::currentUser()->hasPermission('view prevnext links')
) {
return;
}
// Checking if current node is configured for prevnext or not.
$config = \Drupal::config('prevnext.settings');
$enabled_nodetypes = $config->get('prevnext_enabled_nodetypes');
if (empty($enabled_nodetypes[$node->getType()])) {
return;
......
......@@ -2,3 +2,6 @@ administer prevnext:
title: 'Administer the PrevNext module.'
description: 'Administer the settings for the PrevNext module.'
restrict access: TRUE
view prevnext links:
title: 'View the PrevNext links'
description: 'Access to view the Previous and Next links on the node overview page'
......@@ -41,6 +41,12 @@ class PrevNextSettingsForm extends ConfigFormBase {
'#default_value' => !empty($config->get('prevnext_enabled_nodetypes')) ? $config->get('prevnext_enabled_nodetypes') : [],
];
$form['prevnext_premission_check'] = [
'#title' => $this->t('Use permission check to view PrevNext links'),
'#type' => 'checkbox',
'#default_value' => !empty($config->get('prevnext_premission_check')) ? $config->get('prevnext_premission_check') : FALSE,
];
return parent::buildForm($form, $form_state);
}
......@@ -51,6 +57,7 @@ class PrevNextSettingsForm extends ConfigFormBase {
// Save the config values.
$this->config('prevnext.settings')
->set('prevnext_enabled_nodetypes', $form_state->getValue('prevnext_enabled_nodetypes'))
->set('prevnext_premission_check', (bool) $form_state->getValue('prevnext_premission_check'))
->save();
Cache::invalidateTags(['entity_field_info']);
......
......@@ -8,6 +8,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\prevnext\PrevNextServiceInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
/**
* Block with the Previous and Next links.
......@@ -128,4 +130,19 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface
return $build;
}
/**
* {@inheritdoc}
*/
public function blockAccess(AccountInterface $account) {
$config = $this->configFactory->get('prevnext.settings');
if ($config->get('prevnext_premission_check') &&
!$account->hasPermission('view prevnext links')
) {
return AccessResult::forbidden();
}
return AccessResult::allowed();
}
}
......@@ -8,6 +8,7 @@ use Drupal\prevnext\PrevNextServiceInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\views\ResultRow;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
/**
* A handler to provide display for the Previous and Next links.
......@@ -32,6 +33,13 @@ class PrevNextLinks extends FieldPluginBase {
*/
protected $configFactory;
/**
* Returns the current_user service.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Constructs a new PrevNextLinks instance.
*
......@@ -45,12 +53,15 @@ class PrevNextLinks extends FieldPluginBase {
* Interface for the main PrevNext service file.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Defines the interface for a configuration object factory.
* @param \Drupal\Core\Session\AccountInterface $current_user
* Defines an account interface which represents the current user.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, PrevNextServiceInterface $prevnext, ConfigFactoryInterface $config_factory) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, PrevNextServiceInterface $prevnext, ConfigFactoryInterface $config_factory, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->prevnext = $prevnext;
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
}
/**
......@@ -62,7 +73,8 @@ class PrevNextLinks extends FieldPluginBase {
$plugin_id,
$plugin_definition,
$container->get('prevnext.service'),
$container->get('config.factory')
$container->get('config.factory'),
$container->get('current_user')
);
}
......@@ -91,6 +103,12 @@ class PrevNextLinks extends FieldPluginBase {
return $build;
}
if ($config->get('prevnext_premission_check') &&
!$this->currentUser->hasPermission('view prevnext links')
) {
return $build;
}
$previous_next = $this->prevnext->getPreviousNext($node);
$cache = [
'contexts' => [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment