Add EntityLog entity for logging . #330
This commit is contained in:
parent
28ae096a87
commit
5c22fd7cc7
3 changed files with 164 additions and 19 deletions
107
src/Entity/EntityLog.php
Normal file
107
src/Entity/EntityLog.php
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="entity_log")
|
||||
*/
|
||||
class EntityLog
|
||||
{
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(type="integer")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
// user that executed the action
|
||||
/**
|
||||
* @ORM\Column(type="string", length=80)
|
||||
* @Assert\NotBlank()
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
// date action took place
|
||||
/**
|
||||
* @ORM\Column(type="datetime")
|
||||
*/
|
||||
protected $date_action;
|
||||
|
||||
// action
|
||||
/**
|
||||
* @ORM\Column(type="string", length=80)
|
||||
* @Assert\NotBlank()
|
||||
*/
|
||||
protected $action;
|
||||
|
||||
// changes
|
||||
/**
|
||||
* @ORM\Column(type="json")
|
||||
*/
|
||||
protected $changes;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->changes = [];
|
||||
}
|
||||
|
||||
public function getID()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUser()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function setUser(string $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDateAction()
|
||||
{
|
||||
return $this->date_action;
|
||||
}
|
||||
|
||||
public function setDateAction(DateTime $date_action)
|
||||
{
|
||||
$this->date_action = $date_action;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAction()
|
||||
{
|
||||
return $this->action;
|
||||
}
|
||||
|
||||
public function setAction(string $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getChanges($id)
|
||||
{
|
||||
// return null if we don't have it
|
||||
if (!isset($this->changes[$id]))
|
||||
return null;
|
||||
|
||||
return $this->changes[$id];
|
||||
}
|
||||
|
||||
public function addChange($id, $value)
|
||||
{
|
||||
$this->changes[$id] = $value;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,8 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
|||
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
||||
use Doctrine\ORM\Event\OnFlushEventArgs;
|
||||
|
||||
use App\Entity\EntityLog;
|
||||
|
||||
use DateTime;
|
||||
|
||||
abstract class EntityListener
|
||||
|
|
@ -20,27 +22,47 @@ abstract class EntityListener
|
|||
|
||||
public function postPersist($object, LifecycleEventArgs $args)
|
||||
{
|
||||
// TODO: logs need be stored in another db
|
||||
// for now, same db
|
||||
$em = $args->getEntityManager();
|
||||
|
||||
$event_time = new DateTime();
|
||||
$log_time = $event_time->format('Y-m-d H:i:s');
|
||||
|
||||
// get user for logging
|
||||
$user = $this->token_storage->getToken()->getUser();
|
||||
$username = $user->getUsername();
|
||||
|
||||
error_log($log_time . ' - ' . $username . ' - Created object');
|
||||
$entity_log = new EntityLog();
|
||||
$entity_log->setUser($username)
|
||||
->setDateAction($event_time)
|
||||
->setAction('Create');
|
||||
|
||||
$log_message = 'Object with id ' . $object->getID() . ' created.';
|
||||
$entity_log->addChange('Object', $log_message);
|
||||
|
||||
$em->persist($entity_log);
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
public function onFlush(OnFlushEventArgs $args)
|
||||
{
|
||||
// TODO: logs need be stored in another db
|
||||
// for now, same db
|
||||
$em = $args->getEntityManager();
|
||||
|
||||
// get date time of event for logging
|
||||
$event_time = new DateTime();
|
||||
$log_time = $event_time->format('Y-m-d H:i:s');
|
||||
|
||||
// get user for logging
|
||||
$user = $this->token_storage->getToken()->getUser();
|
||||
$username = $user->getUsername();
|
||||
|
||||
$em = $args->getEntityManager();
|
||||
$entity_log = new EntityLog();
|
||||
$entity_log->setUser($username)
|
||||
->setDateAction($event_time);
|
||||
|
||||
$field_changes = [];
|
||||
|
||||
$unit_of_work = $em->getUnitOfWork();
|
||||
$deleted_entities = $unit_of_work->getScheduledEntityDeletions();
|
||||
$updated_entities = $unit_of_work->getScheduledEntityUpdates();
|
||||
|
|
@ -60,7 +82,16 @@ abstract class EntityListener
|
|||
$old_value = $values[0];
|
||||
$new_value = $values[1];
|
||||
|
||||
error_log($log_time . ' - ' . $username . ' - Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value);
|
||||
$field_changes[$field] = [
|
||||
'old_value' => $values[0],
|
||||
'new_value' => $values[1],
|
||||
];
|
||||
}
|
||||
|
||||
$entity_log->setAction('Update');
|
||||
if ($updated_entity instanceof User)
|
||||
{
|
||||
$entity_log->addChange('User', $field_changes);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -68,10 +99,14 @@ abstract class EntityListener
|
|||
foreach ($deleted_entities as $deleted_entity)
|
||||
{
|
||||
$deleted_id = $deleted_entity->getID();
|
||||
$log_message = 'Object with id ' . $deleted_id . ' deleted.';
|
||||
|
||||
error_log($log_time . ' - ' . $username . ' - Deleted object with id ' . $deleted_id);
|
||||
$entity_log->setAction('Delete')
|
||||
->addChange('Object', $log_message);
|
||||
}
|
||||
|
||||
//$em->persist($entity_log);
|
||||
|
||||
// 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
|
||||
/*
|
||||
|
|
@ -111,16 +146,4 @@ abstract class EntityListener
|
|||
} */
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
public function postUpdate($object, LifecycleEventArgs $args)
|
||||
{
|
||||
error_log('Entity updated');
|
||||
}
|
||||
|
||||
public function postRemove($object, LifecycleEventArgs $args)
|
||||
{
|
||||
error_log('Entity deleted');
|
||||
} */
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ use App\EventListener\EntityListener;
|
|||
use App\Entity\User;
|
||||
use App\Entity\Role;
|
||||
use App\Entity\Hub;
|
||||
use App\Entity\EntityLog;
|
||||
|
||||
use DateTime;
|
||||
|
||||
|
|
@ -26,8 +27,13 @@ class UserEntityListener extends EntityListener
|
|||
parent::__construct($token_storage);
|
||||
}
|
||||
|
||||
/*
|
||||
public function postPersist($user, LifecycleEventArgs $args)
|
||||
{
|
||||
// TODO: logs need be stored in another db
|
||||
// for now, same db
|
||||
$em = $args->getEntityManager();
|
||||
|
||||
$event_time = new DateTime();
|
||||
$log_time = $event_time->format('Y-m-d H:i:s');
|
||||
|
||||
|
|
@ -35,7 +41,16 @@ class UserEntityListener extends EntityListener
|
|||
$user = $this->token_storage->getToken()->getUser();
|
||||
$username = $user->getUsername();
|
||||
|
||||
error_log($log_time . ' - ' . $username . ' - Created user with id ' . $user->getID() . ' and username ' . $user->getUsername());
|
||||
$entity_log = new EntityLog();
|
||||
$entity_log->setUser($username)
|
||||
->setDateAction($event_time)
|
||||
->setAction('Create');
|
||||
|
||||
$entity_log->addChange('User', 'User created.');
|
||||
|
||||
$em->persist($entity_log);
|
||||
$em->flush();
|
||||
|
||||
}
|
||||
|
||||
//public function onFlush(OnFlushEventArgs $args)
|
||||
|
|
|
|||
Loading…
Reference in a new issue