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'
|
event: 'postPersist'
|
||||||
entity: 'App\Entity\JobOrder'
|
entity: 'App\Entity\JobOrder'
|
||||||
|
|
||||||
App\EventListener\EntityListener\UserEntityListener:
|
App\EventListener\EntityListener:
|
||||||
arguments:
|
arguments:
|
||||||
$token_storage: "@security.token_storage"
|
$token_storage: "@security.token_storage"
|
||||||
$log_db: '@influxdb_database'
|
$log_db: '@influxdb_database'
|
||||||
|
$entities: ['App\Entity\User', 'App\Entity\Role']
|
||||||
tags:
|
tags:
|
||||||
- name: 'doctrine.event_listener'
|
- name: 'doctrine.event_listener'
|
||||||
event: 'onFlush'
|
event: 'onFlush'
|
||||||
entity: 'App\Entity\User'
|
- name: 'doctrine.event_listener'
|
||||||
- name: 'doctrine.orm.entity_listener'
|
|
||||||
event: 'postPersist'
|
event: 'postPersist'
|
||||||
entity: 'App\Entity\User'
|
|
||||||
|
|
|
||||||
|
|
@ -25,4 +25,18 @@ class Role extends BaseRole
|
||||||
{
|
{
|
||||||
parent::__construct();
|
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 InfluxDB\Point;
|
||||||
|
|
||||||
use DateTime;
|
class EntityListener
|
||||||
|
|
||||||
abstract class EntityListener
|
|
||||||
{
|
{
|
||||||
protected $token_storage;
|
protected $token_storage;
|
||||||
protected $log_db;
|
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->token_storage = $token_storage;
|
||||||
$this->log_db = $log_db;
|
$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();
|
||||||
|
|
||||||
|
$object_class = get_class($object);
|
||||||
|
|
||||||
|
if (isset($this->entities[$object_class]))
|
||||||
|
{
|
||||||
// get user id for logging
|
// get user id for logging
|
||||||
$user = $this->token_storage->getToken()->getUser();
|
$user = $this->token_storage->getToken()->getUser();
|
||||||
//$username = $user->getUsername();
|
|
||||||
$user_id = $user->getID();
|
$user_id = $user->getID();
|
||||||
|
|
||||||
// get entity name
|
// get entity name
|
||||||
$obj_class = preg_replace('/.*\\\\/', '', get_class($object));
|
//$obj_class = preg_replace('/.*\\\\/', '', $object_class);
|
||||||
|
|
||||||
// get fields of object
|
// get fields of object
|
||||||
$fields = $object->fieldDump();
|
$fields = $object->fieldDump();
|
||||||
|
|
||||||
$this->writeToLogDB($obj_class, $object->getID(), 'create', $fields, $user_id);
|
$this->writeToLogDB($object_class, $object->getID(), 'create', $fields, $user_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onFlush(OnFlushEventArgs $args)
|
public function onFlush(OnFlushEventArgs $args)
|
||||||
|
|
@ -62,12 +73,13 @@ abstract class EntityListener
|
||||||
|
|
||||||
// get updated fields. This doesn't include lists of other entities.
|
// get updated fields. This doesn't include lists of other entities.
|
||||||
foreach ($updated_entities as $updated_entity)
|
foreach ($updated_entities as $updated_entity)
|
||||||
|
{
|
||||||
|
// check if entity is in list of entities to log
|
||||||
|
$entity_class = get_class($updated_entity);
|
||||||
|
if (isset($this->entities[$entity_class]))
|
||||||
{
|
{
|
||||||
// get entity name
|
// get entity name
|
||||||
$obj_class = preg_replace('/.*\\\\/', '', get_class($updated_entity));
|
//$obj_class = preg_replace('/.*\\\\/', '', $entity_class);
|
||||||
$log_message = $obj_class . ' with id ' . $updated_entity->getID() . ' updated by ' . $user_id;
|
|
||||||
|
|
||||||
error_log($log_message);
|
|
||||||
|
|
||||||
$changeset = $unit_of_work->getEntityChangeSet($updated_entity);
|
$changeset = $unit_of_work->getEntityChangeSet($updated_entity);
|
||||||
|
|
||||||
|
|
@ -87,31 +99,28 @@ abstract class EntityListener
|
||||||
|
|
||||||
$fields = json_encode($field_changes);
|
$fields = json_encode($field_changes);
|
||||||
|
|
||||||
$this->writeToLogDB($obj_class, $updated_entity->getID(), 'update', $fields, $user_id);
|
$this->writeToLogDB($entity_class, $updated_entity->getID(), 'update', $fields, $user_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get deleted objects
|
// get deleted objects
|
||||||
foreach ($deleted_entities as $deleted_entity)
|
foreach ($deleted_entities as $deleted_entity)
|
||||||
|
{
|
||||||
|
// 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
|
// get entity name
|
||||||
$obj_class = preg_replace('/.*\\\\/', '', get_class($deleted_entity));
|
//$obj_class = preg_replace('/.*\\\\/', '', get_class($deleted_entity));
|
||||||
$log_message = $obj_class . ' with id ' . $deleted_entity->getID() . ' deleted by ' . $user_id;
|
|
||||||
|
|
||||||
error_log($log_message);
|
|
||||||
|
|
||||||
$deleted_id = $deleted_entity->getID();
|
$deleted_id = $deleted_entity->getID();
|
||||||
|
|
||||||
// get fields of deleted entity
|
// get fields of deleted entity
|
||||||
$fields = $deleted_entity->fieldDump();
|
$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
|
// 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
|
// 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