From 0468e2e422d37cbf6165f19c9b03e3a336b012d3 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 12 Feb 2020 04:54:37 +0000 Subject: [PATCH 01/28] Add job order status listener. #331 --- config/services.yaml | 8 +++ src/EventListener/JobOrderStatusListener.php | 69 ++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/EventListener/JobOrderStatusListener.php diff --git a/config/services.yaml b/config/services.yaml index 8fe325e4..0ea9afd4 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -247,3 +247,11 @@ services: $redis_prov: "@App\\Service\\RedisClientProvider" $loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%" $status_key: "%env(STATUS_RIDER_KEY)%" + + App\EventListener\JobOrderStatusListener: + arguments: + $wh: "@App\\Service\\WarrantyHandler" + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postUpdate' + entity: 'App\Entity\JobOrder' diff --git a/src/EventListener/JobOrderStatusListener.php b/src/EventListener/JobOrderStatusListener.php new file mode 100644 index 00000000..6e746fd2 --- /dev/null +++ b/src/EventListener/JobOrderStatusListener.php @@ -0,0 +1,69 @@ +wh = $wh; + } + + public function postUpdate(JobOrder $jo, LifecycleEventArgs $args) + { + $status = $jo->getStatus(); + + if ($status == JOStatus::FULFILLED) + { + error_log('hello!'); + // create warranty + /* + if ($this->checkIfNewBattery($jo)) + { + $serial = null; + $warranty_class = $jo->getWarrantyClass(); + $first_name = $jo->getCustomer()->getFirstName(); + $last_name = $jo->getCustomer()->getLastName(); + $mobile_number = $jo->getCustomer()->getPhoneMobile(); + + // check if date fulfilled is null + if ($jo->getDateFulfill() == null) + $date_purchase = $jo->getDateCreate(); + else + $date_purchase = $jo->getDateFulfill(); + + // validate plate number + // $plate_number = $this->wh->cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); + $plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); + if ($plate_number != false) + { + $batt_list = array(); + $invoice = $jo->getInvoice(); + if (!empty($invoice)) + { + // get battery + $invoice_items = $invoice->getItems(); + foreach ($invoice_items as $item) + { + $battery = $item->getBattery(); + if ($battery != null) + { + $batt_list[] = $item->getBattery(); + } + } + } + + $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); + } + } */ + } + + } + +} -- 2.43.5 From c6217e2f769db7f786fd1b33c1db534c62a93443 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 20 Feb 2020 07:01:41 +0000 Subject: [PATCH 02/28] Move warranty creation from JO to event listener. #331 --- src/EventListener/JobOrderStatusListener.php | 94 ++++++++++++------- .../JobOrderHandler/CMBJobOrderHandler.php | 7 +- 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/EventListener/JobOrderStatusListener.php b/src/EventListener/JobOrderStatusListener.php index 6e746fd2..8c3c9f45 100644 --- a/src/EventListener/JobOrderStatusListener.php +++ b/src/EventListener/JobOrderStatusListener.php @@ -5,65 +5,87 @@ namespace App\EventListener; use Doctrine\Common\Persistence\Event\LifecycleEventArgs; use App\Entity\JobOrder; +use App\Entity\Warranty; use App\Service\WarrantyHandler; +use App\Service\JobOrderHandlerInterface; + +use App\Ramcar\JOStatus; class JobOrderStatusListener { - public function __construct(WarrantyHandler $wh) + protected $wh; + protected $jo_handler; + + public function __construct(WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler) { $this->wh = $wh; + $this->jo_handler = $jo_handler; } + // when a JO is updated public function postUpdate(JobOrder $jo, LifecycleEventArgs $args) { $status = $jo->getStatus(); if ($status == JOStatus::FULFILLED) { - error_log('hello!'); - // create warranty - /* - if ($this->checkIfNewBattery($jo)) + $this->createWarrantyFromJO($jo); + } + } + + // when a new job order comes in and status is already fulfilled + public function postPersist(JobOrder $jo, LifecycleEventArgs $args) + { + $status = $jo->getStatus(); + + if ($status == JOStatus::FULFILLED) + { + $this->createWarrantyFromJO($jo); + } + } + + protected function createWarrantyFromJO(JobOrder $jo) + { + // create warranty + if ($this->jo_handler->checkIfNewBattery($jo)) + { + $serial = null; + $warranty_class = $jo->getWarrantyClass(); + $first_name = $jo->getCustomer()->getFirstName(); + $last_name = $jo->getCustomer()->getLastName(); + $mobile_number = $jo->getCustomer()->getPhoneMobile(); + + // check if date fulfilled is null + if ($jo->getDateFulfill() == null) + $date_purchase = $jo->getDateCreate(); + else + $date_purchase = $jo->getDateFulfill(); + + // validate plate number + // $plate_number = $this->wh->cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); + $plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); + if ($plate_number != false) { - $serial = null; - $warranty_class = $jo->getWarrantyClass(); - $first_name = $jo->getCustomer()->getFirstName(); - $last_name = $jo->getCustomer()->getLastName(); - $mobile_number = $jo->getCustomer()->getPhoneMobile(); - - // check if date fulfilled is null - if ($jo->getDateFulfill() == null) - $date_purchase = $jo->getDateCreate(); - else - $date_purchase = $jo->getDateFulfill(); - - // validate plate number - // $plate_number = $this->wh->cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); - $plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); - if ($plate_number != false) + $batt_list = array(); + $invoice = $jo->getInvoice(); + if (!empty($invoice)) { - $batt_list = array(); - $invoice = $jo->getInvoice(); - if (!empty($invoice)) + // get battery + $invoice_items = $invoice->getItems(); + foreach ($invoice_items as $item) { - // get battery - $invoice_items = $invoice->getItems(); - foreach ($invoice_items as $item) + $battery = $item->getBattery(); + if ($battery != null) { - $battery = $item->getBattery(); - if ($battery != null) - { - $batt_list[] = $item->getBattery(); - } + $batt_list[] = $item->getBattery(); } } - - $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } */ - } + $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); + } + } } } diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 80e4158e..a3a81276 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -937,6 +937,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface // call rider assignment handler's fulfillJobOrder $this->rah->fulfillJobOrder($obj, $image_url, $rider); + /* // create the warranty if new battery only if ($this->checkIfNewBattery($obj)) { @@ -979,7 +980,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } + } */ // validated! save the entity $em->flush(); @@ -2605,7 +2606,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $cust_vehicle->setWarrantyCode($req->request->get('warranty_code')); $em->persist($cust_vehicle); - + /* // create the warranty if new battery only if ($this->checkIfNewBattery($jo)) { @@ -2648,7 +2649,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } + } */ $em->flush(); } -- 2.43.5 From 88ed88a8da3ddb23cfd55cdd0abffe1e34df60fb Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 20 Feb 2020 07:05:40 +0000 Subject: [PATCH 03/28] Add event postPersist to the JO status listener. #331 --- config/services.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/services.yaml b/config/services.yaml index 0ea9afd4..01465e88 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -255,3 +255,6 @@ services: - name: 'doctrine.orm.entity_listener' event: 'postUpdate' entity: 'App\Entity\JobOrder' + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\JobOrder' -- 2.43.5 From 8a328bf4417abba06fdcc924a53e09c7873b021c Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 20 Feb 2020 07:17:42 +0000 Subject: [PATCH 04/28] Move warranty creation from JO to event listener. #331 --- src/Service/JobOrderHandler/ResqJobOrderHandler.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index c6a6fa01..87aaeac7 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -715,6 +715,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface // call rider assignment handler's fulfillJobOrder $this->rah->fulfillJobOrder($obj, $image_url, $rider); + /* // create the warranty if new battery only if ($this->checkIfNewBattery($obj)) { @@ -753,7 +754,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } + } */ } } -- 2.43.5 From 7dd9911e2279f10e59d7c306f6244c3680a19e2d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 21 Feb 2020 02:29:39 +0000 Subject: [PATCH 05/28] Move warranty creation out of the RiderAPIHandler. #331 --- src/Service/JobOrderHandler/CMBJobOrderHandler.php | 7 +------ src/Service/JobOrderHandler/ResqJobOrderHandler.php | 6 +----- src/Service/RiderAPIHandler/CMBRiderAPIHandler.php | 10 +++------- src/Service/RiderAPIHandler/ResqRiderAPIHandler.php | 9 +++------ 4 files changed, 8 insertions(+), 24 deletions(-) diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index a3a81276..d53ee926 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -24,7 +24,6 @@ use App\Entity\Hub; use App\Entity\Promo; use App\Entity\Rider; use App\Entity\JORejection; -use App\Entity\Warranty; use App\Entity\Customer; use App\Entity\ServiceCharge; @@ -44,7 +43,6 @@ use App\Service\InvoiceGeneratorInterface; use App\Service\JobOrderHandlerInterface; use App\Service\RiderAssignmentHandlerInterface; use App\Service\CustomerHandlerInterface; -use App\Service\WarrantyHandler; use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\MapTools; @@ -67,7 +65,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface protected $translator; protected $rah; protected $country_code; - protected $wh; protected $cust_handler; protected $template_hash; @@ -75,8 +72,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, - string $country_code, WarrantyHandler $wh, - CustomerHandlerInterface $cust_handler) + string $country_code, CustomerHandlerInterface $cust_handler) { $this->em = $em; $this->ic = $ic; @@ -85,7 +81,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $this->translator = $translator; $this->rah = $rah; $this->country_code = $country_code; - $this->wh = $wh; $this->cust_handler = $cust_handler; $this->loadTemplates(); diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index 87aaeac7..50908933 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -23,7 +23,6 @@ use App\Entity\Hub; use App\Entity\Promo; use App\Entity\Rider; use App\Entity\JORejection; -use App\Entity\Warranty; use App\Entity\Customer; use App\Ramcar\InvoiceCriteria; @@ -41,7 +40,6 @@ use App\Ramcar\JORejectionReason; use App\Service\InvoiceGeneratorInterface; use App\Service\JobOrderHandlerInterface; use App\Service\RiderAssignmentHandlerInterface; -use App\Service\WarrantyHandler; use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\MapTools; @@ -64,14 +62,13 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface protected $translator; protected $rah; protected $country_code; - protected $wh; protected $template_hash; public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, - string $country_code, WarrantyHandler $wh) + string $country_code) { $this->em = $em; $this->ic = $ic; @@ -80,7 +77,6 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $this->translator = $translator; $this->rah = $rah; $this->country_code = $country_code; - $this->wh = $wh; $this->loadTemplates(); } diff --git a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php index 3c0236e1..32514afd 100644 --- a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php @@ -18,7 +18,6 @@ use App\Service\RiderAPIHandlerInterface; use App\Service\RedisClientProvider; use App\Service\RiderCache; use App\Service\MQTTClient; -use App\Service\WarrantyHandler; use App\Service\JobOrderHandlerInterface; use App\Service\InvoiceGeneratorInterface; @@ -30,7 +29,6 @@ use App\Entity\Promo; use App\Entity\Battery; use App\Entity\BatteryModel; use App\Entity\BatterySize; -use App\Entity\Warranty; use DateTime; @@ -42,7 +40,6 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface protected $rcache; protected $country_code; protected $mclient; - protected $wh; protected $jo_handler; protected $ic; protected $session; @@ -50,8 +47,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, EncoderFactoryInterface $ef, RiderCache $rcache, string $country_code, MQTTClient $mclient, - WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler, - InvoiceGeneratorInterface $ic) + JobOrderHandlerInterface $jo_handler, InvoiceGeneratorInterface $ic) { $this->em = $em; $this->redis = $redis; @@ -59,7 +55,6 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface $this->rcache = $rcache; $this->country_code = $country_code; $this->mclient = $mclient; - $this->wh = $wh; $this->jo_handler = $jo_handler; $this->ic = $ic; @@ -523,6 +518,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface $this->em->flush(); + /* // create warranty if($this->jo_handler->checkIfNewBattery($jo)) { @@ -561,7 +557,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } + } */ // send mqtt event (fulfilled) $rider = $this->session->getRider(); diff --git a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php index b5bed63b..4a3ea62a 100644 --- a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php @@ -18,7 +18,6 @@ use App\Service\RiderAPIHandlerInterface; use App\Service\RedisClientProvider; use App\Service\RiderCache; use App\Service\MQTTClient; -use App\Service\WarrantyHandler; use App\Service\JobOrderHandlerInterface; use App\Service\InvoiceGeneratorInterface; @@ -41,7 +40,6 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface protected $rcache; protected $country_code; protected $mclient; - protected $wh; protected $jo_handler; protected $ic; protected $session; @@ -49,8 +47,7 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, EncoderFactoryInterface $ef, RiderCache $rcache, string $country_code, MQTTClient $mclient, - WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler, - InvoiceGeneratorInterface $ic) + JobOrderHandlerInterface $jo_handler, InvoiceGeneratorInterface $ic) { $this->em = $em; $this->redis = $redis; @@ -58,7 +55,6 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface $this->rcache = $rcache; $this->country_code = $country_code; $this->mclient = $mclient; - $this->wh = $wh; $this->jo_handler = $jo_handler; $this->ic = $ic; @@ -522,6 +518,7 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface $this->em->flush(); + /* // create warranty if($this->jo_handler->checkIfNewBattery($jo)) { @@ -556,7 +553,7 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface } $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); - } + } */ // send mqtt event (fulfilled) $rider = $this->session->getRider(); -- 2.43.5 From c2d109ae0ae8862ac21bc21444dba92f20edfa13 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 21 Feb 2020 08:25:57 +0000 Subject: [PATCH 06/28] Add event listeners to services.yaml for cmb and resq. #331 --- config/cmb.services.yaml | 11 +++++++++++ config/resq.services.yaml | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/config/cmb.services.yaml b/config/cmb.services.yaml index d12b2acf..3ec1e341 100644 --- a/config/cmb.services.yaml +++ b/config/cmb.services.yaml @@ -227,3 +227,14 @@ services: $redis_prov: "@App\\Service\\RedisClientProvider" $loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%" $status_key: "%env(STATUS_RIDER_KEY)%" + + App\EventListener\JobOrderStatusListener: + arguments: + $wh: "@App\\Service\\WarrantyHandler" + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postUpdate' + entity: 'App\Entity\JobOrder' + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\JobOrder' diff --git a/config/resq.services.yaml b/config/resq.services.yaml index 1756cf98..a8143011 100644 --- a/config/resq.services.yaml +++ b/config/resq.services.yaml @@ -226,3 +226,15 @@ services: $redis_prov: "@App\\Service\\RedisClientProvider" $loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%" $status_key: "%env(STATUS_RIDER_KEY)%" + + App\EventListener\JobOrderStatusListener: + arguments: + $wh: "@App\\Service\\WarrantyHandler" + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postUpdate' + entity: 'App\Entity\JobOrder' + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\JobOrder' + -- 2.43.5 From a629d9b0243b6d11897c8d98995ab9c1626faa85 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 26 Feb 2020 07:57:02 +0000 Subject: [PATCH 07/28] Add entity listener for user. #330 --- config/cmb.services.yaml | 8 ++ config/resq.services.yaml | 7 ++ config/services.yaml | 8 ++ src/EventListener/EntityListener.php | 32 +++++++ .../EntityListener/UserEntityListener.php | 87 +++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 src/EventListener/EntityListener.php create mode 100644 src/EventListener/EntityListener/UserEntityListener.php diff --git a/config/cmb.services.yaml b/config/cmb.services.yaml index 3ec1e341..eb691569 100644 --- a/config/cmb.services.yaml +++ b/config/cmb.services.yaml @@ -238,3 +238,11 @@ services: - name: 'doctrine.orm.entity_listener' event: 'postPersist' entity: 'App\Entity\JobOrder' + + App\EventListener\EntityListener\UserEntityListener: + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\User' + - name: 'doctrine.event_listener' + event: 'onFlush' diff --git a/config/resq.services.yaml b/config/resq.services.yaml index a8143011..76948088 100644 --- a/config/resq.services.yaml +++ b/config/resq.services.yaml @@ -238,3 +238,10 @@ services: event: 'postPersist' entity: 'App\Entity\JobOrder' + App\EventListener\EntityListener\UserEntityListener: + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\User' + - name: 'doctrine.event_listener' + event: 'onFlush' diff --git a/config/services.yaml b/config/services.yaml index 01465e88..e2e43641 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -258,3 +258,11 @@ services: - name: 'doctrine.orm.entity_listener' event: 'postPersist' entity: 'App\Entity\JobOrder' + + App\EventListener\EntityListener\UserEntityListener: + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\User' + - name: 'doctrine.event_listener' + event: 'onFlush' diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php new file mode 100644 index 00000000..21711669 --- /dev/null +++ b/src/EventListener/EntityListener.php @@ -0,0 +1,32 @@ +getID() . ' and username ' . $user->getUsername()); + } + + public function onFlush(OnFlushEventArgs $args) + { + $em = $args->getEntityManager(); + $unit_of_work = $em->getUnitOfWork(); + $deleted_entities = $unit_of_work->getScheduledEntityDeletions(); + $updated_entities = $unit_of_work->getScheduledEntityUpdates(); + + foreach ($updated_entities as $updated_entity) + { + if ($updated_entity instanceof User) + { + $changeset = $unit_of_work->getEntityChangeSet($updated_entity); + + $entity_fields = array_keys($changeset); + foreach ($entity_fields as $field) + { + error_log('field ' . $field); + + $values = $changeset[$field]; + + $old_value = $values[0]; + $new_value = $values[1]; + + error_log('Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); + } + } + } + + foreach ($deleted_entities as $deleted_entity) + { + if ($deleted_entity instanceof User) + { + $deleted_id = $deleted_entity->getID(); + $deleted_username = $deleted_entity->getUsername(); + + error_log('Deleted user with id ' . $deleted_id . ' and username ' . $deleted_username); + } + } + } + + /* + public function postUpdate($user, LifecycleEventArgs $args) + { + error_log ('POST UPDATE'); + + error_log('User with id ' . $user->getID() . ' updated.'); + error_log('username ' . $user->getUsername()); + error_log('password ' . $user->getPassword()); + error_log('first name ' . $user->getFirstName()); + error_log('last name ' . $user->getLastName()); + error_log('contact number '. $user->getContactNumber()); + error_log('email ' . $user->getEmail()); + + $roles = $user->getRoles(); + $hubs = $user->getHubs(); + + foreach ($roles as $role) + error_log('user role ' . $role); + + foreach ($hubs as $hub) + error_log('user hub ' . $hub); + } */ + + /* + public function postRemove($user, LifecycleEventArgs $args) + { + error_log('User deleted.'); + } */ +} -- 2.43.5 From 2998154a5582a4f1ac61459256c65616139f7013 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 27 Feb 2020 01:04:27 +0000 Subject: [PATCH 08/28] Add collection update and collection get to logs. #330 --- src/EventListener/EntityListener/UserEntityListener.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/EventListener/EntityListener/UserEntityListener.php b/src/EventListener/EntityListener/UserEntityListener.php index 3ece22f0..2210c3d8 100644 --- a/src/EventListener/EntityListener/UserEntityListener.php +++ b/src/EventListener/EntityListener/UserEntityListener.php @@ -22,6 +22,11 @@ class UserEntityListener extends EntityListener $unit_of_work = $em->getUnitOfWork(); $deleted_entities = $unit_of_work->getScheduledEntityDeletions(); $updated_entities = $unit_of_work->getScheduledEntityUpdates(); + $updated_collections = $unit_of_work->getScheduledCollectionUpdates(); + $deleted_collections = $unit_of_work->getScheduledCollectionDeletions(); + + error_log('count of updated_collections ' . count($updated_collections)); + error_log('count of deleted_collections ' . count($deleted_collections)); foreach ($updated_entities as $updated_entity) { @@ -42,6 +47,10 @@ class UserEntityListener extends EntityListener error_log('Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); } } + else + { + error_log('hhmmm unsa kaha ni'); + } } foreach ($deleted_entities as $deleted_entity) -- 2.43.5 From 8b968553954d5819ee78f80c2066f1d5b08d8d21 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 27 Feb 2020 07:29:17 +0000 Subject: [PATCH 09/28] Added generic onFlush function to EntityListener. #330 --- src/EventListener/EntityListener.php | 33 ++++++++- .../EntityListener/UserEntityListener.php | 70 ++++++++++++++----- 2 files changed, 83 insertions(+), 20 deletions(-) diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index 21711669..ebd51b53 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -10,12 +10,41 @@ abstract class EntityListener public function postPersist($object, LifecycleEventArgs $args) { - error_log('Entity created'); + error_log('Created object'); } public function onFlush(OnFlushEventArgs $args) { - error_log('on flush'); + $em = $args->getEntityManager(); + $unit_of_work = $em->getUnitOfWork(); + $deleted_entities = $unit_of_work->getScheduledEntityDeletions(); + $updated_entities = $unit_of_work->getScheduledEntityUpdates(); + $updated_collections = $unit_of_work->getScheduledCollectionUpdates(); + $deleted_collections = $unit_of_work->getScheduledCollectionDeletions(); + + foreach ($updated_entities as $updated_entity) + { + $changeset = $unit_of_work->getEntityChangeSet($updated_entity); + + $entity_fields = array_keys($changeset); + foreach ($entity_fields as $field) + { + $values = $changeset[$field]; + + $old_value = $values[0]; + $new_value = $values[1]; + + error_log('Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); + } + } + + foreach ($deleted_entities as $deleted_entity) + { + $deleted_id = $deleted_entity->getID(); + + error_log('Deleted object with id ' . $deleted_id); + } + } /* diff --git a/src/EventListener/EntityListener/UserEntityListener.php b/src/EventListener/EntityListener/UserEntityListener.php index 2210c3d8..91495e41 100644 --- a/src/EventListener/EntityListener/UserEntityListener.php +++ b/src/EventListener/EntityListener/UserEntityListener.php @@ -8,6 +8,8 @@ use Doctrine\ORM\Event\OnFlushEventArgs; use App\EventListener\EntityListener; use App\Entity\User; +use App\Entity\Role; +use App\Entity\Hub; class UserEntityListener extends EntityListener { @@ -16,8 +18,9 @@ class UserEntityListener extends EntityListener error_log('Created user with id ' . $user->getID() . ' and username ' . $user->getUsername()); } - public function onFlush(OnFlushEventArgs $args) - { + //public function onFlush(OnFlushEventArgs $args) + //{ + /* $em = $args->getEntityManager(); $unit_of_work = $em->getUnitOfWork(); $deleted_entities = $unit_of_work->getScheduledEntityDeletions(); @@ -25,20 +28,15 @@ class UserEntityListener extends EntityListener $updated_collections = $unit_of_work->getScheduledCollectionUpdates(); $deleted_collections = $unit_of_work->getScheduledCollectionDeletions(); - error_log('count of updated_collections ' . count($updated_collections)); - error_log('count of deleted_collections ' . count($deleted_collections)); - foreach ($updated_entities as $updated_entity) { - if ($updated_entity instanceof User) - { + //if ($updated_entity instanceof User) + //{ $changeset = $unit_of_work->getEntityChangeSet($updated_entity); $entity_fields = array_keys($changeset); foreach ($entity_fields as $field) { - error_log('field ' . $field); - $values = $changeset[$field]; $old_value = $values[0]; @@ -46,24 +44,60 @@ class UserEntityListener extends EntityListener error_log('Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); } - } - else - { - error_log('hhmmm unsa kaha ni'); - } + //} } foreach ($deleted_entities as $deleted_entity) { - if ($deleted_entity instanceof User) - { + //if ($deleted_entity instanceof User) + //{ $deleted_id = $deleted_entity->getID(); $deleted_username = $deleted_entity->getUsername(); error_log('Deleted user with id ' . $deleted_id . ' and username ' . $deleted_username); - } + //} } - } + + // 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 + foreach ($updated_collections as $updated_collection) + { + error_log('in updated_collection snippet'); + + foreach ($updated_collection as $item) + { + if ($item instanceof Role) + { + error_log('updated_collection item ' . $item->getName()); + } + if ($item instanceof Hub) + { + error_log('updated_collection item ' . $item->getName()); + } + } + + } + + foreach ($deleted_collections as $deleted_collection) + { + error_log('in deleted_collection snippet'); + + foreach ($deleted_collection as $item) + { + if ($item instanceof Role) + { + error_log('deleted_collection item ' . $item->getName()); + } + if ($item instanceof Hub) + { + error_log('deleted_collection item ' . $item->getName()); + } + } + + + } */ + + //} /* public function postUpdate($user, LifecycleEventArgs $args) -- 2.43.5 From 4c27b4da06d88047be5d23404a5d8bbb0fa985aa Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 28 Feb 2020 03:21:29 +0000 Subject: [PATCH 10/28] Add date and time for logging. #330 --- src/EventListener/EntityListener.php | 9 +++++++-- src/EventListener/EntityListener/UserEntityListener.php | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index ebd51b53..9cb997b0 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -5,6 +5,8 @@ namespace App\EventListener; use Doctrine\Common\Persistence\Event\LifecycleEventArgs; use Doctrine\ORM\Event\OnFlushEventArgs; +use DateTime; + abstract class EntityListener { @@ -15,6 +17,9 @@ abstract class EntityListener public function onFlush(OnFlushEventArgs $args) { + $event_time = new DateTime(); + $log_time = $event_time->format('Y-m-d H:i:s'); + $em = $args->getEntityManager(); $unit_of_work = $em->getUnitOfWork(); $deleted_entities = $unit_of_work->getScheduledEntityDeletions(); @@ -34,7 +39,7 @@ abstract class EntityListener $old_value = $values[0]; $new_value = $values[1]; - error_log('Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); + error_log($log_time . ' - Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); } } @@ -42,7 +47,7 @@ abstract class EntityListener { $deleted_id = $deleted_entity->getID(); - error_log('Deleted object with id ' . $deleted_id); + error_log($log_time . ' - Deleted object with id ' . $deleted_id); } } diff --git a/src/EventListener/EntityListener/UserEntityListener.php b/src/EventListener/EntityListener/UserEntityListener.php index 91495e41..aef21dbe 100644 --- a/src/EventListener/EntityListener/UserEntityListener.php +++ b/src/EventListener/EntityListener/UserEntityListener.php @@ -11,11 +11,15 @@ use App\Entity\User; use App\Entity\Role; use App\Entity\Hub; +use DateTime; + class UserEntityListener extends EntityListener { public function postPersist($user, LifecycleEventArgs $args) { - error_log('Created user with id ' . $user->getID() . ' and username ' . $user->getUsername()); + $event_time = new DateTime(); + $log_time = $event_time->format('Y-m-d H:i:s'); + error_log($log_time . ' - Created user with id ' . $user->getID() . ' and username ' . $user->getUsername()); } //public function onFlush(OnFlushEventArgs $args) -- 2.43.5 From 987097ff6a558b2636d0c611f13d5039e73b0361 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 28 Feb 2020 08:40:44 +0000 Subject: [PATCH 11/28] Add username of user to log. #330 --- config/cmb.services.yaml | 2 + config/resq.services.yaml | 2 + config/services.yaml | 2 + src/EventListener/EntityListener.php | 66 ++++++++++++++++++- .../EntityListener/UserEntityListener.php | 18 ++++- 5 files changed, 86 insertions(+), 4 deletions(-) diff --git a/config/cmb.services.yaml b/config/cmb.services.yaml index eb691569..f3051cff 100644 --- a/config/cmb.services.yaml +++ b/config/cmb.services.yaml @@ -240,6 +240,8 @@ services: entity: 'App\Entity\JobOrder' App\EventListener\EntityListener\UserEntityListener: + arguments: + ["@security.token_storage"] tags: - name: 'doctrine.orm.entity_listener' event: 'postPersist' diff --git a/config/resq.services.yaml b/config/resq.services.yaml index 76948088..3a3c9e68 100644 --- a/config/resq.services.yaml +++ b/config/resq.services.yaml @@ -239,6 +239,8 @@ services: entity: 'App\Entity\JobOrder' App\EventListener\EntityListener\UserEntityListener: + arguments: + ["@security.token_storage"] tags: - name: 'doctrine.orm.entity_listener' event: 'postPersist' diff --git a/config/services.yaml b/config/services.yaml index e2e43641..ee9a444e 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -260,6 +260,8 @@ services: entity: 'App\Entity\JobOrder' App\EventListener\EntityListener\UserEntityListener: + arguments: + ["@security.token_storage"] tags: - name: 'doctrine.orm.entity_listener' event: 'postPersist' diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index 9cb997b0..e8b72e77 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -2,6 +2,8 @@ namespace App\EventListener; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; + use Doctrine\Common\Persistence\Event\LifecycleEventArgs; use Doctrine\ORM\Event\OnFlushEventArgs; @@ -9,17 +11,35 @@ use DateTime; abstract class EntityListener { + protected $token_storage; + + public function __construct(TokenStorage $token_storage) + { + $this->token_storage = $token_storage; + } public function postPersist($object, LifecycleEventArgs $args) { - error_log('Created object'); + $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'); } public function onFlush(OnFlushEventArgs $args) { + // 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(); $unit_of_work = $em->getUnitOfWork(); $deleted_entities = $unit_of_work->getScheduledEntityDeletions(); @@ -27,6 +47,7 @@ abstract class EntityListener $updated_collections = $unit_of_work->getScheduledCollectionUpdates(); $deleted_collections = $unit_of_work->getScheduledCollectionDeletions(); + // get updated fields. This doesn't include lists of other entities. foreach ($updated_entities as $updated_entity) { $changeset = $unit_of_work->getEntityChangeSet($updated_entity); @@ -39,17 +60,56 @@ abstract class EntityListener $old_value = $values[0]; $new_value = $values[1]; - error_log($log_time . ' - Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); + error_log($log_time . ' - ' . $username . ' - Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); } } + // get deleted objects foreach ($deleted_entities as $deleted_entity) { $deleted_id = $deleted_entity->getID(); - error_log($log_time . ' - Deleted object with id ' . $deleted_id); + error_log($log_time . ' - ' . $username . ' - Deleted object with id ' . $deleted_id); } + // 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 + /* + foreach ($updated_collections as $updated_collection) + { + error_log('in updated_collection snippet'); + + foreach ($updated_collection as $item) + { + if ($item instanceof Role) + { + error_log('updated_collection item ' . $item->getName()); + } + if ($item instanceof Hub) + { + error_log('updated_collection item ' . $item->getName()); + } + } + + } + + foreach ($deleted_collections as $deleted_collection) + { + error_log('in deleted_collection snippet'); + + foreach ($deleted_collection as $item) + { + if ($item instanceof Role) + { + error_log('deleted_collection item ' . $item->getName()); + } + if ($item instanceof Hub) + { + error_log('deleted_collection item ' . $item->getName()); + } + } + } */ + } /* diff --git a/src/EventListener/EntityListener/UserEntityListener.php b/src/EventListener/EntityListener/UserEntityListener.php index aef21dbe..54121bd3 100644 --- a/src/EventListener/EntityListener/UserEntityListener.php +++ b/src/EventListener/EntityListener/UserEntityListener.php @@ -2,6 +2,8 @@ namespace App\EventListener\EntityListener; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; + use Doctrine\Common\Persistence\Event\LifecycleEventArgs; use Doctrine\ORM\Event\OnFlushEventArgs; @@ -15,11 +17,25 @@ use DateTime; class UserEntityListener extends EntityListener { + protected $token_storage; + + public function __construct(TokenStorage $token_storage) + { + $this->token_storage = $token_storage; + + parent::__construct($token_storage); + } + public function postPersist($user, LifecycleEventArgs $args) { $event_time = new DateTime(); $log_time = $event_time->format('Y-m-d H:i:s'); - error_log($log_time . ' - Created user with id ' . $user->getID() . ' and username ' . $user->getUsername()); + + // get user for logging + $user = $this->token_storage->getToken()->getUser(); + $username = $user->getUsername(); + + error_log($log_time . ' - ' . $username . ' - Created user with id ' . $user->getID() . ' and username ' . $user->getUsername()); } //public function onFlush(OnFlushEventArgs $args) -- 2.43.5 From 5c22fd7cc76526bde989d3d283ae8d2535c7dbc4 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 2 Mar 2020 08:33:05 +0000 Subject: [PATCH 12/28] Add EntityLog entity for logging . #330 --- src/Entity/EntityLog.php | 107 ++++++++++++++++++ src/EventListener/EntityListener.php | 59 +++++++--- .../EntityListener/UserEntityListener.php | 17 ++- 3 files changed, 164 insertions(+), 19 deletions(-) create mode 100644 src/Entity/EntityLog.php diff --git a/src/Entity/EntityLog.php b/src/Entity/EntityLog.php new file mode 100644 index 00000000..ccbf19dc --- /dev/null +++ b/src/Entity/EntityLog.php @@ -0,0 +1,107 @@ +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; + } +} diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index e8b72e77..0b02d3f2 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -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'); - } */ - } diff --git a/src/EventListener/EntityListener/UserEntityListener.php b/src/EventListener/EntityListener/UserEntityListener.php index 54121bd3..64ee24d0 100644 --- a/src/EventListener/EntityListener/UserEntityListener.php +++ b/src/EventListener/EntityListener/UserEntityListener.php @@ -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) -- 2.43.5 From c748a19da8d1adaae531a4de9970476dbbafde04 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 3 Mar 2020 06:59:46 +0000 Subject: [PATCH 13/28] Modify doctrine.yaml to support multiple entity managers. #330 --- .env.dist | 1 + config/packages/doctrine.yaml | 272 ++++++++++-------- src/Entity/{ => Logging}/EntityLog.php | 2 +- src/EventListener/EntityListener.php | 2 +- .../EntityListener/UserEntityListener.php | 3 +- 5 files changed, 154 insertions(+), 126 deletions(-) rename src/Entity/{ => Logging}/EntityLog.php (98%) diff --git a/.env.dist b/.env.dist index 30f9a925..b6a80203 100644 --- a/.env.dist +++ b/.env.dist @@ -14,6 +14,7 @@ APP_SECRET=b344cd6cd151ae1d61403ed55806c5ce # For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" # Configure your db driver and server_version in config/packages/doctrine.yaml DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name +LOGGING_DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_logging ###< doctrine/doctrine-bundle ### GMAPS_API_KEY=insert_gmapsapikey_here diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index c154fbc2..b425f88f 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -4,137 +4,163 @@ parameters: # environment variables are not available yet. # You should not need to change this value. env(DATABASE_URL): '' + env(LOGGING_DATABASE_URL): '' doctrine: dbal: - # configure these for your database server - driver: 'pdo_mysql' - server_version: '5.7' - charset: utf8mb4 + default_connection: default + connections: + default: + # configure these for your database server + driver: 'pdo_mysql' + server_version: '5.7' + charset: utf8mb4 - # With Symfony 3.3, remove the `resolve:` prefix - url: '%env(resolve:DATABASE_URL)%' + # With Symfony 3.3, remove the `resolve:` prefix + url: '%env(resolve:DATABASE_URL)%' + logging: + # configure these for your database server + url: '%env(resolve:LOGGING_DATABASE_URL)%' + driver: 'pdo_mysql' + server_version: '5.7' + charset: utf8mb4 types: geometry: CrEOF\Spatial\DBAL\Types\GeometryType point: CrEOF\Spatial\DBAL\Types\Geometry\PointType polygon: CrEOF\Spatial\DBAL\Types\Geometry\PolygonType linestring: CrEOF\Spatial\DBAL\Types\Geometry\LineStringType multipolygon: CrEOF\Spatial\DBAL\Types\Geometry\MultiPolygonType - orm: - auto_generate_proxy_classes: '%kernel.debug%' - naming_strategy: doctrine.orm.naming_strategy.underscore - auto_mapping: true - mappings: - App: - is_bundle: false - type: annotation - dir: '%kernel.project_dir%/src/Entity' - prefix: 'App\Entity' - alias: App - dql: - string_functions: - # for postgresql - geometry: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\Geometry - stbuffer: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBuffer - stcollect: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCollect - stsnaptogrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSnapToGrid - stoverlaps: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STOverlaps - # for match against - match_against: App\Doctrine\MatchAgainst - numeric_functions: - # for postgresql - starea: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STArea - stasbinary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsBinary - stasgeojson: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsGeoJson - stastext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsText - stazimuth: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAzimuth - stboundary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBoundary - stcentroid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCentroid - stclosestpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STClosestPoint - stcontains: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContains - stcontainsproperly: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContainsProperly - stcoveredby: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCoveredBy - stcovers: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCovers - stcrosses: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCrosses - stdisjoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDisjoint - stdistance: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDistance - stdistancesphere: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDistanceSphere - stdwithin: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDWithin - stenvelope: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STEnvelope - stexpand: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STExpand - stextent: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STExtent - stgeomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STGeomFromText - stintersection: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersection - stintersects: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersects - stlength: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLength - stlinecrossingdirection: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineCrossingDirection - stlineinterpolatepoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineInterpolatePoint - stmakebox2d: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeBox2D - stmakeline: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeLine - stmakepoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakePoint - stperimeter: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPerimeter - stpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPoint - stscale: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STScale - stsetsrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSetSRID - stsimplify: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSimplify - ststartpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STStartPoint - stsummary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSummary - sttouches: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STTouches - sttransform: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STTransform - # for mysql - area: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Area - asbinary: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\AsBinary - astext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\AsText - buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Buffer - centroid: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Centroid - contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Contains - crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Crosses - dimension: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Dimension - distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Distance - disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Disjoint - distancefrommultyLine: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\DistanceFromMultyLine - endpoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\EndPoint - envelope: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Envelope - equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Equals - exteriorring: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\ExteriorRing - geodistpt: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeodistPt - geometrytype: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeometryType - geomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeomFromText - glength: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GLength - interiorringn: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\InteriorRingN - intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Intersects - isclosed: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsClosed - isempty: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsEmpty - issimple: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsSimple - linestringfromwkb: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\LineStringFromWKB - linestring: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\LineString - mbrcontains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRContains - mbrdisjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRDisjoint - mbrequal: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBREqual - mbrintersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRIntersects - mbroverlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBROverlaps - mbrtouches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRTouches - mbrwithin: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRWithin - numinteriorrings: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\NumInteriorRings - numpoints: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\NumPoints - overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Overlaps - pointfromwkb: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\PointFromWKB - pointn: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\PointN - point: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Point - srid: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\SRID - startpoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\StartPoint - st_buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STBuffer - st_contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STContains - st_crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STCrosses - st_disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDisjoint - st_distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDistance - st_equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STEquals - st_intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STIntersects - st_overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STOverlaps - st_touches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STTouches - st_within: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STWithin - touches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Touches - within: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Within - x: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\X - y: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Y + + orm: + default_entity_manager: default + auto_generate_proxy_classes: '%kernel.debug%' + entity_managers: + logging: + connection: logging + naming_strategy: doctrine.orm.naming_strategy.underscore + mappings: + Log: + is_bundle: false + type: annotation + dir: '%kernel.project_dir%/src/Entity/Logging' + prefix: 'App\Entity\Logging' + alias: Log + default: + connection: default + naming_strategy: doctrine.orm.naming_strategy.underscore + auto_mapping: true + mappings: + App: + is_bundle: false + type: annotation + dir: '%kernel.project_dir%/src/Entity' + prefix: 'App\Entity' + alias: App + dql: + string_functions: + # for postgresql + geometry: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\Geometry + stbuffer: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBuffer + stcollect: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCollect + stsnaptogrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSnapToGrid + stoverlaps: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STOverlaps + # for match against + match_against: App\Doctrine\MatchAgainst + numeric_functions: + # for postgresql + starea: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STArea + stasbinary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsBinary + stasgeojson: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsGeoJson + stastext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsText + stazimuth: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAzimuth + stboundary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBoundary + stcentroid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCentroid + stclosestpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STClosestPoint + stcontains: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContains + stcontainsproperly: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContainsProperly + stcoveredby: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCoveredBy + stcovers: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCovers + stcrosses: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCrosses + stdisjoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDisjoint + stdistance: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDistance + stdistancesphere: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDistanceSphere + stdwithin: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDWithin + stenvelope: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STEnvelope + stexpand: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STExpand + stextent: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STExtent + stgeomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STGeomFromText + stintersection: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersection + stintersects: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersects + stlength: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLength + stlinecrossingdirection: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineCrossingDirection + stlineinterpolatepoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineInterpolatePoint + stmakebox2d: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeBox2D + stmakeline: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeLine + stmakepoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakePoint + stperimeter: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPerimeter + stpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPoint + stscale: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STScale + stsetsrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSetSRID + stsimplify: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSimplify + ststartpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STStartPoint + stsummary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSummary + sttouches: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STTouches + sttransform: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STTransform + + # for mysql + area: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Area + asbinary: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\AsBinary + astext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\AsText + buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Buffer + centroid: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Centroid + contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Contains + crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Crosses + dimension: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Dimension + distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Distance + disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Disjoint + distancefrommultyLine: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\DistanceFromMultyLine + endpoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\EndPoint + envelope: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Envelope + equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Equals + exteriorring: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\ExteriorRing + geodistpt: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeodistPt + geometrytype: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeometryType + geomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeomFromText + glength: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GLength + interiorringn: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\InteriorRingN + intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Intersects + isclosed: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsClosed + isempty: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsEmpty + issimple: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsSimple + linestringfromwkb: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\LineStringFromWKB + linestring: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\LineString + mbrcontains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRContains + mbrdisjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRDisjoint + mbrequal: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBREqual + mbrintersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRIntersects + mbroverlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBROverlaps + mbrtouches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRTouches + mbrwithin: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRWithin + numinteriorrings: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\NumInteriorRings + numpoints: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\NumPoints + overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Overlaps + pointfromwkb: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\PointFromWKB + pointn: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\PointN + point: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Point + srid: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\SRID + startpoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\StartPoint + st_buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STBuffer + st_contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STContains + st_crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STCrosses + st_disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDisjoint + st_distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDistance + st_equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STEquals + st_intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STIntersects + st_overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STOverlaps + st_touches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STTouches + st_within: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STWithin + touches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Touches + within: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Within + x: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\X + y: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Y diff --git a/src/Entity/EntityLog.php b/src/Entity/Logging/EntityLog.php similarity index 98% rename from src/Entity/EntityLog.php rename to src/Entity/Logging/EntityLog.php index ccbf19dc..041addfb 100644 --- a/src/Entity/EntityLog.php +++ b/src/Entity/Logging/EntityLog.php @@ -1,6 +1,6 @@ Date: Tue, 3 Mar 2020 08:24:47 +0000 Subject: [PATCH 14/28] Remove postPersist. #330 --- config/services.yaml | 7 +- src/EventListener/EntityListener.php | 43 ++--- .../EntityListener/UserEntityListener.php | 152 +----------------- 3 files changed, 23 insertions(+), 179 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index ee9a444e..1184b00b 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -261,10 +261,9 @@ services: App\EventListener\EntityListener\UserEntityListener: arguments: - ["@security.token_storage"] + $token_storage: "@security.token_storage" + $log_em: "@doctrine.orm.logging_entity_manager" tags: - - name: 'doctrine.orm.entity_listener' - event: 'postPersist' - entity: 'App\Entity\User' - name: 'doctrine.event_listener' event: 'onFlush' + entity: 'App\Entity\User' diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index 15334b2d..45035120 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -4,6 +4,7 @@ namespace App\EventListener; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\Common\Persistence\Event\LifecycleEventArgs; use Doctrine\ORM\Event\OnFlushEventArgs; @@ -14,40 +15,16 @@ use DateTime; abstract class EntityListener { protected $token_storage; + protected $log_em; - public function __construct(TokenStorage $token_storage) + public function __construct(TokenStorage $token_storage, EntityManagerInterface $log_em) { $this->token_storage = $token_storage; - } - - 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(); - - // get user for logging - $user = $this->token_storage->getToken()->getUser(); - $username = $user->getUsername(); - - $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(); + $this->log_em = $log_em; } 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 @@ -64,11 +41,21 @@ abstract class EntityListener $field_changes = []; $unit_of_work = $em->getUnitOfWork(); + $created_entities = $unit_of_work->getScheduledEntityInsertions(); $deleted_entities = $unit_of_work->getScheduledEntityDeletions(); $updated_entities = $unit_of_work->getScheduledEntityUpdates(); $updated_collections = $unit_of_work->getScheduledCollectionUpdates(); $deleted_collections = $unit_of_work->getScheduledCollectionDeletions(); + // get objects for creation + foreach ($created_entities as $created_entity) + { + $log_message = 'Object with id ' . $created_entity->getID() . ' created.'; + + $entity_log->setAction('Create') + ->addChange('Object', $log_message); + } + // get updated fields. This doesn't include lists of other entities. foreach ($updated_entities as $updated_entity) { @@ -105,7 +92,7 @@ abstract class EntityListener ->addChange('Object', $log_message); } - //$em->persist($entity_log); + $this->log_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 diff --git a/src/EventListener/EntityListener/UserEntityListener.php b/src/EventListener/EntityListener/UserEntityListener.php index 3c447b91..5532bf50 100644 --- a/src/EventListener/EntityListener/UserEntityListener.php +++ b/src/EventListener/EntityListener/UserEntityListener.php @@ -4,163 +4,21 @@ namespace App\EventListener\EntityListener; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; -use Doctrine\Common\Persistence\Event\LifecycleEventArgs; -use Doctrine\ORM\Event\OnFlushEventArgs; +use Doctrine\ORM\EntityManagerInterface; use App\EventListener\EntityListener; -use App\Entity\User; -use App\Entity\Role; -use App\Entity\Hub; - -use App\Entity\Logging\EntityLog; - -use DateTime; - class UserEntityListener extends EntityListener { protected $token_storage; + protected $log_em; - public function __construct(TokenStorage $token_storage) + public function __construct(TokenStorage $token_storage, EntityManagerInterface $log_em) { $this->token_storage = $token_storage; + $this->log_em = $log_em; - parent::__construct($token_storage); + parent::__construct($token_storage, $log_em); } - /* - 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'); - - // get user for logging - $user = $this->token_storage->getToken()->getUser(); - $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) - //{ - /* - $em = $args->getEntityManager(); - $unit_of_work = $em->getUnitOfWork(); - $deleted_entities = $unit_of_work->getScheduledEntityDeletions(); - $updated_entities = $unit_of_work->getScheduledEntityUpdates(); - $updated_collections = $unit_of_work->getScheduledCollectionUpdates(); - $deleted_collections = $unit_of_work->getScheduledCollectionDeletions(); - - foreach ($updated_entities as $updated_entity) - { - //if ($updated_entity instanceof User) - //{ - $changeset = $unit_of_work->getEntityChangeSet($updated_entity); - - $entity_fields = array_keys($changeset); - foreach ($entity_fields as $field) - { - $values = $changeset[$field]; - - $old_value = $values[0]; - $new_value = $values[1]; - - error_log('Changed ' . $field . ' from ' . $old_value . ' to ' . $new_value); - } - //} - } - - foreach ($deleted_entities as $deleted_entity) - { - //if ($deleted_entity instanceof User) - //{ - $deleted_id = $deleted_entity->getID(); - $deleted_username = $deleted_entity->getUsername(); - - error_log('Deleted user with id ' . $deleted_id . ' and username ' . $deleted_username); - //} - } - - // 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 - foreach ($updated_collections as $updated_collection) - { - error_log('in updated_collection snippet'); - - foreach ($updated_collection as $item) - { - if ($item instanceof Role) - { - error_log('updated_collection item ' . $item->getName()); - } - if ($item instanceof Hub) - { - error_log('updated_collection item ' . $item->getName()); - } - } - - } - - foreach ($deleted_collections as $deleted_collection) - { - error_log('in deleted_collection snippet'); - - foreach ($deleted_collection as $item) - { - if ($item instanceof Role) - { - error_log('deleted_collection item ' . $item->getName()); - } - if ($item instanceof Hub) - { - error_log('deleted_collection item ' . $item->getName()); - } - } - - - } */ - - //} - - /* - public function postUpdate($user, LifecycleEventArgs $args) - { - error_log ('POST UPDATE'); - - error_log('User with id ' . $user->getID() . ' updated.'); - error_log('username ' . $user->getUsername()); - error_log('password ' . $user->getPassword()); - error_log('first name ' . $user->getFirstName()); - error_log('last name ' . $user->getLastName()); - error_log('contact number '. $user->getContactNumber()); - error_log('email ' . $user->getEmail()); - - $roles = $user->getRoles(); - $hubs = $user->getHubs(); - - foreach ($roles as $role) - error_log('user role ' . $role); - - foreach ($hubs as $hub) - error_log('user hub ' . $hub); - } */ - - /* - public function postRemove($user, LifecycleEventArgs $args) - { - error_log('User deleted.'); - } */ } -- 2.43.5 From b2154e28b8fb26df41df55dacef3d77f60c82419 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 4 Mar 2020 09:02:50 +0000 Subject: [PATCH 15/28] Use InfluxDB for logging. #330 --- composer.json | 1 + composer.lock | 65 ++++++++++++++++++- config/services.yaml | 10 ++- src/EventListener/EntityListener.php | 33 +++------- .../EntityListener/UserEntityListener.php | 12 ++-- symfony.lock | 3 + 6 files changed, 91 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index 7219d070..01f26a5e 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "data-dog/audit-bundle": "^0.1.10", "edwinhoksberg/php-fcm": "^1.0", "guzzlehttp/guzzle": "^6.3", + "influxdb/influxdb-php": "^1.15", "predis/predis": "^1.1", "sensio/framework-extra-bundle": "^5.1", "setasign/fpdf": "^1.8", diff --git a/composer.lock b/composer.lock index 08ee90e0..d3656b7d 100644 --- a/composer.lock +++ b/composer.lock @@ -1,10 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b101ecfbc1f6f2270f0e8ad326035b7e", + "content-hash": "cbde0e7f1fa49277c6196a3c677c3a51", "packages": [ { "name": "catalyst/auth-bundle", @@ -1836,6 +1836,67 @@ ], "time": "2019-07-01T23:21:34+00:00" }, + { + "name": "influxdb/influxdb-php", + "version": "1.15.0", + "source": { + "type": "git", + "url": "https://github.com/influxdata/influxdb-php.git", + "reference": "bf3415f81962e1ab8c939bc1a08a85f500bead35" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/influxdata/influxdb-php/zipball/bf3415f81962e1ab8c939bc1a08a85f500bead35", + "reference": "bf3415f81962e1ab8c939bc1a08a85f500bead35", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.0", + "php": "^5.5 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7" + }, + "suggest": { + "ext-curl": "Curl extension, needed for Curl driver", + "stefanotorresi/influxdb-php-async": "An asyncronous client for InfluxDB, implemented via ReactPHP." + }, + "type": "library", + "autoload": { + "psr-4": { + "InfluxDB\\": "src/InfluxDB" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gianluca Arbezzano", + "email": "gianarb92@gmail.com" + }, + { + "name": "Daniel Martinez", + "email": "danimartcas@hotmail.com" + }, + { + "name": "Stephen Hoogendijk", + "email": "stephen@tca0.nl" + } + ], + "description": "InfluxDB client library for PHP", + "keywords": [ + "client", + "influxdata", + "influxdb", + "influxdb class", + "influxdb client", + "influxdb library", + "time series" + ], + "time": "2019-05-30T00:15:14+00:00" + }, { "name": "jdorn/sql-formatter", "version": "v1.2.17", diff --git a/config/services.yaml b/config/services.yaml index 1184b00b..d5b9ee16 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -21,6 +21,14 @@ services: public: false # Allows optimizing the container by removing unused services; this also means # fetching services directly from the container via $container->get() won't work. # The best practice is to be explicit about your dependencies anyway. + + # influxdb + influxdb_client: + class: InfluxDB\Client + arguments: ['%env(INFLUXDB_HOST)%', '%env(INFLUXDB_PORT)%'] + influxdb_database: + class: InfluxDB\Database + arguments: ['%env(INFLUXDB_DB)%', '@influxdb_client'] # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name @@ -262,7 +270,7 @@ services: App\EventListener\EntityListener\UserEntityListener: arguments: $token_storage: "@security.token_storage" - $log_em: "@doctrine.orm.logging_entity_manager" + $log_db: '@influxdb_database' tags: - name: 'doctrine.event_listener' event: 'onFlush' diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index 45035120..afd8d780 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -2,25 +2,24 @@ namespace App\EventListener; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; -use Doctrine\ORM\EntityManagerInterface; use Doctrine\Common\Persistence\Event\LifecycleEventArgs; use Doctrine\ORM\Event\OnFlushEventArgs; -use App\Entity\Logging\EntityLog; +use InfluxDB\Point; use DateTime; abstract class EntityListener { protected $token_storage; - protected $log_em; + protected $log_db; - public function __construct(TokenStorage $token_storage, EntityManagerInterface $log_em) + public function __construct(TokenStorageInterface $token_storage, $log_db) { $this->token_storage = $token_storage; - $this->log_em = $log_em; + $this->log_db = $log_db; } public function onFlush(OnFlushEventArgs $args) @@ -34,10 +33,6 @@ abstract class EntityListener $user = $this->token_storage->getToken()->getUser(); $username = $user->getUsername(); - $entity_log = new EntityLog(); - $entity_log->setUser($username) - ->setDateAction($event_time); - $field_changes = []; $unit_of_work = $em->getUnitOfWork(); @@ -51,9 +46,6 @@ abstract class EntityListener foreach ($created_entities as $created_entity) { $log_message = 'Object with id ' . $created_entity->getID() . ' created.'; - - $entity_log->setAction('Create') - ->addChange('Object', $log_message); } // get updated fields. This doesn't include lists of other entities. @@ -74,12 +66,6 @@ abstract class EntityListener 'new_value' => $values[1], ]; } - - $entity_log->setAction('Update'); - if ($updated_entity instanceof User) - { - $entity_log->addChange('User', $field_changes); - } } // get deleted objects @@ -87,12 +73,13 @@ abstract class EntityListener { $deleted_id = $deleted_entity->getID(); $log_message = 'Object with id ' . $deleted_id . ' deleted.'; - - $entity_log->setAction('Delete') - ->addChange('Object', $log_message); } - $this->log_em->persist($entity_log); + $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 diff --git a/src/EventListener/EntityListener/UserEntityListener.php b/src/EventListener/EntityListener/UserEntityListener.php index 5532bf50..24e18543 100644 --- a/src/EventListener/EntityListener/UserEntityListener.php +++ b/src/EventListener/EntityListener/UserEntityListener.php @@ -2,23 +2,21 @@ namespace App\EventListener\EntityListener; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; - -use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use App\EventListener\EntityListener; class UserEntityListener extends EntityListener { protected $token_storage; - protected $log_em; + protected $log_db; - public function __construct(TokenStorage $token_storage, EntityManagerInterface $log_em) + public function __construct(TokenStorageInterface $token_storage, $log_db) { $this->token_storage = $token_storage; - $this->log_em = $log_em; + $this->log_db = $log_db; - parent::__construct($token_storage, $log_em); + parent::__construct($token_storage, $log_db); } } diff --git a/symfony.lock b/symfony.lock index 116ed675..1f800f3e 100644 --- a/symfony.lock +++ b/symfony.lock @@ -110,6 +110,9 @@ "guzzlehttp/psr7": { "version": "1.4.2" }, + "influxdb/influxdb-php": { + "version": "1.15.0" + }, "jdorn/sql-formatter": { "version": "v1.2.17" }, -- 2.43.5 From eb0fd34eecdf7682de34a5c6ee99d1b6e58785c5 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 5 Mar 2020 01:43:37 +0000 Subject: [PATCH 16/28] Remove EntityLog and revert doctrine.yaml for one entity manager. #330 --- config/packages/doctrine.yaml | 269 ++++++++++++++----------------- src/Entity/Logging/EntityLog.php | 107 ------------ 2 files changed, 121 insertions(+), 255 deletions(-) delete mode 100644 src/Entity/Logging/EntityLog.php diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index b425f88f..b1b6d947 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -4,163 +4,136 @@ parameters: # environment variables are not available yet. # You should not need to change this value. env(DATABASE_URL): '' - env(LOGGING_DATABASE_URL): '' doctrine: dbal: - default_connection: default - connections: - default: - # configure these for your database server - driver: 'pdo_mysql' - server_version: '5.7' - charset: utf8mb4 + # configure these for your database server + driver: 'pdo_mysql' + server_version: '5.7' + charset: utf8mb4 - # With Symfony 3.3, remove the `resolve:` prefix - url: '%env(resolve:DATABASE_URL)%' - logging: - # configure these for your database server - url: '%env(resolve:LOGGING_DATABASE_URL)%' - driver: 'pdo_mysql' - server_version: '5.7' - charset: utf8mb4 + # With Symfony 3.3, remove the `resolve:` prefix + url: '%env(resolve:DATABASE_URL)%' types: geometry: CrEOF\Spatial\DBAL\Types\GeometryType point: CrEOF\Spatial\DBAL\Types\Geometry\PointType polygon: CrEOF\Spatial\DBAL\Types\Geometry\PolygonType linestring: CrEOF\Spatial\DBAL\Types\Geometry\LineStringType - multipolygon: CrEOF\Spatial\DBAL\Types\Geometry\MultiPolygonType - - orm: - default_entity_manager: default auto_generate_proxy_classes: '%kernel.debug%' - entity_managers: - logging: - connection: logging - naming_strategy: doctrine.orm.naming_strategy.underscore - mappings: - Log: - is_bundle: false - type: annotation - dir: '%kernel.project_dir%/src/Entity/Logging' - prefix: 'App\Entity\Logging' - alias: Log - default: - connection: default - naming_strategy: doctrine.orm.naming_strategy.underscore - auto_mapping: true - mappings: - App: - is_bundle: false - type: annotation - dir: '%kernel.project_dir%/src/Entity' - prefix: 'App\Entity' - alias: App - dql: - string_functions: - # for postgresql - geometry: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\Geometry - stbuffer: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBuffer - stcollect: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCollect - stsnaptogrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSnapToGrid - stoverlaps: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STOverlaps - # for match against - match_against: App\Doctrine\MatchAgainst - numeric_functions: - # for postgresql - starea: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STArea - stasbinary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsBinary - stasgeojson: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsGeoJson - stastext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsText - stazimuth: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAzimuth - stboundary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBoundary - stcentroid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCentroid - stclosestpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STClosestPoint - stcontains: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContains - stcontainsproperly: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContainsProperly - stcoveredby: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCoveredBy - stcovers: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCovers - stcrosses: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCrosses - stdisjoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDisjoint - stdistance: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDistance - stdistancesphere: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDistanceSphere - stdwithin: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDWithin - stenvelope: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STEnvelope - stexpand: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STExpand - stextent: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STExtent - stgeomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STGeomFromText - stintersection: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersection - stintersects: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersects - stlength: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLength - stlinecrossingdirection: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineCrossingDirection - stlineinterpolatepoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineInterpolatePoint - stmakebox2d: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeBox2D - stmakeline: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeLine - stmakepoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakePoint - stperimeter: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPerimeter - stpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPoint - stscale: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STScale - stsetsrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSetSRID - stsimplify: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSimplify - ststartpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STStartPoint - stsummary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSummary - sttouches: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STTouches - sttransform: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STTransform + naming_strategy: doctrine.orm.naming_strategy.underscore + auto_mapping: true + mappings: + App: + is_bundle: false + type: annotation + dir: '%kernel.project_dir%/src/Entity' + prefix: 'App\Entity' + alias: App + dql: + string_functions: + # for postgresql + geometry: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\Geometry + stbuffer: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBuffer + stcollect: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCollect + stsnaptogrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSnapToGrid + stoverlaps: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STOverlaps + # for match against + match_against: App\Doctrine\MatchAgainst + numeric_functions: + # for postgresql + starea: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STArea + stasbinary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsBinary + stasgeojson: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsGeoJson + stastext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAsText + stazimuth: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STAzimuth + stboundary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBoundary + stcentroid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCentroid + stclosestpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STClosestPoint + stcontains: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContains + stcontainsproperly: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContainsProperly + stcoveredby: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCoveredBy + stcovers: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCovers + stcrosses: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCrosses + stdisjoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDisjoint + stdistance: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDistance + stdistancesphere: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDistanceSphere + stdwithin: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STDWithin + stenvelope: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STEnvelope + stexpand: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STExpand + stextent: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STExtent + stgeomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STGeomFromText + stintersection: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersection + stintersects: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersects + stlength: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLength + stlinecrossingdirection: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineCrossingDirection + stlineinterpolatepoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineInterpolatePoint + stmakebox2d: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeBox2D + stmakeline: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeLine + stmakepoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakePoint + stperimeter: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPerimeter + stpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPoint + stscale: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STScale + stsetsrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSetSRID + stsimplify: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSimplify + ststartpoint: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STStartPoint + stsummary: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSummary + sttouches: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STTouches + sttransform: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STTransform - # for mysql - area: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Area - asbinary: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\AsBinary - astext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\AsText - buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Buffer - centroid: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Centroid - contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Contains - crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Crosses - dimension: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Dimension - distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Distance - disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Disjoint - distancefrommultyLine: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\DistanceFromMultyLine - endpoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\EndPoint - envelope: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Envelope - equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Equals - exteriorring: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\ExteriorRing - geodistpt: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeodistPt - geometrytype: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeometryType - geomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeomFromText - glength: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GLength - interiorringn: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\InteriorRingN - intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Intersects - isclosed: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsClosed - isempty: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsEmpty - issimple: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsSimple - linestringfromwkb: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\LineStringFromWKB - linestring: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\LineString - mbrcontains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRContains - mbrdisjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRDisjoint - mbrequal: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBREqual - mbrintersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRIntersects - mbroverlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBROverlaps - mbrtouches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRTouches - mbrwithin: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRWithin - numinteriorrings: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\NumInteriorRings - numpoints: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\NumPoints - overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Overlaps - pointfromwkb: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\PointFromWKB - pointn: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\PointN - point: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Point - srid: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\SRID - startpoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\StartPoint - st_buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STBuffer - st_contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STContains - st_crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STCrosses - st_disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDisjoint - st_distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDistance - st_equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STEquals - st_intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STIntersects - st_overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STOverlaps - st_touches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STTouches - st_within: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STWithin - touches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Touches - within: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Within - x: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\X - y: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Y + # for mysql + area: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Area + asbinary: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\AsBinary + astext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\AsText + buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Buffer + centroid: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Centroid + contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Contains + crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Crosses + dimension: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Dimension + distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Distance + disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Disjoint + distancefrommultyLine: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\DistanceFromMultyLine + endpoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\EndPoint + envelope: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Envelope + equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Equals + exteriorring: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\ExteriorRing + geodistpt: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeodistPt + geometrytype: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeometryType + geomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeomFromText + glength: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GLength + interiorringn: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\InteriorRingN + intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Intersects + isclosed: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsClosed + isempty: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsEmpty + issimple: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\IsSimple + linestringfromwkb: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\LineStringFromWKB + linestring: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\LineString + mbrcontains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRContains + mbrdisjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRDisjoint + mbrequal: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBREqual + mbrintersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRIntersects + mbroverlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBROverlaps + mbrtouches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRTouches + mbrwithin: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\MBRWithin + numinteriorrings: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\NumInteriorRings + numpoints: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\NumPoints + overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Overlaps + pointfromwkb: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\PointFromWKB + pointn: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\PointN + point: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Point + srid: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\SRID + startpoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\StartPoint + st_buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STBuffer + st_contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STContains + st_crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STCrosses + st_disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDisjoint + st_distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDistance + st_equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STEquals + st_intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STIntersects + st_overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STOverlaps + st_touches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STTouches + st_within: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STWithin + touches: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Touches + within: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Within + x: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\X + y: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Y diff --git a/src/Entity/Logging/EntityLog.php b/src/Entity/Logging/EntityLog.php deleted file mode 100644 index 041addfb..00000000 --- a/src/Entity/Logging/EntityLog.php +++ /dev/null @@ -1,107 +0,0 @@ -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; - } -} -- 2.43.5 From 485865901a1eefc2a2f3dc4bcaa9f005487725a2 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 5 Mar 2020 03:54:33 +0000 Subject: [PATCH 17/28] Add postPersist to listener. Add fieldDump function to user. #330 --- src/Entity/User.php | 4 ++++ src/EventListener/EntityListener.php | 29 ++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Entity/User.php b/src/Entity/User.php index a57afb97..491a1af6 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -309,4 +309,8 @@ class User extends BaseUser implements Serializable { return $this->partners_created; } + + public function fieldDump() + { + } } diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index afd8d780..6d9190d1 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -22,6 +22,20 @@ abstract class EntityListener $this->log_db = $log_db; } + public function postPersist($object, LifecycleEventArgs $args) + { + $event_time = new DateTime(); + + // get user for logging + $user = $this->token_storage->getToken()->getUser(); + $username = $user->getUsername(); + + $obj_class = preg_replace('/.*\\\\/', '', get_class($object)); + $log_message = $obj_class . ' with id ' . $object->getID() . ' created.'; + + error_log($log_message); + } + public function onFlush(OnFlushEventArgs $args) { $em = $args->getEntityManager(); @@ -45,7 +59,10 @@ abstract class EntityListener // get objects for creation foreach ($created_entities as $created_entity) { - $log_message = 'Object with id ' . $created_entity->getID() . ' created.'; + $obj_class = preg_replace('/.*\\\\/', '', get_class($created_entity)); + $log_message = $obj_class . ' with id ' . $created_entity->getUsername() . ' created.'; + + error_log($log_message); } // get updated fields. This doesn't include lists of other entities. @@ -75,11 +92,11 @@ abstract class EntityListener $log_message = 'Object with id ' . $deleted_id . ' deleted.'; } - $this->log_db->writePoints([new Point( - 'User', - 'Action', - ['status' => 'success'] - )]); + //$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 -- 2.43.5 From eff3dc94ec98c17d74564b5e68ba274259e1d8bf Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 6 Mar 2020 06:20:22 +0000 Subject: [PATCH 18/28] Add fieldDump() to User. Add writing to logging db. #330 --- config/services.yaml | 3 ++ src/Entity/User.php | 16 +++++++ src/EventListener/EntityListener.php | 63 ++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index d5b9ee16..59f4fb0a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -275,3 +275,6 @@ services: - name: 'doctrine.event_listener' event: 'onFlush' entity: 'App\Entity\User' + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\User' diff --git a/src/Entity/User.php b/src/Entity/User.php index 491a1af6..12414190 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -312,5 +312,21 @@ class User extends BaseUser implements Serializable public function fieldDump() { + // get all fields and their values into an array + $field_array = [ + 'id' => $this->getID(), + 'username' => $this->getUsername(), + 'password' => $this->getPassword(), + 'first_name' => $this->getFirstName(), + 'last_name' => $this->getLastName(), + 'email' => $this->getEmail(), + 'contact_number' => $this->getContactNumber(), + 'roles' => $this->getRoles(), + 'hubs' => $this->getHubs(), + ]; + + $fields = json_encode($field_array); + + return $fields; } } diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index 6d9190d1..91adff0c 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -24,16 +24,23 @@ abstract class EntityListener public function postPersist($object, LifecycleEventArgs $args) { - $event_time = new DateTime(); + //$event_time = new DateTime(); - // get user for logging + // get user id for logging $user = $this->token_storage->getToken()->getUser(); - $username = $user->getUsername(); + //$username = $user->getUsername(); + $user_id = $user->getID(); + // get entity name $obj_class = preg_replace('/.*\\\\/', '', get_class($object)); - $log_message = $obj_class . ' with id ' . $object->getID() . ' created.'; + $log_message = $obj_class . ' with id ' . $object->getID() . ' created by ' . $user_id; - error_log($log_message); + // get fields of object + $fields = $object->fieldDump(); + + error_log($log_message . ' with ' . $fields); + + $this->writeToLogDB($obj_class, $object->getID(), 'create', $fields, $user_id); } public function onFlush(OnFlushEventArgs $args) @@ -41,11 +48,11 @@ abstract class EntityListener $em = $args->getEntityManager(); // get date time of event for logging - $event_time = new DateTime(); + //$event_time = new DateTime(); // get user for logging $user = $this->token_storage->getToken()->getUser(); - $username = $user->getUsername(); + $user_id = $user->getID(); $field_changes = []; @@ -56,18 +63,15 @@ abstract class EntityListener $updated_collections = $unit_of_work->getScheduledCollectionUpdates(); $deleted_collections = $unit_of_work->getScheduledCollectionDeletions(); - // get objects for creation - foreach ($created_entities as $created_entity) - { - $obj_class = preg_replace('/.*\\\\/', '', get_class($created_entity)); - $log_message = $obj_class . ' with id ' . $created_entity->getUsername() . ' created.'; - - error_log($log_message); - } - // 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); @@ -83,13 +87,25 @@ abstract class EntityListener 'new_value' => $values[1], ]; } + + $this->writeToLogDB($obj_class, $updated_entity->getID(), 'update', $field_changes, $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; + + error_log($log_message); + $deleted_id = $deleted_entity->getID(); - $log_message = 'Object with id ' . $deleted_id . ' deleted.'; + + // get fields of deleted entity + $fields = $deleted_entity->fieldDump(); + + $this->writeToLogDB($obj_class, $deleted_id, 'delete', $fields, $user_id); } //$this->log_db->writePoints([new Point( @@ -137,4 +153,17 @@ abstract class EntityListener } */ } + + protected function writeToLogDB($entity_type, $entity_id, $action, $content, $user_id) + { + $new_point = new Point( + 'entity_log', // measurement + null, // measurement value + ['entity_type' => $entity_type, 'entity_id' => $entity_id], // measurement tags + ['action' => $action, 'content' => $content, 'user' => $user_id], // measurement fields + exec('date +%s%N') // timestamp in nanoseconds on Linux only + ); + + $this->log_db->writePoints([$new_point]); + } } -- 2.43.5 From e305dc4c16be3d932bdfebdd0466363d73f1a9ee Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 6 Mar 2020 08:53:45 +0000 Subject: [PATCH 19/28] Fix bugs found during testing. #330 --- src/EventListener/EntityListener.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index 91adff0c..f02a0e10 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -33,13 +33,10 @@ abstract class EntityListener // get entity name $obj_class = preg_replace('/.*\\\\/', '', get_class($object)); - $log_message = $obj_class . ' with id ' . $object->getID() . ' created by ' . $user_id; // get fields of object $fields = $object->fieldDump(); - error_log($log_message . ' with ' . $fields); - $this->writeToLogDB($obj_class, $object->getID(), 'create', $fields, $user_id); } @@ -88,7 +85,9 @@ abstract class EntityListener ]; } - $this->writeToLogDB($obj_class, $updated_entity->getID(), 'update', $field_changes, $user_id); + $fields = json_encode($field_changes); + + $this->writeToLogDB($obj_class, $updated_entity->getID(), 'update', $fields, $user_id); } // get deleted objects -- 2.43.5 From b743d202fbcc9a1626e07139fcb116eb08947ddb Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 9 Mar 2020 06:08:07 +0000 Subject: [PATCH 20/28] Reworked the entity listener. #330 --- config/services.yaml | 7 +- src/Entity/Role.php | 14 +++ src/EventListener/EntityListener.php | 111 ++++++++++-------- .../EntityListener/UserEntityListener.php | 22 ---- 4 files changed, 77 insertions(+), 77 deletions(-) delete mode 100644 src/EventListener/EntityListener/UserEntityListener.php diff --git a/config/services.yaml b/config/services.yaml index 59f4fb0a..e0df8209 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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' diff --git a/src/Entity/Role.php b/src/Entity/Role.php index 38fcc0b2..af526954 100644 --- a/src/Entity/Role.php +++ b/src/Entity/Role.php @@ -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; + } } diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index f02a0e10..2de40623 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -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 /* diff --git a/src/EventListener/EntityListener/UserEntityListener.php b/src/EventListener/EntityListener/UserEntityListener.php deleted file mode 100644 index 24e18543..00000000 --- a/src/EventListener/EntityListener/UserEntityListener.php +++ /dev/null @@ -1,22 +0,0 @@ -token_storage = $token_storage; - $this->log_db = $log_db; - - parent::__construct($token_storage, $log_db); - } - -} -- 2.43.5 From d7806a26647462e83bd7c96bb728e39dff320f56 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 9 Mar 2020 07:21:45 +0000 Subject: [PATCH 21/28] Add AuditableEntity interface. #330 --- src/Entity/AuditableEntity.php | 8 +++ src/Entity/Role.php | 2 +- src/Entity/User.php | 2 +- src/EventListener/EntityListener.php | 75 ++++++++++++++++------------ 4 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 src/Entity/AuditableEntity.php diff --git a/src/Entity/AuditableEntity.php b/src/Entity/AuditableEntity.php new file mode 100644 index 00000000..f06e4cd8 --- /dev/null +++ b/src/Entity/AuditableEntity.php @@ -0,0 +1,8 @@ +entities[$object_class])) { - // get user id for logging - $user = $this->token_storage->getToken()->getUser(); - $user_id = $user->getID(); + if ($object instanceof AuditableEntity) + { + // get user id for logging + $user = $this->token_storage->getToken()->getUser(); + $user_id = $user->getID(); - // get entity name - //$obj_class = preg_replace('/.*\\\\/', '', $object_class); + // get entity name + //$obj_class = preg_replace('/.*\\\\/', '', $object_class); - // get fields of object - $fields = $object->fieldDump(); + // get fields of object + $fields = $object->fieldDump(); - $this->writeToLogDB($object_class, $object->getID(), 'create', $fields, $user_id); + $this->writeToLogDB($object_class, $object->getID(), 'create', $fields, $user_id); + } } } @@ -78,28 +83,31 @@ class EntityListener $entity_class = get_class($updated_entity); if (isset($this->entities[$entity_class])) { - // get entity name - //$obj_class = preg_replace('/.*\\\\/', '', $entity_class); - - $changeset = $unit_of_work->getEntityChangeSet($updated_entity); - - $entity_fields = array_keys($changeset); - foreach ($entity_fields as $field) + if ($updated_entity instanceof AuditableEntity) { - $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($entity_class, $updated_entity->getID(), 'update', $fields, $user_id); } } @@ -110,15 +118,18 @@ class EntityListener $entity_class = get_class($deleted_entity); if (isset($this->entities[$entity_class])) { - // get entity name - //$obj_class = preg_replace('/.*\\\\/', '', get_class($deleted_entity)); + if ($deleted_entity instanceof AuditableEntity) + { + // get entity name + //$obj_class = preg_replace('/.*\\\\/', '', get_class($deleted_entity)); - $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($entity_class, $deleted_id, 'delete', $fields, $user_id); + $this->writeToLogDB($entity_class, $deleted_id, 'delete', $fields, $user_id); + } } } -- 2.43.5 From 5b167ef680e52734ef873669d935a6cd38e71742 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 9 Mar 2020 07:30:11 +0000 Subject: [PATCH 22/28] Add logging to error_log if entity has no fieldDump method. #330 --- config/services.yaml | 2 +- src/EventListener/EntityListener.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/config/services.yaml b/config/services.yaml index e0df8209..3e7e3604 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -271,7 +271,7 @@ services: arguments: $token_storage: "@security.token_storage" $log_db: '@influxdb_database' - $entities: ['App\Entity\User', 'App\Entity\Role'] + $entities: ['App\Entity\User', 'App\Entity\Role', 'App\Entity\Partner'] tags: - name: 'doctrine.event_listener' event: 'onFlush' diff --git a/src/EventListener/EntityListener.php b/src/EventListener/EntityListener.php index 708f2564..0a4ee0f4 100644 --- a/src/EventListener/EntityListener.php +++ b/src/EventListener/EntityListener.php @@ -53,6 +53,8 @@ class EntityListener $this->writeToLogDB($object_class, $object->getID(), 'create', $fields, $user_id); } + else + error_log($object_class . ' does not implement the AuditableEntity interface.'); } } @@ -108,6 +110,8 @@ class EntityListener $this->writeToLogDB($entity_class, $updated_entity->getID(), 'update', $fields, $user_id); } + else + error_log($entity_class . ' does not implement the AuditableEntity interface.'); } } @@ -130,6 +134,8 @@ class EntityListener $this->writeToLogDB($entity_class, $deleted_id, 'delete', $fields, $user_id); } + else + error_log($entity_class . ' does not implement the AuditableEntity interface.'); } } -- 2.43.5 From afeb06006dc259f57c6ff8b5eb3663f3fe574a2d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 10 Mar 2020 04:30:19 +0000 Subject: [PATCH 23/28] Add View Audit Logs to view/edit user. #330 --- config/acl.yaml | 2 + config/routes/user.yaml | 10 +++ config/services.yaml | 10 +-- src/Controller/UserController.php | 47 ++++++++++++ templates/user/form.html.twig | 120 +++++++++++++++--------------- templates/user/log.html.twig | 79 ++++++++++++++++++++ 6 files changed, 204 insertions(+), 64 deletions(-) create mode 100644 templates/user/log.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index c6ccc243..1ecbfcdb 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -21,6 +21,8 @@ access_keys: label: Super Admin Role - id: user.profile label: User Profile + - id: user.logs + label: User Logs - id: role label: Role Access acls: diff --git a/config/routes/user.yaml b/config/routes/user.yaml index 5b8dedbe..171d4f93 100644 --- a/config/routes/user.yaml +++ b/config/routes/user.yaml @@ -41,3 +41,13 @@ user_profile_submit: path: /profile controller: App\Controller\UserController::profileSubmit methods: [POST] + +user_view_logs_form: + path: /users/{id}/logs + controller: App\Controller\UserController::viewLogsForm + methods: [GET] + +user_view_logs: + path: /user/{id}/logs + controller: App\Controller\UserController::getLogs + methods: [POST] diff --git a/config/services.yaml b/config/services.yaml index 3e7e3604..ba4ed655 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -23,12 +23,10 @@ services: # The best practice is to be explicit about your dependencies anyway. # influxdb - influxdb_client: - class: InfluxDB\Client + InfluxDB\Client: arguments: ['%env(INFLUXDB_HOST)%', '%env(INFLUXDB_PORT)%'] - influxdb_database: - class: InfluxDB\Database - arguments: ['%env(INFLUXDB_DB)%', '@influxdb_client'] + InfluxDB\Database: + arguments: ['%env(INFLUXDB_DB)%', "@InfluxDB\\Client"] # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name @@ -270,7 +268,7 @@ services: App\EventListener\EntityListener: arguments: $token_storage: "@security.token_storage" - $log_db: '@influxdb_database' + $log_db: "@InfluxDB\\Database" $entities: ['App\Entity\User', 'App\Entity\Role', 'App\Entity\Partner'] tags: - name: 'doctrine.event_listener' diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index d9488e81..5a6af055 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -15,6 +15,8 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Catalyst\MenuBundle\Annotation\Menu; +use InfluxDB\Client; + class UserController extends Controller { /** @@ -484,4 +486,49 @@ class UserController extends Controller ]); } } + + /** + * @Menu(selected="user_list") + */ + public function viewLogsForm($id) + { + $this->denyAccessUnlessGranted('user.logs', null, 'No access.'); + + $params['id'] = $id; + + // response + return $this->render('user/log.html.twig', $params); + } + + public function getLogs(Client $client, $id) + { + error_log('in getLogs'); + + // fetch database + // TODO: find way to replace hardcoded db name + $database = $client->selectDB('logging_db'); + + // query will return a resultset object + $result = $database->query('SELECT * FROM entity_log'); + + // get the points from the resultset, which is an array + $points = $result->getPoints(); + + error_log('getLogs count points ' . count($points)); + + // array has format + /* + $rows = []; + foreach ($points as $point) + { + $row['time'] = $point['time']; + error_log($point['entity_type']); + + error_log($point['action']); + } */ + + return $this->json([ + 'data' => $points, + ]); + } } diff --git a/templates/user/form.html.twig b/templates/user/form.html.twig index efa35dc4..dba00ef0 100644 --- a/templates/user/form.html.twig +++ b/templates/user/form.html.twig @@ -57,6 +57,9 @@ Unique alias for this user +
@@ -182,72 +185,73 @@ {% endblock %} {% block scripts %} - + // remove all error classes + function removeErrors() { + $(".form-control-danger").removeClass('form-control-danger'); + $("[data-field]").removeClass('has-danger'); + $(".form-control-feedback[data-field]").addClass('hide'); + } + }); + + {% endblock %} diff --git a/templates/user/log.html.twig b/templates/user/log.html.twig new file mode 100644 index 00000000..bd8f3655 --- /dev/null +++ b/templates/user/log.html.twig @@ -0,0 +1,79 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Audit Logs

