From eedf1972049dc982f7923db4dd0fe000d84b7d8e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Tue, 9 Jun 2020 11:05:13 +0200
Subject: [PATCH] :sparkles: feat(errors) Use new error objects in more places

issue #6071
---
 .../attributes/class_DateAttribute.inc          | 17 ++++++++++++++---
 .../attributes/class_SelectAttribute.inc        |  5 ++++-
 .../attributes/class_SetAttribute.inc           |  5 ++++-
 .../attributes/class_StringAttribute.inc        |  5 ++++-
 include/simpleplugin/class_Attribute.inc        |  5 ++++-
 include/simpleplugin/class_helpersAttribute.inc |  5 ++++-
 plugins/admin/departments/class_department.inc  |  5 ++++-
 plugins/admin/groups/class_ogroup.inc           |  5 ++++-
 plugins/config/class_configInLdap.inc           | 10 ++++++++--
 .../generic/class_UserPasswordAttribute.inc     |  8 +++++++-
 10 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/include/simpleplugin/attributes/class_DateAttribute.inc b/include/simpleplugin/attributes/class_DateAttribute.inc
index 63ca51d5d..8796696f6 100644
--- a/include/simpleplugin/attributes/class_DateAttribute.inc
+++ b/include/simpleplugin/attributes/class_DateAttribute.inc
@@ -136,16 +136,27 @@ class DateAttribute extends Attribute
       try {
         $dateValue = $this->getDateValue();
         if (($this->minDate !== NULL) && ($dateValue < $this->minDate)) {
-          return sprintf(_('Invalid date in %s, should be newer than: %s'), $this->getLabel(), $this->minDate->format('Y-m-d'));
+          return new SimplePluginCheckError(
+            $this,
+            SimplePluginCheckError::invalidValue(sprintf(_('%s is older than %s'), $dateValue->format('Y-m-d'), $this->minDate->format('Y-m-d')))
+          );
         }
         if (($this->maxDate !== NULL) && ($dateValue > $this->maxDate)) {
-          return sprintf(_('Invalid date in %s, should be older than: %s'), $this->getLabel(), $this->maxDate->format('Y-m-d'));
+          return new SimplePluginCheckError(
+            $this,
+            SimplePluginCheckError::invalidValue(sprintf(_('%s is newer than %s'), $dateValue->format('Y-m-d'), $this->maxDate->format('Y-m-d')))
+          );
         }
       } catch (Exception $e) {
         if ($this->isTemplate() && preg_match('/%/', $this->value)) {
           return '';
         } else {
-          return sprintf(_('Error, incorrect date: %s'), $e->getMessage());
+          return new SimplePluginCheckError(
+            $this,
+            SimplePluginCheckError::invalidValue(sprintf(_('Incorrect date: %s'), $e->getMessage())),
+            0,
+            $e
+          );
         }
       }
       return '';
diff --git a/include/simpleplugin/attributes/class_SelectAttribute.inc b/include/simpleplugin/attributes/class_SelectAttribute.inc
index 3accc9082..4551557cd 100644
--- a/include/simpleplugin/attributes/class_SelectAttribute.inc
+++ b/include/simpleplugin/attributes/class_SelectAttribute.inc
@@ -158,7 +158,10 @@ class SelectAttribute extends Attribute
       return $error;
     } else {
       if (!$this->disabled && !in_array($this->value, $this->choices)) {
-        return sprintf(_('The value "%s" for field "%s" is not in the list of possible choices'), $this->value, $this->getLabel());
+        return new SimplePluginCheckError(
+          $this,
+          SimplePluginCheckError::invalidValue(sprintf(_('"%s" is not in the list of possible choices'), $this->value))
+        );
       }
     }
   }
diff --git a/include/simpleplugin/attributes/class_SetAttribute.inc b/include/simpleplugin/attributes/class_SetAttribute.inc
index d79d36654..d35183252 100644
--- a/include/simpleplugin/attributes/class_SetAttribute.inc
+++ b/include/simpleplugin/attributes/class_SetAttribute.inc
@@ -147,7 +147,10 @@ class SetAttribute extends Attribute
       return $error;
     } else {
       if (!is_array($this->value)) {
-        return sprintf(_('The value for multivaluated field "%s" is not an array'), $this->getLabel());
+        return new SimplePluginCheckError(
+          $this,
+          SimplePluginCheckError::invalidValue(_('Value is not an array'))
+        );
       }
       foreach ($this->value as $value) {
         $this->attribute->setValue($value);
diff --git a/include/simpleplugin/attributes/class_StringAttribute.inc b/include/simpleplugin/attributes/class_StringAttribute.inc
index 857710e30..74a73ee93 100644
--- a/include/simpleplugin/attributes/class_StringAttribute.inc
+++ b/include/simpleplugin/attributes/class_StringAttribute.inc
@@ -133,7 +133,10 @@ class StringAttribute extends Attribute
   function validate ()
   {
     if (($this->pattern !== '') && !preg_match($this->pattern, $this->value)) {
-      return msgPool::invalid($this->getLabel(), $this->value, $this->example);
+      return new SimplePluginCheckError(
+        $this,
+        SimplePluginCheckError::invalidValue(sprintf('"%s"', $this->getValue()))
+      );
     }
   }
 
diff --git a/include/simpleplugin/class_Attribute.inc b/include/simpleplugin/class_Attribute.inc
index 747f7becc..ebf5815a6 100644
--- a/include/simpleplugin/class_Attribute.inc
+++ b/include/simpleplugin/class_Attribute.inc
@@ -586,7 +586,10 @@ class Attribute
             continue;
           }
 
-          return msgPool::duplicated($this->getLabel(), $attrs['dn']);
+          return new SimplePluginCheckError(
+            $this,
+            htmlescape(msgPool::duplicated($this->getLabel(), $attrs['dn']))
+          );
         }
       }
     }
