From 7af41ff1a742142ede4ff674225b591a06c98be1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Thu, 6 Jun 2019 16:21:26 +0200
Subject: [PATCH] :sparkles: feat(hooks) Make special password attrs available
 to hooks on any user tab

issue #5977
---
 include/simpleplugin/class_simplePlugin.inc  | 23 ++++++++++++--------
 include/simpleplugin/interface_SimpleTab.inc |  7 ++++++
 plugins/personal/generic/class_user.inc      |  4 ++--
 3 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/include/simpleplugin/class_simplePlugin.inc b/include/simpleplugin/class_simplePlugin.inc
index 26d8c50f6..c005a8431 100644
--- a/include/simpleplugin/class_simplePlugin.inc
+++ b/include/simpleplugin/class_simplePlugin.inc
@@ -1422,6 +1422,16 @@ class simplePlugin implements SimpleTab
     return $this->handle_hooks('PRE', $mode, $addAttrs);
   }
 
+  function fillHookAttrs (array &$addAttrs)
+  {
+    // Walk trough attributes list and add the plugins attributes.
+    foreach ($this->attributes as $attr) {
+      if (!isset($addAttrs[$attr])) {
+        $addAttrs[$attr] = $this->$attr;
+      }
+    }
+  }
+
   /*!
    * \brief    Calls external hooks which are defined for this plugin (fusiondirectory.conf)
    *           Replaces placeholder by class values of this plugin instance.
@@ -1438,10 +1448,7 @@ class simplePlugin implements SimpleTab
     $messages = [];
 
     foreach ($commands as $command) {
-      // Walk trough attributes list and add the plugins attributes.
-      foreach ($this->attributes as $attr) {
-        $addAttrs[$attr] = $this->$attr;
-      }
+      $this->fillHookAttrs($addAttrs);
 
       $ui = get_userinfo();
 
@@ -1455,11 +1462,9 @@ class simplePlugin implements SimpleTab
       $addAttrs['location']   = $config->current['NAME'];
 
       if (isset($this->parent->by_object)) {
-        foreach ($this->parent->by_object as $object) {
-          foreach ($object->attributes as $attr) {
-            if (!isset($addAttrs[$attr])) {
-              $addAttrs[$attr] = $object->$attr;
-            }
+        foreach ($this->parent->by_object as $class => $object) {
+          if ($class != get_class($this)) {
+            $object->fillHookAttrs($addAttrs);
           }
         }
       }
diff --git a/include/simpleplugin/interface_SimpleTab.inc b/include/simpleplugin/interface_SimpleTab.inc
index bf3e36299..7a6070f7c 100644
--- a/include/simpleplugin/interface_SimpleTab.inc
+++ b/include/simpleplugin/interface_SimpleTab.inc
@@ -142,4 +142,11 @@ interface SimpleTab
    *  Used by prepare_save and template::apply
    */
   public function mergeObjectClasses (array $oc): array;
+
+  /*!
+   * \brief Fill attributes which may be used in hooks
+   *
+   *  Used by simplePlugin::callHook
+   */
+  public function fillHookAttrs (array &$addAttrs);
 }
diff --git a/plugins/personal/generic/class_user.inc b/plugins/personal/generic/class_user.inc
index 4e20f2bcf..0332c6937 100644
--- a/plugins/personal/generic/class_user.inc
+++ b/plugins/personal/generic/class_user.inc
@@ -389,12 +389,12 @@ class user extends simplePlugin
     }
   }
 
-  function callHook ($cmd, array $addAttrs = [], &$returnOutput = [], &$returnCode = NULL): array
+  function fillHookAttrs (array &$addAttrs)
   {
+    parent::fillHookAttrs($addAttrs);
     $addAttrs['passwordMethod'] = $this->attributesAccess['userPassword']->getMethod();
     $addAttrs['userLocked']     = (int)($this->attributesAccess['userPassword']->isLocked());
     $addAttrs['passwordClear']  = $this->attributesAccess['userPassword']->getClear();
-    return parent::callHook($cmd, $addAttrs, $returnOutput, $returnCode);
   }
 
   static function reportPasswordProblems ($user, $new_password, $repeated_password, $current_password = NULL)
-- 
GitLab