From 45ef6a9ea3ebf6171a07cea9dda113fffad67a90 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 28 Mar 2024 11:29:49 +0100 Subject: [PATCH] feat: Separate home page Refs: #OPS-10197 --- composer.lock | 2 +- config/views.view.my_documents.yml | 2 +- .../ocha_ai_summarize.services.yml | 6 + .../AnonymousFrontpageSubscriber.php | 128 ++++++++++++++++++ 4 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 html/modules/custom/ocha_ai_summarize/ocha_ai_summarize.services.yml create mode 100644 html/modules/custom/ocha_ai_summarize/src/EventSubscriber/AnonymousFrontpageSubscriber.php diff --git a/composer.lock b/composer.lock index 879393c..f73f6e6 100644 --- a/composer.lock +++ b/composer.lock @@ -16120,5 +16120,5 @@ "php": ">=8.1" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/config/views.view.my_documents.yml b/config/views.view.my_documents.yml index 398ef80..4b9930d 100644 --- a/config/views.view.my_documents.yml +++ b/config/views.view.my_documents.yml @@ -269,7 +269,7 @@ display: plugin_id: text_custom label: '' empty: true - content: "

Welcome to OCHA’s internal AI-based document summarization tool. This proof-of-concept is only available to OCHA personnel.

\r\n\r\n

Log in

" + content: "

Welcome to OCHA’s internal AI-based document summarization tool.

\r\n\r\n

Add your first document.

" tokenize: false title: id: title diff --git a/html/modules/custom/ocha_ai_summarize/ocha_ai_summarize.services.yml b/html/modules/custom/ocha_ai_summarize/ocha_ai_summarize.services.yml new file mode 100644 index 0000000..fd51f90 --- /dev/null +++ b/html/modules/custom/ocha_ai_summarize/ocha_ai_summarize.services.yml @@ -0,0 +1,6 @@ +services: + ocha_ai_summarize.event_subscriber: + class: Drupal\ocha_ai_summarize\EventSubscriber\AnonymousFrontpageSubscriber + arguments: ['@messenger','@current_user','@path.matcher', '@config.factory', '@state'] + tags: + - { name: event_subscriber } diff --git a/html/modules/custom/ocha_ai_summarize/src/EventSubscriber/AnonymousFrontpageSubscriber.php b/html/modules/custom/ocha_ai_summarize/src/EventSubscriber/AnonymousFrontpageSubscriber.php new file mode 100644 index 0000000..30834cf --- /dev/null +++ b/html/modules/custom/ocha_ai_summarize/src/EventSubscriber/AnonymousFrontpageSubscriber.php @@ -0,0 +1,128 @@ +messenger = $messenger; + $this->currentUser = $currentUser; + $this->pathMatcher = $pathMatcher; + $this->configFactory = $configFactory; + $this->state = $state; + } + + /** + * Kernel request event handler. + * + * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event + * Response event. + */ + public function onKernelRequest(RequestEvent $event) { + // Make sure front page module is not run when using cli (drush). + // Make sure front page module does not run when installing Drupal either. + if (PHP_SAPI === 'cli' || InstallerKernel::installationAttempted()) { + return; + } + + // Don't run when site is in maintenance mode. + if ($this->state->get('system.maintenance_mode')) { + return; + } + + // Ignore non index.php requests (like cron). + if (!empty($_SERVER['SCRIPT_FILENAME']) + && realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) { + return; + } + + // Ignore authenticated users. + if ($this->currentUser->isAuthenticated()) { + return; + } + + if ($this->pathMatcher->isFrontPage()) { + $url_object = Url::fromUserInput('/home'); + $event->setResponse(new RedirectResponse($url_object->toString())); + } + + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + return [ + KernelEvents::REQUEST => ['onKernelRequest'], + ]; + } + +}