diff --git a/include/simpleplugin/class_helpersAttribute.inc b/include/simpleplugin/class_helpersAttribute.inc
index 28829a54c..523e7ed55 100644
--- a/include/simpleplugin/class_helpersAttribute.inc
+++ b/include/simpleplugin/class_helpersAttribute.inc
@@ -30,7 +30,10 @@ class TestValidateAttribute extends StringAttribute
   {
     $func = $this->testFunc;
     if (!tests::$func($this->value)) {
-      return msgPool::invalid($this->getLabel(), $this->value, $this->example);
+      return new SimplePluginCheckError(
+        $this,
+        SimplePluginCheckError::invalidValue(sprintf('"%s"', $this->value))
+      );
     }
   }
 }
diff --git a/plugins/admin/departments/class_department.inc b/plugins/admin/departments/class_department.inc
index 1fe769aa5..5ab4a8815 100644
--- a/plugins/admin/departments/class_department.inc
+++ b/plugins/admin/departments/class_department.inc
@@ -167,7 +167,10 @@ class department extends simplePlugin
     $namingAttr = static::$namingAttr;
 
     if (($namingAttr == 'ou') && tests::is_department_name_reserved($this->$namingAttr)) {
-      $message[] = msgPool::reserved(_('Name'));
+      $message[] = new SimplePluginCheckError(
+        $this,
+        htmlescape(msgPool::reserved(_('Name')))
+      );
     }
 
     return $message;
diff --git a/plugins/admin/groups/class_ogroup.inc b/plugins/admin/groups/class_ogroup.inc
index 80afd7bdc..094598b3b 100644
--- a/plugins/admin/groups/class_ogroup.inc
+++ b/plugins/admin/groups/class_ogroup.inc
@@ -248,7 +248,10 @@ class ogroup extends simplePlugin
     $message = parent::check();
     $this->reload();
     if (preg_match('/W/', $this->gosaGroupObjects) && preg_match('/T/', $this->gosaGroupObjects)) {
-      $message[] = _('Putting both workstations and terminals in the same group is not allowed');
+      $message[] = new SimplePluginCheckError(
+        $this,
+        htmlescape(_('Putting both workstations and terminals in the same group is not allowed'))
+      );
     }
 
     return $message;
diff --git a/plugins/config/class_configInLdap.inc b/plugins/config/class_configInLdap.inc
index c05a5aa03..bbb942bce 100644
--- a/plugins/config/class_configInLdap.inc
+++ b/plugins/config/class_configInLdap.inc
@@ -536,10 +536,16 @@ class configInLdap extends simplePlugin
   {
     $messages = parent::check();
     if (($this->fdPasswordDefaultHash == 'sasl') && ($this->fdSaslRealm == '') && ($this->fdSaslExop == '')) {
-      $messages[] = _('You need to fill saslRealm or saslExop in the configuration screen in order to use SASL');
+      $messages[] = new SimplePluginCheckError(
+        $this,
+        htmlescape(_('You need to fill saslRealm or saslExop in the configuration screen in order to use SASL'))
+      );
     }
     if ($this->attributesAccess['fdLanguage']->hasChanged() && ($this->fdLanguage != '') && !Language::isAvailable($this->fdLanguage)) {
-      $messages[] = sprintf(_('It seems the selected language "%s" is not installed on the system. Please install it or select an other one.'), $this->fdLanguage);
+      $messages[] = new SimplePluginCheckError(
+        $this,
+        htmlescape(sprintf(_('It seems the selected language "%s" is not installed on the system. Please install it or select an other one.'), $this->fdLanguage))
+      );
     }
     return $messages;
   }
diff --git a/plugins/personal/generic/class_UserPasswordAttribute.inc b/plugins/personal/generic/class_UserPasswordAttribute.inc
index bdcb7a957..2373a5638 100644
--- a/plugins/personal/generic/class_UserPasswordAttribute.inc
+++ b/plugins/personal/generic/class_UserPasswordAttribute.inc
@@ -237,7 +237,13 @@ class UserPasswordAttribute extends CompositeAttribute
       return $error;
     }
     if (($this->attributes[1]->getValue() != '') || ($this->attributes[2]->getValue() != '')) {
-      return user::reportPasswordProblems($this->plugin->dn, $this->attributes[1]->getValue(), $this->attributes[2]->getValue());
+      $error = user::reportPasswordProblems($this->plugin->dn, $this->attributes[1]->getValue(), $this->attributes[2]->getValue());
+      if ($error !== FALSE) {
+        return new SimplePluginCheckError(
+          $this,
+          htmlescape($error)
+        );
+      }
     }
   }
 
-- 
GitLab