Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch an actionEvent on creating a log #1574

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 86 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,96 @@ class SessionDataProcessor
private SessionInterface $session;
private RequestStack $requestStack;

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

private EntityManagerInterface $entityManager;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing doc block


/**
* @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);
}
}
Loading