From 1f88638c1eb170dca428340e37afacef1ead15e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be> Date: Wed, 28 Mar 2018 12:50:24 +0200 Subject: [PATCH] :ambulance: fix(simpleplugin) Throw Exceptions when attributes are set to non-compatible types At least useful for webservice issue #5807 --- include/class_exceptions.inc | 7 +++++++ .../attributes/class_BaseSelectorAttribute.inc | 7 +++++++ include/simpleplugin/attributes/class_SetAttribute.inc | 7 +++++++ include/simpleplugin/attributes/class_StringAttribute.inc | 7 +++++++ include/simpleplugin/class_Attribute.inc | 6 ++++++ 5 files changed, 34 insertions(+) diff --git a/include/class_exceptions.inc b/include/class_exceptions.inc index 7bbb356c9..a5553ceec 100644 --- a/include/class_exceptions.inc +++ b/include/class_exceptions.inc @@ -38,6 +38,13 @@ class LDIFImportException extends FusionDirectoryException {} */ class LdapGeneralizedTimeBadFormatException extends FusionDirectoryException {} +/*! \class InvalidValueException + \brief Exception class which can be thrown if an attribute is set to a value with a non-compatible type +*/ +class InvalidValueException extends FusionDirectoryException +{ +} + class NonExistingObjectTypeException extends FusionDirectoryException {} class NonExistingBranchException extends FusionDirectoryException {} class NonExistingLdapNodeException extends FusionDirectoryException {} diff --git a/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc b/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc index 4c4351a5b..e60e4c3c8 100644 --- a/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc +++ b/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc @@ -118,6 +118,13 @@ class BaseSelectorAttribute extends Attribute } } + function checkValue($value) + { + if (!is_string($value) && (!is_object($value) || !method_exists($value, '__toString' ))) { + throw new InvalidValueException(_('Base field value should always be a string')); + } + } + function setValue ($value) { parent::setValue($value); diff --git a/include/simpleplugin/attributes/class_SetAttribute.inc b/include/simpleplugin/attributes/class_SetAttribute.inc index 53679615d..fa092e42b 100644 --- a/include/simpleplugin/attributes/class_SetAttribute.inc +++ b/include/simpleplugin/attributes/class_SetAttribute.inc @@ -339,6 +339,13 @@ class SetAttribute extends Attribute { $this->size = $size; } + + function checkValue($value) + { + if (!is_array($value)) { + throw new InvalidValueException(sprintf(_('SetAttribute "%s" was set to a non-compatible value'), $this->getLabel())); + } + } } class OrderedArrayAttribute extends SetAttribute diff --git a/include/simpleplugin/attributes/class_StringAttribute.inc b/include/simpleplugin/attributes/class_StringAttribute.inc index c9a7ab8c1..5fe3d3f3c 100644 --- a/include/simpleplugin/attributes/class_StringAttribute.inc +++ b/include/simpleplugin/attributes/class_StringAttribute.inc @@ -107,6 +107,13 @@ class StringAttribute extends Attribute return $this->autocomplete; } + function checkValue($value) + { + if (!is_scalar($value) && (!is_object($value) || !method_exists($value, '__toString' ))) { + throw new InvalidValueException(sprintf(_('StringAttribute "%s" was set to a non-compatible value'), $this->getLabel())); + } + } + function setValue ($value) { if ($this->trim) { diff --git a/include/simpleplugin/class_Attribute.inc b/include/simpleplugin/class_Attribute.inc index 283b86011..8228f7e30 100644 --- a/include/simpleplugin/class_Attribute.inc +++ b/include/simpleplugin/class_Attribute.inc @@ -149,8 +149,14 @@ class Attribute return $this->inLdap; } + function checkValue ($value) + { + /* Should throw InvalidValueException if needed */ + } + function setValue ($value) { + $this->checkValue($value); $old_value = $this->value; $this->value = $value; if (($this->submitForm != FALSE) && ($this->submitForm !== TRUE) && ($old_value != $value) && is_object($this->plugin)) { -- GitLab