Skip to content

Commit

Permalink
Update BNF server content view permissions.
Browse files Browse the repository at this point in the history
This view should only be available if the `bnf_server`
module is enabled.
  • Loading branch information
rasben committed Jan 13, 2025
1 parent 07d90ab commit e8c4110
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/sync/user.role.anonymous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ permissions:
- 'view eventinstance entity'
- 'view eventseries entity'
- 'view media'
- 'view the administration theme'
1 change: 1 addition & 0 deletions config/sync/user.role.authenticated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ permissions:
- 'view eventinstance entity'
- 'view eventseries entity'
- 'view media'
- 'view the administration theme'
9 changes: 3 additions & 6 deletions config/sync/views.view.importable_content.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ dependencies:
- taxonomy.vocabulary.categories
- taxonomy.vocabulary.tags
module:
- dpl_admin
- link
- media
- node
- taxonomy
- user
id: importable_content
label: 'Importable content'
module: views
Expand Down Expand Up @@ -657,9 +657,8 @@ display:
sort_asc_label: Asc
sort_desc_label: Desc
access:
type: perm
options:
perm: 'access content overview'
type: bnf_server_access
options: { }
cache:
type: tag
options: { }
Expand Down Expand Up @@ -1044,7 +1043,6 @@ display:
- url
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags:
- 'config:field.storage.node.field_canonical_url'
- 'config:field.storage.node.field_subtitle'
Expand All @@ -1066,7 +1064,6 @@ display:
- url
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags:
- 'config:field.storage.node.field_canonical_url'
- 'config:field.storage.node.field_subtitle'
Expand Down
8 changes: 8 additions & 0 deletions web/modules/custom/dpl_admin/dpl_admin.services.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
---
services:
dpl_admin.version_helper:
class: Drupal\dpl_admin\Services\VersionHelper
dpl_admin.bnf_server_access:
class: Drupal\dpl_admin\Plugin\views\access\BnfServerEnabledAccess
arguments:
$configuration: []
$plugin_id: 'dpl_admin.bnf_server_access'
$plugin_definition: []
$module_handler: '@module_handler'
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Drupal\dpl_admin\Plugin\views\access;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Psr\Container\ContainerInterface;
use Symfony\Component\Routing\Route;

/**
* A custom access handler, that returns Allowed if bnf_server is enabled.
*
* This is necessary as we have views and content that should only be available
* on the BNF server.
*
* @ingroup views_access_plugins
*
* @ViewsAccess(
* id = "bnf_server_access",
* title = @Translation("Allowed if bnf_server is enabled.")
* )
*/
class BnfServerEnabledAccess extends AccessPluginBase {

/**
* The module handler.
*/
protected ModuleHandlerInterface $moduleHandler;

/**
* {@inheritDoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
}

/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('module_handler'),
);
}

/**
* {@inheritDoc}
*/
public function access(AccountInterface $account): bool {
return $this->moduleHandler->moduleExists('bnf_server');
}

/**
* {@inheritDoc}
*/
public function accessRoute(AccountInterface $account): AccessResult {
return AccessResult::allowedIf($this->access($account));
}

/**
* {@inheritDoc}
*/
public function alterRouteDefinition(Route $route): void {
$route->setRequirement('_custom_access', 'dpl_admin.bnf_server_access:accessRoute');
}

}

0 comments on commit e8c4110

Please sign in to comment.