From cd5e518113eb4d0e16613e1dd62f25be50211b07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Wed, 13 Nov 2019 11:32:21 +0100
Subject: [PATCH] :ambulance: fix(logging) Fix logging of paste action

Only log successful paste with pasted dn.
Otherwise we get an audit line before the actual paste happens.

issue #6046
---
 include/class_CopyPasteHandler.inc |  3 +--
 include/class_logging.inc          | 19 +++++++------------
 2 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/include/class_CopyPasteHandler.inc b/include/class_CopyPasteHandler.inc
index 5f9988b3f..4911927b8 100644
--- a/include/class_CopyPasteHandler.inc
+++ b/include/class_CopyPasteHandler.inc
@@ -211,7 +211,6 @@ class CopyPasteHandler
       foreach ($this->clean_objects as $key => $entry) {
         $this->current = $entry;
         $errors = $this->current['object']->save();
-        logging::log('copy', 'paste', $this->current['object']->dn, [], reset($errors));
 
         if (empty($errors)) {
           $this->current_saved();
@@ -231,7 +230,6 @@ class CopyPasteHandler
       /* Save current object if edition is finished */
       if (isset($_POST['edit_finish'])) {
         $errors = $this->current['object']->save();
-        logging::log('copy', 'paste', $this->current['object']->dn, [], reset($errors));
 
         if (empty($errors)) {
           $this->current_saved();
@@ -272,6 +270,7 @@ class CopyPasteHandler
   private function current_saved ()
   {
     $this->lastdn   = $this->current['object']->dn;
+    logging::log('copy', 'paste', $this->lastdn);
     $this->handleReferences();
     $this->current  = FALSE;
   }
diff --git a/include/class_logging.inc b/include/class_logging.inc
index dbc06eede..63cce8d05 100644
--- a/include/class_logging.inc
+++ b/include/class_logging.inc
@@ -40,32 +40,27 @@ class logging
   /*!
    * \brief logging method
    *
-   * \param $action         One of these values (modify|create|remove|copy|snapshot|security|error)
+   * \param string $action      One of these values (modify|create|remove|copy|snapshot|security|error)
    *
-   * \param $objecttype     represents the current edited objecttype, like user/user, or the event, like logout
+   * \param string $objecttype  represents the current edited objecttype, like user/user, or the event, like logout
    *
-   * \param $object         represents the current edited object dn, or the target of the operation
+   * \param string $object      represents the current edited object dn, or the target of the operation
    *
-   * \param $changes_array  An array containing names of all touched attributes
+   * \param array $changes      An array containing names of all touched attributes
    *
-   * \param $result         A status message, containing errors or success messages
+   * \param string $result      A status message, containing errors or success messages
    */
-  static function log ($action, $objecttype, $object, array $changes_array = [], $result = '')
+  static function log (string $action, string $objecttype, string $object, array $changes = [], string $result = '')
   {
     global $config, $ui;
 
-    if ($result === NULL) {
-      trigger_error('"Result" parameter should not be NULL');
-      $result = '';
-    }
-
     /* Create data object */
     $entry = [
       'timestamp'   => microtime(TRUE),
       'action'      => $action,
       'objecttype'  => $objecttype,
       'object'      => $object,
-      'changes'     => $changes_array,
+      'changes'     => $changes,
       'result'      => $result
     ];
     if (isset($ui->dn) && !empty($ui->dn)) {
-- 
GitLab