From 6c877f272d8c5edc9cd5ed735cde176ccd55777f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Wed, 25 Jan 2017 13:27:02 +0100
Subject: [PATCH] Fixes #5357 Do not remove tabs when applying templates to
 existing objects

---
 include/class_baseSelector.inc              | 3 +--
 include/class_template.inc                  | 5 +++++
 include/simpleplugin/class_simplePlugin.inc | 8 +++++++-
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/include/class_baseSelector.inc b/include/class_baseSelector.inc
index 6f0ad4d75..c88485e6c 100644
--- a/include/class_baseSelector.inc
+++ b/include/class_baseSelector.inc
@@ -142,7 +142,7 @@ class baseSelector {
   {
     global $config;
 
-    if (!isset($this->base) || ($this->base == "")) {
+    if (!isset($this->base) || ($this->base == '')) {
       $this->lastState = FALSE;
       return FALSE;
     }
@@ -179,7 +179,6 @@ class baseSelector {
           }
         }
       }
-
     }
 
     /* Skip if there's no change */
diff --git a/include/class_template.inc b/include/class_template.inc
index 5299a4675..e780b2c21 100644
--- a/include/class_template.inc
+++ b/include/class_template.inc
@@ -194,6 +194,11 @@ class template
   {
     if ($targetdn !== NULL) {
       $this->tabObject = objects::open($targetdn, $this->type);
+      foreach ($this->tabObject->by_object as $class => &$plugin) {
+        if ($plugin->is_account || $plugin->ignore_account) {
+          $this->attrs['objectClass'] = $plugin->mergeObjectClasses($this->attrs['objectClass']);
+        }
+      }
     } elseif ($this->applied) {
       trigger_error('Templates can’t be applied twice without calling reset before');
       return;
diff --git a/include/simpleplugin/class_simplePlugin.inc b/include/simpleplugin/class_simplePlugin.inc
index e64dec188..4d65e7b80 100644
--- a/include/simpleplugin/class_simplePlugin.inc
+++ b/include/simpleplugin/class_simplePlugin.inc
@@ -1197,6 +1197,12 @@ class simplePlugin
     return !empty($this->attrs);
   }
 
+  /* Used by prepare_save and template::apply */
+  public function mergeObjectClasses(array $oc)
+  {
+    return array_merge_unique($oc, $this->objectclasses);
+  }
+
   protected function prepare_save ()
   {
     global $config;
@@ -1229,7 +1235,7 @@ class simplePlugin
       }
     }
 
-    $this->attrs['objectClass'] = array_merge_unique($oc, $this->objectclasses);
+    $this->attrs['objectClass'] = $this->mergeObjectClasses($oc);
 
     /* Fill attributes LDAP values into the attrs array */
     foreach ($this->attributesInfo as $section => $sectionInfo) {
-- 
GitLab