Skip to content
Snippets Groups Projects
Commit a98fe613 authored by Chris Snyder's avatar Chris Snyder
Browse files

Issue #3133546 by ChrisSnyder, jnicola, gngn: Sitewide Alert Renders For...

Issue #3133546 by ChrisSnyder, jnicola, gngn: Sitewide Alert Renders For Anonymous Regardless of Permission
parent fd61b31b
Branches
Tags 2.0.0-beta2 2.0.0-rc1
1 merge request!22Issue #3133546: Sitewide Alert Renders For Anonymous Regardless of Permission
...@@ -6,7 +6,7 @@ use Drupal\Core\Block\BlockBase; ...@@ -6,7 +6,7 @@ use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\sitewide_alert\SitewideAlertRenderer; use Drupal\sitewide_alert\SitewideAlertRendererInterface;
/** /**
* Implements SiteAlertBlock class. * Implements SiteAlertBlock class.
...@@ -21,7 +21,7 @@ class SitewideAlertBlock extends BlockBase implements ContainerFactoryPluginInte ...@@ -21,7 +21,7 @@ class SitewideAlertBlock extends BlockBase implements ContainerFactoryPluginInte
/** /**
* The alert placeholder rendering service. * The alert placeholder rendering service.
* *
* @var \Drupal\sitewide_alert\SitewideAlertRender * @var \Drupal\sitewide_alert\SitewideAlertRendererInterface
*/ */
protected $renderer; protected $renderer;
...@@ -34,10 +34,10 @@ class SitewideAlertBlock extends BlockBase implements ContainerFactoryPluginInte ...@@ -34,10 +34,10 @@ class SitewideAlertBlock extends BlockBase implements ContainerFactoryPluginInte
* Block plugin id. * Block plugin id.
* @param mixed $plugin_definition * @param mixed $plugin_definition
* Block plugin configuration. * Block plugin configuration.
* @param \Drupal\sitewide_alert\SitewideAlertRenderer $renderer * @param \Drupal\sitewide_alert\SitewideAlertRendererInterface $renderer
* Alert placeholder rendering service. * Alert placeholder rendering service.
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, SitewideAlertRenderer $renderer) { public function __construct(array $configuration, $plugin_id, $plugin_definition, SitewideAlertRendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->renderer = $renderer; $this->renderer = $renderer;
} }
......
...@@ -5,14 +5,18 @@ ...@@ -5,14 +5,18 @@
* Install, update and uninstall functions for the sitewide_alert module. * Install, update and uninstall functions for the sitewide_alert module.
*/ */
use Drupal\Core\Session\AccountInterface; use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/** /**
* Implements hook_install(). * Implements hook_install().
*/ */
function sitewide_alert_install() { function sitewide_alert_install() {
// By default allow anonymous users to view Sitewide Alerts. // Grant the "view published sitewide alert entities" permission to all users by default.
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ['view published sitewide alert entities']); if (\Drupal::moduleHandler()->moduleExists('user')) {
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view published sitewide alert entities']);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view published sitewide alert entities']);
}
} }
/** /**
...@@ -50,6 +54,18 @@ function sitewide_alert_update_9001() { ...@@ -50,6 +54,18 @@ function sitewide_alert_update_9001() {
} }
} }
/**
* Add the 'view published sitewide alert entities' permission for roles that
* have the 'access content' permission.
*/
function sitewide_alert_update_9002() {
foreach (array_filter(Role::loadMultiple(), static fn($role) => $role->hasPermission('access content')) as $role) {
$role
->grantPermission('view published sitewide alert entities')
->save();
}
}
/** /**
* Rename the 'revision_log_message' revision metadata key in the entity type. * Rename the 'revision_log_message' revision metadata key in the entity type.
* *
......
...@@ -11,11 +11,6 @@ entity.sitewide_alert.collection: ...@@ -11,11 +11,6 @@ entity.sitewide_alert.collection:
entity.sitewide_alert.canonical: entity.sitewide_alert.canonical:
route_name: entity.sitewide_alert.canonical route_name: entity.sitewide_alert.canonical
base_route: entity.sitewide_alert.canonical base_route: entity.sitewide_alert.canonical
title: 'View'
entity.sitewide_alert.edit_form:
route_name: entity.sitewide_alert.edit_form
base_route: entity.sitewide_alert.canonical
title: 'Edit' title: 'Edit'
entity.sitewide_alert.version_history: entity.sitewide_alert.version_history:
......
...@@ -17,7 +17,8 @@ edit sitewide alert entities: ...@@ -17,7 +17,8 @@ edit sitewide alert entities:
title: 'Edit Sitewide Alert entities' title: 'Edit Sitewide Alert entities'
view published sitewide alert entities: view published sitewide alert entities:
title: 'View published Sitewide Alert entities' title: 'View published (active) Sitewide Alert entities'
description: 'This permission should be given to all roles that should be able to see active alerts.'
view unpublished sitewide alert entities: view unpublished sitewide alert entities:
title: 'View unpublished Sitewide Alert entities' title: 'View unpublished Sitewide Alert entities'
......
...@@ -4,7 +4,7 @@ sitewide_alert.sitewide_alerts_controller_load: ...@@ -4,7 +4,7 @@ sitewide_alert.sitewide_alerts_controller_load:
_controller: '\Drupal\sitewide_alert\Controller\SitewideAlertsController::load' _controller: '\Drupal\sitewide_alert\Controller\SitewideAlertsController::load'
_title: 'load' _title: 'load'
requirements: requirements:
_permission: 'access content' _permission: 'view published sitewide alert entities'
entity.sitewide_alert.config_form: entity.sitewide_alert.config_form:
path: '/admin/config/sitewide_alerts' path: '/admin/config/sitewide_alerts'
...@@ -15,3 +15,23 @@ entity.sitewide_alert.config_form: ...@@ -15,3 +15,23 @@ entity.sitewide_alert.config_form:
_permission: 'administer sitewide alert' _permission: 'administer sitewide alert'
options: options:
_admin_route: TRUE _admin_route: TRUE
entity.sitewide_alert.canonical:
path: '/admin/content/sitewide_alert/{sitewide_alert}'
defaults:
_entity_form: 'sitewide_alert.edit'
options:
_admin_route: TRUE
requirements:
_entity_access: 'sitewide_alert.update'
block_content: \d+
entity.sitewide_alert.edit_form:
path: '/admin/content/sitewide_alert/{sitewide_alert}'
defaults:
_entity_form: 'sitewide_alert.edit'
options:
_admin_route: TRUE
requirements:
_entity_access: 'sitewide_alert.update'
block_content: \d+
...@@ -4,4 +4,4 @@ services: ...@@ -4,4 +4,4 @@ services:
arguments: [ '@entity_type.manager', '@datetime.time' ] arguments: [ '@entity_type.manager', '@datetime.time' ]
sitewide_alert.sitewide_alert_renderer: sitewide_alert.sitewide_alert_renderer:
class: Drupal\sitewide_alert\SitewideAlertRenderer class: Drupal\sitewide_alert\SitewideAlertRenderer
arguments: [ '@config.factory', '@router.admin_context' ] arguments: [ '@config.factory', '@router.admin_context', '@current_user' ]
...@@ -148,25 +148,19 @@ class SitewideAlertController extends ControllerBase implements ContainerInjecti ...@@ -148,25 +148,19 @@ class SitewideAlertController extends ControllerBase implements ContainerInjecti
'#account' => $revision->getRevisionUser(), '#account' => $revision->getRevisionUser(),
]; ];
// Use revision link to revisions that are not active.
$date = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short');
if ($vid !== $sitewide_alert->getRevisionId()) {
$link = Link::fromTextAndUrl($date, new Url('entity.sitewide_alert.revision', [
'sitewide_alert' => $sitewide_alert->id(),
'sitewide_alert_revision' => $vid,
]))->toString();
}
else {
$link = $sitewide_alert->toLink($date)->toString();
}
$row = []; $row = [];
$column = [ $column = [
'data' => [ 'data' => [
'#type' => 'inline_template', '#type' => 'inline_template',
'#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}', '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
'#context' => [ '#context' => [
'date' => $link, 'date' => Link::fromTextAndUrl(
$this->dateFormatter->format($revision->getRevisionCreationTime(), 'short'),
new Url('entity.sitewide_alert.revision', [
'sitewide_alert' => $sitewide_alert->id(),
'sitewide_alert_revision' => $vid,
])
)->toString(),
'username' => $this->renderer->renderPlain($username), 'username' => $this->renderer->renderPlain($username),
'message' => [ 'message' => [
'#markup' => $revision->getRevisionLogMessage(), '#markup' => $revision->getRevisionLogMessage(),
......
...@@ -66,7 +66,7 @@ use Drupal\user\UserInterface; ...@@ -66,7 +66,7 @@ use Drupal\user\UserInterface;
* links = { * links = {
* "canonical" = "/admin/content/sitewide_alert/{sitewide_alert}", * "canonical" = "/admin/content/sitewide_alert/{sitewide_alert}",
* "add-form" = "/admin/content/sitewide_alert/add", * "add-form" = "/admin/content/sitewide_alert/add",
* "edit-form" = "/admin/content/sitewide_alert/{sitewide_alert}/edit", * "edit-form" = "/admin/content/sitewide_alert/{sitewide_alert}",
* "delete-form" = "/admin/content/sitewide_alert/{sitewide_alert}/delete", * "delete-form" = "/admin/content/sitewide_alert/{sitewide_alert}/delete",
* "version-history" = "/admin/content/sitewide_alert/{sitewide_alert}/revisions", * "version-history" = "/admin/content/sitewide_alert/{sitewide_alert}/revisions",
* "revision" = "/admin/content/sitewide_alert/{sitewide_alert}/revisions/{sitewide_alert_revision}/view", * "revision" = "/admin/content/sitewide_alert/{sitewide_alert}/revisions/{sitewide_alert_revision}/view",
......
...@@ -33,7 +33,7 @@ class SitewideAlertListBuilder extends EntityListBuilder { ...@@ -33,7 +33,7 @@ class SitewideAlertListBuilder extends EntityListBuilder {
/** @var \Drupal\sitewide_alert\Entity\SitewideAlert $entity */ /** @var \Drupal\sitewide_alert\Entity\SitewideAlert $entity */
$row['name'] = Link::createFromRoute( $row['name'] = Link::createFromRoute(
$entity->label(), $entity->label(),
'entity.sitewide_alert.edit_form', 'entity.sitewide_alert.canonical',
['sitewide_alert' => $entity->id()] ['sitewide_alert' => $entity->id()]
); );
$row['style'] = AlertStyleProvider::alertStyleName($entity->getStyle()); $row['style'] = AlertStyleProvider::alertStyleName($entity->getStyle());
......
...@@ -2,21 +2,22 @@ ...@@ -2,21 +2,22 @@
namespace Drupal\sitewide_alert; namespace Drupal\sitewide_alert;
use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\AdminContext; use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Session\AccountProxyInterface;
/** /**
* Sitewide Alert placeholder render array builder service. * Sitewide Alert placeholder render array builder service.
* *
* Used in default page_top display and by block submodule. * Used in default page_top display and by block submodule.
*/ */
class SitewideAlertRenderer { class SitewideAlertRenderer implements SitewideAlertRendererInterface {
/** /**
* Module configuration. * Module configuration.
* *
* @var \Drupal\Core\Config\ConfigInterface * @var \Drupal\Core\Config\ImmutableConfig
*/ */
protected $config; protected $config;
...@@ -27,33 +28,39 @@ class SitewideAlertRenderer { ...@@ -27,33 +28,39 @@ class SitewideAlertRenderer {
*/ */
protected $adminContext; protected $adminContext;
/**
* The current active user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
private AccountProxyInterface $currentUser;
/** /**
* Constructor. * Constructor.
* *
* @param \Drupal\Core\Config\ConfigFactory $configFactory * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory service. * Config factory service.
* @param \Drupal\Core\Routing\AdminContext $adminContext * @param \Drupal\Core\Routing\AdminContext $adminContext
* Admin context service. * Admin context service.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
*/ */
public function __construct(ConfigFactory $configFactory, AdminContext $adminContext) { public function __construct(ConfigFactoryInterface $configFactory, AdminContext $adminContext, AccountProxyInterface $currentUser) {
$this->config = $configFactory->get('sitewide_alert.settings'); $this->config = $configFactory->get('sitewide_alert.settings');
$this->adminContext = $adminContext; $this->adminContext = $adminContext;
$this->currentUser = $currentUser;
} }
/** /**
* Construct the sitewide-alert placeholder render array. * {@inheritdoc}
*
* @param bool $adminAware
* Used to indicate a non-block context, where showing the alert on
* admin pages is configurable. When using blocks, block placement is
* per theme so this setting is ignored.
*/ */
public function build(bool $adminAware = TRUE) : array { public function build(bool $adminAware = TRUE): array {
$cacheMetadata = CacheableMetadata::createFromObject($this->config); $cacheMetadata = CacheableMetadata::createFromObject($this->config)
->addCacheContexts(['user.permissions']);
// Do not show alert on admin pages if we are not configured to do so. // Do not show alert on admin pages if we are not configured to do so or when we don't have enough permissions.
if ($adminAware && !$this->config->get('show_on_admin') && if (!$this->currentUser->hasPermission('view published sitewide alert entities')
$this->adminContext->isAdminRoute()) { || ($adminAware && !$this->config->get('show_on_admin') && $this->adminContext->isAdminRoute())) {
// Populate an empty render array with cache-metadata to force it to // Populate an empty render array with cache-metadata to force it to
// invalidate when settings change. // invalidate when settings change.
$build = []; $build = [];
......
<?php
declare(strict_types=1);
namespace Drupal\sitewide_alert;
/**
* Sitewide Alert placeholder render array builder service.
*
* Used in default page_top display and by block submodule.
*/
interface SitewideAlertRendererInterface {
/**
* Construct the sitewide-alert placeholder render array.
*
* @param bool $adminAware
* Used to indicate a non-block context, where showing the alert on
* admin pages is configurable. When using blocks, block placement is
* per theme so this setting is ignored.
*/
public function build(bool $adminAware = TRUE): array;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment