<?php
namespace Drupal\your_module\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
class YourEventSubscriber implements EventSubscriberInterface {
const OPERATION_DISPLAY_SUBMISSIONS = 'Display submission list';
const OPERATION_DOWNLOAD_SUBMISSIONS = 'Download submissions in CSV';
const OPERATION_DELETE_SUBMISSIONS = 'Delete submissions';
protected $routeProvider;
protected $accessManager;
protected $currentUser;
public function __construct(RouteProviderInterface $routeProvider, AccessManagerInterface $accessManager, AccountProxyInterface $currentUser) {
$this->routeProvider = $routeProvider;
$this->accessManager = $accessManager;
$this->currentUser = $currentUser;
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = ['onKernelRequest'];
return $events;
}
public function onKernelRequest(RequestEvent $event) {
$request = $event->getRequest();
$current_path = $request->getPathInfo();
$url = Url::fromUserInput($current_path);
$route_parameters = $url->isRouted() ? $url->getRouteParameters() : [];
if (!empty($route_parameters['webform'])) {
$access_result = $this->accessManager->checkNamedRoute($url->getRouteName(), $url->getRouteParameters(), $this->currentUser, TRUE);
if ($access_result) {
if (strpos($current_path, '/admin/structure/webform/manage/') === 0 && strpos($current_path, '/results/submissions') !== false) {
$this->logAccess(self::OPERATION_DISPLAY_SUBMISSIONS, $route_parameters);
}
if (strpos($current_path, '/admin/structure/webform/manage/') === 0 && strpos($current_path, '/results/download') !== false) {
$this->logAccess(self::OPERATION_DOWNLOAD_SUBMISSIONS, $route_parameters);
}
if (strpos($current_path, '/admin/structure/webform/manage/') === 0 && strpos($current_path, '/results/clear') !== false) {
$this->logAccess(self::OPERATION_DELETE_SUBMISSIONS, $route_parameters);
}
}
}
}
protected function logAccess($operation, array $routeParameters) {
$log_entry = [
'uid' => $this->currentUser->id(),
'operation' => $operation,
'webform_id' => $routeParameters['webform'],
'timestamp' => \Drupal::time()->getRequestTime(),
];
\Drupal::database()->insert('kumanichi_webform_operation_log_access_log')
->fields($log_entry)
->execute();
}
}