+
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} + -- 2.43.5 From 025e14ff699151cd6073c96b94d44a3b057453c3 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 10 Mar 2020 05:44:18 +0000 Subject: [PATCH 24/28] Add TODOs for view audit logs. #330 --- src/Controller/UserController.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 5a6af055..45279031 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -502,30 +502,18 @@ class UserController extends Controller public function getLogs(Client $client, $id) { - error_log('in getLogs'); - // fetch database // TODO: find way to replace hardcoded db name $database = $client->selectDB('logging_db'); // query will return a resultset object + // TODO: filter query using user id $result = $database->query('SELECT * FROM entity_log'); // get the points from the resultset, which is an array $points = $result->getPoints(); - error_log('getLogs count points ' . count($points)); - - // array has format - /* - $rows = []; - foreach ($points as $point) - { - $row['time'] = $point['time']; - error_log($point['entity_type']); - - error_log($point['action']); - } */ + // TODO: might need to format time data return $this->json([ 'data' => $points, -- 2.43.5 From 882a36f705f1bd8ebbc71050b466d985ec877418 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 11 Mar 2020 02:22:16 +0000 Subject: [PATCH 25/28] Filter logs by user id. #330 --- src/Controller/UserController.php | 4 ++-- templates/user/form.html.twig | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 45279031..e2d9085d 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -507,8 +507,8 @@ class UserController extends Controller $database = $client->selectDB('logging_db'); // query will return a resultset object - // TODO: filter query using user id - $result = $database->query('SELECT * FROM entity_log'); + $query_string = 'SELECT * FROM entity_log WHERE "user" = ' . $id; + $result = $database->query($query_string); // get the points from the resultset, which is an array $points = $result->getPoints(); diff --git a/templates/user/form.html.twig b/templates/user/form.html.twig index dba00ef0..f6502795 100644 --- a/templates/user/form.html.twig +++ b/templates/user/form.html.twig @@ -58,7 +58,9 @@ Unique alias for this user
- View Audit Logs + {% if mode == 'update' %} + View Audit Logs + {% endif %}
-- 2.43.5 From 29add2f078f467e14fa9dadd1bd494942ced55a6 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 11 Mar 2020 02:42:56 +0000 Subject: [PATCH 26/28] Add the name for the logs db to .env. Add parameter for logs db to services.yaml. #330 --- .env.dist | 5 +++++ config/services.yaml | 13 +++++++------ src/Controller/UserController.php | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.env.dist b/.env.dist index b6a80203..9f173e4b 100644 --- a/.env.dist +++ b/.env.dist @@ -18,6 +18,11 @@ LOGGING_DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_logging ###< doctrine/doctrine-bundle ### GMAPS_API_KEY=insert_gmapsapikey_here +# influxdb +INFLUXDB_HOST=127.0.0.1 +INFLUXDB_PORT=8086 +INFLUXDB_DB=logging_db + # rising tide sms gateway RT_USER=rt_user RT_PASS=rt_pass diff --git a/config/services.yaml b/config/services.yaml index ba4ed655..1a72a956 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -12,6 +12,7 @@ parameters: app_access_key: 'access_keys' cvu_brand_id: "%env(CVU_BRAND_ID)%" country_code: "%env(COUNTRY_CODE)%" + log_db: "%env(INFLUXDB_DB)%" services: # default configuration for services in *this* file @@ -22,12 +23,6 @@ services: # fetching services directly from the container via $container->get() won't work. # The best practice is to be explicit about your dependencies anyway. - # influxdb - InfluxDB\Client: - arguments: ['%env(INFLUXDB_HOST)%', '%env(INFLUXDB_PORT)%'] - InfluxDB\Database: - arguments: ['%env(INFLUXDB_DB)%', "@InfluxDB\\Client"] - # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name App\: @@ -227,6 +222,12 @@ services: #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Bing" App\Service\GISManagerInterface: "@App\\Service\\GISManager\\OpenStreet" #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Google" + + # influxdb + InfluxDB\Client: + arguments: ['%env(INFLUXDB_HOST)%', '%env(INFLUXDB_PORT)%'] + InfluxDB\Database: + arguments: ['%env(INFLUXDB_DB)%', "@InfluxDB\\Client"] App\EventListener\JobOrderActiveCacheListener: arguments: diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index e2d9085d..8bd441aa 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -503,8 +503,8 @@ class UserController extends Controller public function getLogs(Client $client, $id) { // fetch database - // TODO: find way to replace hardcoded db name - $database = $client->selectDB('logging_db'); + $log_db = $this->getParameter('log_db'); + $database = $client->selectDB($log_db); // query will return a resultset object $query_string = 'SELECT * FROM entity_log WHERE "user" = ' . $id; -- 2.43.5 From eb1ce1a97f13af345f1b8d08ceb2fbd0794b1e5a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 11 Mar 2020 02:49:53 +0000 Subject: [PATCH 27/28] Add influxdb and entity listener configuration to cmb and resq services.yaml. #330 --- config/cmb.services.yaml | 19 ++++++++++++++----- config/resq.services.yaml | 21 +++++++++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/config/cmb.services.yaml b/config/cmb.services.yaml index f3051cff..46e348b9 100644 --- a/config/cmb.services.yaml +++ b/config/cmb.services.yaml @@ -12,6 +12,7 @@ parameters: app_access_key: 'access_keys' cvu_brand_id: "%env(CVU_BRAND_ID)%" country_code: "%env(COUNTRY_CODE)%" + log_db: "%env(INFLUXDB_DB)%" services: # default configuration for services in *this* file @@ -201,6 +202,12 @@ services: #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Bing" App\Service\GISManagerInterface: "@App\\Service\\GISManager\\OpenStreet" #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Google" + + # influxdb + InfluxDB\Client: + arguments: ['%env(INFLUXDB_HOST)%', '%env(INFLUXDB_PORT)%'] + InfluxDB\Database: + arguments: ['%env(INFLUXDB_DB)%', "@InfluxDB\\Client"] App\EventListener\JobOrderActiveCacheListener: arguments: @@ -239,12 +246,14 @@ services: event: 'postPersist' entity: 'App\Entity\JobOrder' - App\EventListener\EntityListener\UserEntityListener: + App\EventListener\EntityListener: arguments: - ["@security.token_storage"] + $token_storage: "@security.token_storage" + $log_db: "@InfluxDB\\Database" + $entities: ['App\Entity\User', 'App\Entity\Role', 'App\Entity\Partner'] tags: - - name: 'doctrine.orm.entity_listener' - event: 'postPersist' - entity: 'App\Entity\User' - name: 'doctrine.event_listener' event: 'onFlush' + - name: 'doctrine.event_listener' + event: 'postPersist' + diff --git a/config/resq.services.yaml b/config/resq.services.yaml index 3a3c9e68..b487ed82 100644 --- a/config/resq.services.yaml +++ b/config/resq.services.yaml @@ -12,6 +12,7 @@ parameters: app_access_key: 'access_keys' cvu_brand_id: "%env(CVU_BRAND_ID)%" country_code: "%env(COUNTRY_CODE)%" + log_db: "%env(INFLUXDB_DB)%" services: # default configuration for services in *this* file @@ -200,7 +201,13 @@ services: #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Bing" App\Service\GISManagerInterface: "@App\\Service\\GISManager\\OpenStreet" #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Google" - + + # influxdb + InfluxDB\Client: + arguments: ['%env(INFLUXDB_HOST)%', '%env(INFLUXDB_PORT)%'] + InfluxDB\Database: + arguments: ['%env(INFLUXDB_DB)%', "@InfluxDB\\Client"] + App\EventListener\JobOrderActiveCacheListener: arguments: $jo_cache: "@App\\Service\\JobOrderCache" @@ -238,12 +245,14 @@ services: event: 'postPersist' entity: 'App\Entity\JobOrder' - App\EventListener\EntityListener\UserEntityListener: + App\EventListener\EntityListener: arguments: - ["@security.token_storage"] + $token_storage: "@security.token_storage" + $log_db: "@InfluxDB\\Database" + $entities: ['App\Entity\User', 'App\Entity\Role', 'App\Entity\Partner'] tags: - - name: 'doctrine.orm.entity_listener' - event: 'postPersist' - entity: 'App\Entity\User' - name: 'doctrine.event_listener' event: 'onFlush' + - name: 'doctrine.event_listener' + event: 'postPersist' + -- 2.43.5 From e3cece21658d16a62222ca7a3db7dd58ddbd4617 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 11 Mar 2020 05:35:25 +0000 Subject: [PATCH 28/28] Add change history for user entity. #330 --- config/acl.yaml | 2 + config/routes/user.yaml | 12 ++- src/Controller/UserController.php | 36 ++++++++- .../{log.html.twig => audit.log.html.twig} | 0 templates/user/change.history.log.html.twig | 79 +++++++++++++++++++ templates/user/form.html.twig | 7 +- 6 files changed, 131 insertions(+), 5 deletions(-) rename templates/user/{log.html.twig => audit.log.html.twig} (100%) create mode 100644 templates/user/change.history.log.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index 1ecbfcdb..83cdedad 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -23,6 +23,8 @@ access_keys: label: User Profile - id: user.logs label: User Logs + - id: user.change.history + label: User Change History - id: role label: Role Access acls: diff --git a/config/routes/user.yaml b/config/routes/user.yaml index 171d4f93..4b0bb93f 100644 --- a/config/routes/user.yaml +++ b/config/routes/user.yaml @@ -49,5 +49,15 @@ user_view_logs_form: user_view_logs: path: /user/{id}/logs - controller: App\Controller\UserController::getLogs + controller: App\Controller\UserController::getAuditLogs + methods: [POST] + +user_view_change_history_form: + path: /users/{id}/change-history + controller: App\Controller\UserController::viewChangeHistoryForm + methods: [GET] + +user_view_change_history: + path: /user/{id}/change-history + controller: App\Controller\UserController::getChangeHistory methods: [POST] diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 8bd441aa..39422e9a 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -497,10 +497,10 @@ class UserController extends Controller $params['id'] = $id; // response - return $this->render('user/log.html.twig', $params); + return $this->render('user/audit.log.html.twig', $params); } - public function getLogs(Client $client, $id) + public function getAuditLogs(Client $client, $id) { // fetch database $log_db = $this->getParameter('log_db'); @@ -513,10 +513,40 @@ class UserController extends Controller // get the points from the resultset, which is an array $points = $result->getPoints(); - // TODO: might need to format time data + return $this->json([ + 'data' => $points, + ]); + } + + /** + * @Menu(selected="user_list") + */ + public function viewChangeHistoryForm($id) + { + $this->denyAccessUnlessGranted('user.change.history', null, 'No access.'); + + $params['id'] = $id; + + // response + return $this->render('user/change.history.log.html.twig', $params); + } + + public function getChangeHistory(Client $client, $id) + { + // fetch database + $log_db = $this->getParameter('log_db'); + $database = $client->selectDB($log_db); + + // query will return a resultset object + $query_string = 'SELECT * FROM entity_log WHERE entity_id = \'' . $id . '\''; + $result = $database->query($query_string); + + // get the points from the resultset, which is an array + $points = $result->getPoints(); return $this->json([ 'data' => $points, ]); } + } diff --git a/templates/user/log.html.twig b/templates/user/audit.log.html.twig similarity index 100% rename from templates/user/log.html.twig rename to templates/user/audit.log.html.twig diff --git a/templates/user/change.history.log.html.twig b/templates/user/change.history.log.html.twig new file mode 100644 index 00000000..cf7ed1dc --- /dev/null +++ b/templates/user/change.history.log.html.twig @@ -0,0 +1,79 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Audit Logs

+
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} + diff --git a/templates/user/form.html.twig b/templates/user/form.html.twig index f6502795..05ea805f 100644 --- a/templates/user/form.html.twig +++ b/templates/user/form.html.twig @@ -57,11 +57,16 @@ Unique alias for this user
-
+
{% if mode == 'update' %} View Audit Logs {% endif %}
+
+ {% if mode == 'update' %} + View Change History + {% endif %} +
-- 2.43.5