Add AuditableEntity interface. #330
This commit is contained in:
parent
b743d202fb
commit
d7806a2664
4 changed files with 53 additions and 34 deletions
8
src/Entity/AuditableEntity.php
Normal file
8
src/Entity/AuditableEntity.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Entity;
|
||||||
|
|
||||||
|
interface AuditableEntity
|
||||||
|
{
|
||||||
|
public function fieldDump();
|
||||||
|
}
|
||||||
|
|
@ -14,7 +14,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
* @UniqueEntity("id")
|
* @UniqueEntity("id")
|
||||||
* @UniqueEntity("name")
|
* @UniqueEntity("name")
|
||||||
*/
|
*/
|
||||||
class Role extends BaseRole
|
class Role extends BaseRole implements AuditableEntity
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles", fetch="EXTRA_LAZY")
|
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles", fetch="EXTRA_LAZY")
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use Serializable;
|
||||||
* @UniqueEntity("username")
|
* @UniqueEntity("username")
|
||||||
* @UniqueEntity("email")
|
* @UniqueEntity("email")
|
||||||
*/
|
*/
|
||||||
class User extends BaseUser implements Serializable
|
class User extends BaseUser implements Serializable, AuditableEntity
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @ORM\Id
|
* @ORM\Id
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ use Doctrine\ORM\Event\OnFlushEventArgs;
|
||||||
|
|
||||||
use InfluxDB\Point;
|
use InfluxDB\Point;
|
||||||
|
|
||||||
|
use App\Entity\AuditableEntity;
|
||||||
|
|
||||||
class EntityListener
|
class EntityListener
|
||||||
{
|
{
|
||||||
protected $token_storage;
|
protected $token_storage;
|
||||||
|
|
@ -36,6 +38,8 @@ class EntityListener
|
||||||
$object_class = get_class($object);
|
$object_class = get_class($object);
|
||||||
|
|
||||||
if (isset($this->entities[$object_class]))
|
if (isset($this->entities[$object_class]))
|
||||||
|
{
|
||||||
|
if ($object instanceof AuditableEntity)
|
||||||
{
|
{
|
||||||
// get user id for logging
|
// get user id for logging
|
||||||
$user = $this->token_storage->getToken()->getUser();
|
$user = $this->token_storage->getToken()->getUser();
|
||||||
|
|
@ -50,6 +54,7 @@ class EntityListener
|
||||||
$this->writeToLogDB($object_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)
|
||||||
{
|
{
|
||||||
|
|
@ -77,6 +82,8 @@ class EntityListener
|
||||||
// check if entity is in list of entities to log
|
// check if entity is in list of entities to log
|
||||||
$entity_class = get_class($updated_entity);
|
$entity_class = get_class($updated_entity);
|
||||||
if (isset($this->entities[$entity_class]))
|
if (isset($this->entities[$entity_class]))
|
||||||
|
{
|
||||||
|
if ($updated_entity instanceof AuditableEntity)
|
||||||
{
|
{
|
||||||
// get entity name
|
// get entity name
|
||||||
//$obj_class = preg_replace('/.*\\\\/', '', $entity_class);
|
//$obj_class = preg_replace('/.*\\\\/', '', $entity_class);
|
||||||
|
|
@ -102,6 +109,7 @@ class EntityListener
|
||||||
$this->writeToLogDB($entity_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)
|
||||||
|
|
@ -109,6 +117,8 @@ class EntityListener
|
||||||
// check if entity is in list of entities to log
|
// check if entity is in list of entities to log
|
||||||
$entity_class = get_class($deleted_entity);
|
$entity_class = get_class($deleted_entity);
|
||||||
if (isset($this->entities[$entity_class]))
|
if (isset($this->entities[$entity_class]))
|
||||||
|
{
|
||||||
|
if ($deleted_entity instanceof AuditableEntity)
|
||||||
{
|
{
|
||||||
// get entity name
|
// get entity name
|
||||||
//$obj_class = preg_replace('/.*\\\\/', '', get_class($deleted_entity));
|
//$obj_class = preg_replace('/.*\\\\/', '', get_class($deleted_entity));
|
||||||
|
|
@ -121,6 +131,7 @@ class EntityListener
|
||||||
$this->writeToLogDB($entity_class, $deleted_id, 'delete', $fields, $user_id);
|
$this->writeToLogDB($entity_class, $deleted_id, 'delete', $fields, $user_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue