Skip to content

Commit

Permalink
Merge pull request #1574 from ConductionNL/feature/IM-83/trigger-acti…
Browse files Browse the repository at this point in the history
…on-on-log

Dispatch an actionEvent on creating a log
  • Loading branch information
rjzondervan authored Oct 24, 2023
2 parents ff556ed + b301d8b commit beb6c06
Showing 1 changed file with 89 additions and 17 deletions.
106 changes: 89 additions & 17 deletions api/src/Logger/SessionDataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Logger;

use App\Event\ActionEvent;
use Doctrine\ORM\EntityManagerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

Expand All @@ -10,30 +13,99 @@ class SessionDataProcessor
private SessionInterface $session;
private RequestStack $requestStack;

public function __construct(SessionInterface $session, RequestStack $requestStack)
/**
* @var EventDispatcherInterface The event dispatcher.
*/
private EventDispatcherInterface $eventDispatcher;

/**
* @var EntityManagerInterface The entity manager.
*/
private EntityManagerInterface $entityManager;

/**
* @param SessionInterface $session
* @param RequestStack $requestStack
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
SessionInterface $session,
RequestStack $requestStack,
EventDispatcherInterface $eventDispatcher,
EntityManagerInterface $entityManager
)
{
$this->session = $session;
$this->requestStack = $requestStack;
$this->eventDispatcher = $eventDispatcher;
$this->entityManager = $entityManager;
}

public function __invoke(array $record): array
/**
* Update the context with data from the session and the request stack.
*
* @param array $context The context to update.
*
* @return array The updated context.
*/
public function updateContext($context): array
{
$context['session'] = $this->session->getId();
$context['process'] = $this->session->has('process') ? $this->session->get('process') : '';
$context['endpoint'] = $this->session->has('endpoint') ? $this->session->get('endpoint') : '';
$context['schema'] = $this->session->has('schema') ? $this->session->get('schema') : '';
$context['object'] = $this->session->has('object') === true ? $this->session->get('object') : '';
$context['cronjob'] = $this->session->has('cronjob') ? $this->session->get('cronjob') : '';
$context['action'] = $this->session->has('cronjob') ? $this->session->get('action') : '';
$context['mapping'] = $this->session->has('mapping') ? $this->session->get('mapping') : '';
$context['source'] = $this->session->has('source') ? $this->session->get('source') : '';
$context['plugin'] = isset($record['data']['plugin']) === true ? $record['data']['plugin'] : '';
$context['user'] = $this->session->has('user') ? $this->session->get('user') : '';
$context['organization'] = $this->session->has('organization') ? $this->session->get('organization') : '';
$context['application'] = $this->session->has('application') ? $this->session->get('application') : '';
$context['host'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getHost() : '';
$context['ip'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getClientIp() : '';

return $context;
}

/**
* Dispatches a log create action.
*
* @param array $record The log record that is created.
*
* @return array The resulting log record after the action.
*/
public function dispatchLogCreateAction(array $record): array
{
$record['context']['session'] = $this->session->getId();
$record['context']['process'] = $this->session->has('process') ? $this->session->get('process') : '';
$record['context']['endpoint'] = $this->session->has('endpoint') ? $this->session->get('endpoint') : '';
$record['context']['schema'] = $this->session->has('schema') ? $this->session->get('schema') : '';
$record['context']['object'] = $this->session->has('object') === true ? $this->session->get('object') : '';
$record['context']['cronjob'] = $this->session->has('cronjob') ? $this->session->get('cronjob') : '';
$record['context']['action'] = $this->session->has('cronjob') ? $this->session->get('action') : '';
$record['context']['mapping'] = $this->session->has('mapping') ? $this->session->get('mapping') : '';
$record['context']['source'] = $this->session->has('source') ? $this->session->get('source') : '';
$record['context']['plugin'] = isset($record['data']['plugin']) === true ? $record['data']['plugin'] : '';
$record['context']['user'] = $this->session->has('user') ? $this->session->get('user') : '';
$record['context']['organization'] = $this->session->has('organization') ? $this->session->get('organization') : '';
$record['context']['application'] = $this->session->has('application') ? $this->session->get('application') : '';
$record['context']['host'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getHost() : '';
$record['context']['ip'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getClientIp() : '';
if ($this->entityManager->getConnection()->isConnected() === true
&& in_array(
$this->entityManager->getConnection()->getDatabase(),
$this->entityManager->getConnection()->getSchemaManager()->listDatabases()
) === true
&& $this->entityManager->getConnection()->getSchemaManager()->tablesExist('action') === true
){
$event = new ActionEvent('commongateway.action.event', $record, 'core.log.create');

$this->eventDispatcher->dispatch($event, 'commongateway.action.event');

$record = $event->getData();
}

return $record;
}

/**
* Updates the log record with data from the session, request and from actions.
*
* @param array $record The log record.
*
* @return array The updated log record.
*/
public function __invoke(array $record): array
{
$record['context'] = $this->updateContext($record['context']);

return $this->dispatchLogCreateAction($record);
}
}

0 comments on commit beb6c06

Please sign in to comment.