From 6c8b98c677dde7d0e9094ae5e576f1d814a53cd9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Wed, 29 Aug 2018 10:02:47 +0200
Subject: [PATCH] :sparkles: feat(management) Search in nameAttr and mainAttr

Still needs to be added: ACL check to avoid crashes, and
 plSearchAttribute key in plInfo for tabs to add fields to the search.

issue #5867
---
 include/class_objects.inc                     | 22 ++++++++++++++-----
 include/management/class_managementFilter.inc | 21 +++++++++---------
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/include/class_objects.inc b/include/class_objects.inc
index c0fae81ef..48fea29e1 100644
--- a/include/class_objects.inc
+++ b/include/class_objects.inc
@@ -352,7 +352,7 @@ class objects
     return static::open('new', $type);
   }
 
-  static function infos ($type)
+  static function &infos ($type)
   {
     global $config;
 
@@ -395,11 +395,7 @@ class objects
   {
     global $config;
 
-    if (!isset($config->data['OBJECTS'][strtoupper($type)])) {
-      throw new NonExistingObjectTypeException('Non-existing type "'.$type.'"');
-    }
-
-    $infos =& $config->data['OBJECTS'][strtoupper($type)];
+    $infos =& static::infos($type);
 
     if (!isset($infos['filterObject'])) {
       $infos['filterObject'] = ldapFilter::parse($infos['filter']);
@@ -407,6 +403,20 @@ class objects
     return $infos['filterObject'];
   }
 
+  /* This method allows to cache searched attributes list in objectTypes */
+  static function getSearchedAttributes ($type)
+  {
+    global $config;
+
+    $infos =& static::infos($type);
+
+    if (!isset($infos['searchAttributes'])) {
+      $infos['searchAttributes'] = array_unique(array($infos['mainAttr'], $infos['nameAttr']));
+    }
+
+    return $infos['searchAttributes'];
+  }
+
   static function types ()
   {
     global $config;
diff --git a/include/management/class_managementFilter.inc b/include/management/class_managementFilter.inc
index 279b5fddc..25bb7a5c5 100644
--- a/include/management/class_managementFilter.inc
+++ b/include/management/class_managementFilter.inc
@@ -25,7 +25,7 @@ class managementFilter
 {
   protected $types;
 
-  protected $searchAttributes = array('cn', 'description');
+  protected $searchAttributes = array();
   protected $scope = 'one';
   protected $showTemplates = FALSE;
 
@@ -61,6 +61,7 @@ class managementFilter
         'filters' => array(),
         'infos'   => objects::infos($type),
       );
+      $this->searchAttributes = array_unique(array_merge($this->searchAttributes, objects::getSearchedAttributes($type)));
     }
 
     $this->filterElements = array(
@@ -132,15 +133,6 @@ class managementFilter
   {
     global $ui;
 
-    $elementFilters = array();
-    if (!empty($this->search)) {
-      if (preg_match('/^\(.+\)$/', $this->search)) {
-        $elementFilters[] = $this->search;
-      } else {
-        $elementFilters[] = '(|('.implode('=*'.$this->search.'*)(', $this->searchAttributes).'=*'.$this->search.'*))';
-      }
-    }
-
     $objectTypeCount  = array();
     $entries          = array();
     $row              = 0;
@@ -171,6 +163,15 @@ class managementFilter
         }
       }
 
+      $elementFilters = array();
+      if (!empty($this->search)) {
+        if (preg_match('/^\(.+\)$/', $this->search)) {
+          $elementFilters[] = $this->search;
+        } else {
+          $elementFilters[] = '(|('.implode('=*'.$this->search.'*)(', objects::getSearchedAttributes($type)).'=*'.$this->search.'*))';
+        }
+      }
+
       $typeElementFilters = $elementFilters;
       foreach ($this->filterElements as $element) {
         $skip = $element->getFilters($type, $typeElementFilters);
-- 
GitLab