diff --git a/src/Form/UwContentModerationForm.php b/src/Form/UwContentModerationForm.php index b6613f17ee84e993b042d0f9e1c9ecdd34fea2cd..d7b8876cf890ab8883ea8ea067afa85f0a08c304 100644 --- a/src/Form/UwContentModerationForm.php +++ b/src/Form/UwContentModerationForm.php @@ -2,10 +2,13 @@ namespace Drupal\uw_cfg_common\Form; +use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +use Drupal\uw_cfg_common\Service\UWService; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -54,6 +57,19 @@ class UwContentModerationForm extends ConfirmFormBase { ); } + /** + * Access callback. No access for the home page, except by admin. + * + * @return object + * A AccessResult object. + */ + public function access(int $nid, AccountInterface $account): AccessResult { + if (UWService::nodeIsHomePage($nid) && !$account->hasPermission('bypass home page protection')) { + return AccessResult::forbidden(); + } + return AccessResult::allowed(); + } + /** * {@inheritdoc} */ diff --git a/src/Service/UWService.php b/src/Service/UWService.php index 362e36d142a7392ee06ea4bede710087997afe4f..e9369108e62939a6034287d757a9eb1db4292319 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -368,4 +368,19 @@ class UWService implements UWServiceInterface { } } + /** + * Determine whether a node is the home page. + * + * @param int $nid + * A node ID. + * + * @return bool + * TRUE when the node is the home page, FALSE otherwise. + */ + public static function nodeIsHomePage(int $nid): bool { + $front_page_path = \Drupal::configFactory()->get('system.site')->get('page.front'); + $node_alias = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $nid); + return $front_page_path === $node_alias; + } + } diff --git a/uw_cfg_common.permissions.yml b/uw_cfg_common.permissions.yml index a9fef77cb710799722ef0647b5417aecf19d8f58..35b905c1b759a6d4b603e450698e186d428aa0eb 100644 --- a/uw_cfg_common.permissions.yml +++ b/uw_cfg_common.permissions.yml @@ -1,3 +1,7 @@ 'access content access form': title: 'Access content access form' description: 'Allows access to the content access form.' +'bypass home page protection': + title: 'Bypass home page protection' + description: 'Allows taking actions that are not normally allowed for the home page, such as unpublishing.' + restrict access: true diff --git a/uw_cfg_common.routing.yml b/uw_cfg_common.routing.yml index cd86dba516fdd844199f59060c7fa1140128fc75..fe107ffc729639d3662c62f5f477297db57ff194 100644 --- a/uw_cfg_common.routing.yml +++ b/uw_cfg_common.routing.yml @@ -11,4 +11,5 @@ uw_content_moderation.form: _title: 'Content moderation' _form: '\Drupal\uw_cfg_common\Form\UwContentModerationForm' requirements: + _custom_access: '\Drupal\uw_cfg_common\Form\UwContentModerationForm::access' _permission: 'access content'