From f753d52f77946ca73a677581d0b97703b640e730 Mon Sep 17 00:00:00 2001 From: Igor Biki <ibiki@uwaterloo.ca> Date: Thu, 6 Oct 2022 18:31:20 -0400 Subject: [PATCH] ISTWCMS-5680: Adding webform pre save fix for ohana branch. --- .../UwWebformEventSubscriber.php | 53 +++++++++++++++++++ uw_cfg_common.services.yml | 4 ++ 2 files changed, 57 insertions(+) create mode 100644 src/EventSubscriber/UwWebformEventSubscriber.php diff --git a/src/EventSubscriber/UwWebformEventSubscriber.php b/src/EventSubscriber/UwWebformEventSubscriber.php new file mode 100644 index 00000000..d2486723 --- /dev/null +++ b/src/EventSubscriber/UwWebformEventSubscriber.php @@ -0,0 +1,53 @@ +<?php + +namespace Drupal\uw_cfg_common\EventSubscriber; + +use Drupal\core_event_dispatcher\EntityHookEvents; +use Drupal\core_event_dispatcher\Event\Entity\EntityPresaveEvent; +use Drupal\webform\WebformInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * Fixes roles permission for submission create for webforms. + */ +class UwWebformEventSubscriber implements EventSubscriberInterface { + + /** + * {@inheritDoc} + */ + public static function getSubscribedEvents(): array { + return [ + EntityHookEvents::ENTITY_PRE_SAVE => 'preSave', + ]; + } + + /** + * Entity pre save dispatched event. + * + * @param \Drupal\core_event_dispatcher\Event\Entity\EntityPresaveEvent $event + * Event object. + */ + public function preSave(EntityPresaveEvent $event): void { + $entity = $event->getEntity(); + + if ($entity->bundle() === 'webform') { + /** @var \Drupal\webform\Entity\Webform $entity */ + $access_rules = $entity->getAccessRules(); + + // Update webform submission access roles to be anonymous and + // authenticated. This will be done on EVERY webform save. + // Fixes migration issue with webforms. Once migration is updated and + // fixed, this code is no longer needed and can be removed. + $access_rules['create']['roles'] = [ + 'anonymous', + 'authenticated', + ]; + + $entity->setAccessRules($access_rules); + + // Setting default access denied handler. + $entity->setSetting('form_access_denied', WebformInterface::ACCESS_DENIED_PAGE); + } + } + +} diff --git a/uw_cfg_common.services.yml b/uw_cfg_common.services.yml index 28b61b84..f3b10415 100644 --- a/uw_cfg_common.services.yml +++ b/uw_cfg_common.services.yml @@ -45,3 +45,7 @@ services: uw_cfg_common.missing_blocks: class: Drupal\uw_cfg_common\Service\UWMissingBlocks arguments: ['@entity_type.manager', '@keyvalue.expirable', '@layout_builder.tempstore_repository'] + uw_cfg_common.webform.event_subscriber: + class: '\Drupal\uw_cfg_common\EventSubscriber\UwWebformEventSubscriber' + tags: + - { name: 'event_subscriber' } -- GitLab