Reworked the entity listener. #330
This commit is contained in:
parent
e305dc4c16
commit
b743d202fb
4 changed files with 77 additions and 77 deletions
|
|
@ -267,14 +267,13 @@ services:
|
|||
event: 'postPersist'
|
||||
entity: 'App\Entity\JobOrder'
|
||||
|
||||
App\EventListener\EntityListener\UserEntityListener:
|
||||
App\EventListener\EntityListener:
|
||||
arguments:
|
||||
$token_storage: "@security.token_storage"
|
||||
$log_db: '@influxdb_database'
|
||||
$entities: ['App\Entity\User', 'App\Entity\Role']
|
||||
tags:
|
||||
- name: 'doctrine.event_listener'
|
||||
event: 'onFlush'
|
||||
entity: 'App\Entity\User'
|
||||
- name: 'doctrine.orm.entity_listener'
|
||||
- name: 'doctrine.event_listener'
|
||||
event: 'postPersist'
|
||||
entity: 'App\Entity\User'
|
||||
|
|
|
|||
|
|
@ -25,4 +25,18 @@ class Role extends BaseRole
|
|||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function fieldDump()
|
||||
{
|
||||
// get all fields and their values into an array
|
||||
$field_array = [
|
||||
'id' => $this->getID(),
|
||||
'name' => $this->getName(),
|
||||
'acl_attributes' => $this->getACLAttributes(),
|
||||
];
|
||||
|
||||
$fields = json_encode($field_array);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,35 +9,46 @@ use Doctrine\ORM\Event\OnFlushEventArgs;
|
|||
|
||||
use InfluxDB\Point;
|
||||
|
||||
use DateTime;
|
||||
|
||||
abstract class EntityListener
|
||||
class EntityListener
|
||||
{
|
||||
protected $token_storage;
|
||||
protected $log_db;
|
||||
protected $entities;
|
||||
|
||||
public function __construct(TokenStorageInterface $token_storage, $log_db)
|
||||
public function __construct(TokenStorageInterface $token_storage, $log_db,
|
||||
$entities)
|
||||
{
|
||||
$this->token_storage = $token_storage;
|
||||
$this->log_db = $log_db;
|
||||
|
||||
// reconstruct entity array with class name as keys
|
||||
// with values of true
|
||||
foreach ($entities as $entity)
|
||||
{
|
||||
$this->entities[$entity] = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function postPersist($object, LifecycleEventArgs $args)
|
||||
public function postPersist(LifecycleEventArgs $args)
|
||||
{
|
||||
//$event_time = new DateTime();
|
||||
$object = $args->getObject();
|
||||
|
||||
// get user id for logging
|
||||
$user = $this->token_storage->getToken()->getUser();
|
||||
//$username = $user->getUsername();
|
||||
$user_id = $user->getID();
|
||||
$object_class = get_class($object);
|
||||
|
||||
// get entity name
|
||||
$obj_class = preg_replace('/.*\\\\/', '', get_class($object));
|
||||
if (isset($this->entities[$object_class]))
|
||||
{
|
||||
// get user id for logging
|
||||
$user = $this->token_storage->getToken()->getUser();
|
||||
$user_id = $user->getID();
|
||||
|
||||
// get fields of object
|
||||
$fields = $object->fieldDump();
|
||||
// get entity name
|
||||
//$obj_class = preg_replace('/.*\\\\/', '', $object_class);
|
||||
|
||||
$this->writeToLogDB($obj_class, $object->getID(), 'create', $fields, $user_id);
|
||||
// get fields of object
|
||||
$fields = $object->fieldDump();
|
||||
|
||||
$this->writeToLogDB($object_class, $object->getID(), 'create', $fields, $user_id);
|
||||
}
|
||||
}
|
||||
|
||||
public function onFlush(OnFlushEventArgs $args)
|
||||
|
|
@ -63,56 +74,54 @@ abstract class EntityListener
|
|||
// get updated fields. This doesn't include lists of other entities.
|
||||
foreach ($updated_entities as $updated_entity)
|
||||
{
|
||||
// get entity name
|
||||
$obj_class = preg_replace('/.*\\\\/', '', get_class($updated_entity));
|
||||
$log_message = $obj_class . ' with id ' . $updated_entity->getID() . ' updated by ' . $user_id;
|
||||
|
||||
error_log($log_message);
|
||||
|
||||
$changeset = $unit_of_work->getEntityChangeSet($updated_entity);
|
||||
|
||||
$entity_fields = array_keys($changeset);
|
||||
foreach ($entity_fields as $field)
|
||||
// check if entity is in list of entities to log
|
||||
$entity_class = get_class($updated_entity);
|
||||
if (isset($this->entities[$entity_class]))
|
||||
{
|
||||
$values = $changeset[$field];
|
||||
// get entity name
|
||||
//$obj_class = preg_replace('/.*\\\\/', '', $entity_class);
|
||||
|
||||
$old_value = $values[0];
|
||||
$new_value = $values[1];
|
||||
$changeset = $unit_of_work->getEntityChangeSet($updated_entity);
|
||||
|
||||
$field_changes[$field] = [
|
||||
'old_value' => $values[0],
|
||||
'new_value' => $values[1],
|
||||
];
|
||||
$entity_fields = array_keys($changeset);
|
||||
foreach ($entity_fields as $field)
|
||||
{
|
||||
$values = $changeset[$field];
|
||||
|
||||
$old_value = $values[0];
|
||||
$new_value = $values[1];
|
||||
|
||||
$field_changes[$field] = [
|
||||
'old_value' => $values[0],
|
||||
'new_value' => $values[1],
|
||||
];
|
||||
}
|
||||
|
||||
$fields = json_encode($field_changes);
|
||||
|
||||
$this->writeToLogDB($entity_class, $updated_entity->getID(), 'update', $fields, $user_id);
|
||||
}
|
||||
|
||||
$fields = json_encode($field_changes);
|
||||
|
||||
$this->writeToLogDB($obj_class, $updated_entity->getID(), 'update', $fields, $user_id);
|
||||
}
|
||||
|
||||
// get deleted objects
|
||||
foreach ($deleted_entities as $deleted_entity)
|
||||
{
|
||||
// get entity name
|
||||
$obj_class = preg_replace('/.*\\\\/', '', get_class($deleted_entity));
|
||||
$log_message = $obj_class . ' with id ' . $deleted_entity->getID() . ' deleted by ' . $user_id;
|
||||
// check if entity is in list of entities to log
|
||||
$entity_class = get_class($deleted_entity);
|
||||
if (isset($this->entities[$entity_class]))
|
||||
{
|
||||
// get entity name
|
||||
//$obj_class = preg_replace('/.*\\\\/', '', get_class($deleted_entity));
|
||||
|
||||
error_log($log_message);
|
||||
$deleted_id = $deleted_entity->getID();
|
||||
|
||||
$deleted_id = $deleted_entity->getID();
|
||||
// get fields of deleted entity
|
||||
$fields = $deleted_entity->fieldDump();
|
||||
|
||||
// get fields of deleted entity
|
||||
$fields = $deleted_entity->fieldDump();
|
||||
|
||||
$this->writeToLogDB($obj_class, $deleted_id, 'delete', $fields, $user_id);
|
||||
$this->writeToLogDB($entity_class, $deleted_id, 'delete', $fields, $user_id);
|
||||
}
|
||||
}
|
||||
|
||||
//$this->log_db->writePoints([new Point(
|
||||
// 'User',
|
||||
// 'Action',
|
||||
// ['status' => 'success']
|
||||
//)]);
|
||||
|
||||
// TODO: find a way to get the list of "old" items, compare with list of updated items
|
||||
// so we can find out what was added/removed
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\EventListener\EntityListener;
|
||||
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
|
||||
use App\EventListener\EntityListener;
|
||||
|
||||
class UserEntityListener extends EntityListener
|
||||
{
|
||||
protected $token_storage;
|
||||
protected $log_db;
|
||||
|
||||
public function __construct(TokenStorageInterface $token_storage, $log_db)
|
||||
{
|
||||
$this->token_storage = $token_storage;
|
||||
$this->log_db = $log_db;
|
||||
|
||||
parent::__construct($token_storage, $log_db);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue