diff --git a/include/simpleplugin/class_simplePlugin.inc b/include/simpleplugin/class_simplePlugin.inc index 3292dd7cffc55cc9d522fcbac9470a03b5ebfbe6..ce0481daf6c3f6becaa1fb4db49ee2236a594a3a 100644 --- a/include/simpleplugin/class_simplePlugin.inc +++ b/include/simpleplugin/class_simplePlugin.inc @@ -94,7 +94,9 @@ class simplePlugin implements SimpleTab protected $objectclasses = []; /*! \brief The state of the attributes when we opened the object */ - protected $saved_attributes = []; + protected $saved_attributes = []; // Note : This is overwritten during post_save logic + // Requiring therefore a save to threat this during logging mechanism. + protected $beforeLdapChangeAttributes = []; /*! \brief Do we want a header allowing to able/disable this plugin */ protected $displayHeader = FALSE; @@ -1201,6 +1203,9 @@ class simplePlugin implements SimpleTab { /* Prepare saved attributes */ $this->saved_attributes = $this->attrs; + // Fill for differenciation in the post save as saved_attributes will be modified. + $this->beforeLdapChangeAttributes = $this->saved_attributes; + foreach (array_keys($this->saved_attributes) as $index) { if (is_numeric($index)) { unset($this->saved_attributes[$index]); @@ -1466,12 +1471,15 @@ class simplePlugin implements SimpleTab if ($this->initially_was_account) { $errors = $this->handle_post_events('modify', ['modifiedLdapAttrs' => array_keys($this->attrs)]); + $modifiedAttrs = $this->getModifiedAttributesValues(); // We log values of attributes as well if modification occur in order for notification to be aware of the change. (Json allows array to string conversion). - logging::log('modify', 'plugin/' . get_class($this), $this->dn, [json_encode($this->attrs)], $this->ldap_error); + logging::log('modify', 'plugin/' . get_class($this), $this->dn, [json_encode($modifiedAttrs)], $this->ldap_error); + } else { $errors = $this->handle_post_events('add', ['modifiedLdapAttrs' => array_keys($this->attrs)]); logging::log('create', 'plugin/' . get_class($this), $this->dn, array_keys($this->attrs), $this->ldap_error); } + if (!empty($errors)) { msg_dialog::displayChecks($errors); } @@ -1501,6 +1509,40 @@ class simplePlugin implements SimpleTab return $result; } + private function getModifiedAttributesValues (): array + { + // Initialize result array + $result = []; + + // Find common keys between old attributes and modified attributes. + $commonKeys = array_intersect_key($this->attrs, $this->beforeLdapChangeAttributes); + + // Iterate over each common key + foreach ($commonKeys as $key => $value) { + // Check if the new value differs from the old value + if ($this->attrs[$key] !== $this->beforeLdapChangeAttributes[$key]) { + $newValues = $this->attrs[$key]; + $oldValues = $this->beforeLdapChangeAttributes[$key]; + + // Ensure both new and old values are arrays for comparison + if (is_array($newValues) && is_array($oldValues)) { + // Find the new values that are not present in the old values + $diffValues = array_diff($newValues, $oldValues); + + // Store only the new values that are different + if (!empty($diffValues)) { + $result[$key] = $diffValues; + } + } else { + // If values are scalar (non-array), store the new value directly if it differs + $result[$key] = $newValues; + } + } + } + + return $result; + } + /*! \brief Forward command execution requests * to the pre/post hook execution method. *