This repository has been archived on 2022-07-12. You can view files and clone it, but cannot push or open issues or pull requests.
opencase/src/EventSubscriber/OpencaseSubscriber.php

59 lines
1.8 KiB
PHP

<?php
namespace Drupal\opencase\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\Core\Url;
/**
* Class OpencaseSubscriber.
*
* @package Drupal\opencase\EventSubscriber
*/
class OpencaseSubscriber implements EventSubscriberInterface {
private $alternativeHomePage = '/opencase/cases/all';
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = ['onRequest'];
return $events;
}
/**
* Manipulates the request object.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The Event to process.
*/
public function onRequest(GetResponseEvent $event): void {
if ($this->requestIsForHomePage($event) && $this->currentUserShouldBeDirectedToAlternativeHomePage()){
$this->redirectToAlternativeHomePage($event);
}
}
private function requestIsForHomePage($event):bool {
return $event->getRequest()->getRequestUri() == '/';
}
private function currentUserShouldBeDirectedToAlternativeHomePage():bool {
return $this->currentUserCanSeeAllCases();
}
private function currentUserCanSeeAllCases():bool {
return \Drupal::currentUser()->hasPermission('view published case entities');
}
private function redirectToAlternativeHomePage(GetResponseEvent $event):void {
$redirect_target_url = Url::fromUserInput($this->alternativeHomePage);
$response = new RedirectResponse($redirect_target_url->setAbsolute()->toString());
$event->setResponse($response);
}
}