From b391782363552e195e1e84c2cba7a8688f12965f 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 ac67ff135..16eeea5ad 100644 --- a/include/class_exceptions.inc +++ b/include/class_exceptions.inc @@ -44,6 +44,13 @@ 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 { } diff --git a/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc b/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc index 0e0736c15..9d0b9000c 100644 --- a/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc +++ b/include/simpleplugin/attributes/class_BaseSelectorAttribute.inc @@ -120,6 +120,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 b0a546d3f..7871fcf64 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())); + } + } } /*! diff --git a/include/simpleplugin/attributes/class_StringAttribute.inc b/include/simpleplugin/attributes/class_StringAttribute.inc index a283089ac..97471ab52 100644 --- a/include/simpleplugin/attributes/class_StringAttribute.inc +++ b/include/simpleplugin/attributes/class_StringAttribute.inc @@ -108,6 +108,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 0e53aef74..65b247891 100644 --- a/include/simpleplugin/class_Attribute.inc +++ b/include/simpleplugin/class_Attribute.inc @@ -154,8 +154,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