From 7e1714a0d7f0efe1483a0ad0ab583f3c44453d86 Mon Sep 17 00:00:00 2001 From: Alex Rothuis Date: Thu, 28 Jul 2016 14:14:24 +0200 Subject: [PATCH] Add explicit boolean conversion for doctrine types We do not want any random type to be converted to boolean. For PostgreSQL, Doctrine has platform specific options. For other platforms, it relies on PHPs default (and lenient) type conversion. --- .../Type/ShowRaaContactInformationOptionType.php | 11 ++++++++++- .../Doctrine/Type/UseRaLocationsOptionType.php | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/ShowRaaContactInformationOptionType.php b/src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/ShowRaaContactInformationOptionType.php index e58930b26..ad3f2b873 100644 --- a/src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/ShowRaaContactInformationOptionType.php +++ b/src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/ShowRaaContactInformationOptionType.php @@ -61,8 +61,17 @@ public function convertToPHPValue($value, AbstractPlatform $platform) return $value; } + // Prevent a too lenient boolean conversion on non-postgresql platforms + if ($platform->getName() !== 'postgresql') { + if ($value !== 1 && $value !== 0 && $value !== '1' && $value !== '0' && !is_bool($value)) { + throw ConversionException::conversionFailed($value, $this->getName()); + } + } + try { - $showRaaContactInformationOption = new ShowRaaContactInformationOption($value); + $showRaaContactInformationOption = new ShowRaaContactInformationOption( + $platform->convertFromBoolean($value) + ); } catch (InvalidArgumentException $e) { // get nice standard message, so we can throw it keeping the exception chain $doctrineExceptionMessage = ConversionException::conversionFailedFormat( diff --git a/src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/UseRaLocationsOptionType.php b/src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/UseRaLocationsOptionType.php index 5d591365f..b26e7fd99 100644 --- a/src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/UseRaLocationsOptionType.php +++ b/src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/UseRaLocationsOptionType.php @@ -61,8 +61,15 @@ public function convertToPHPValue($value, AbstractPlatform $platform) return $value; } + // Prevent a too lenient boolean conversion on non-postgresql platforms + if ($platform->getName() !== 'postgresql') { + if ($value !== 1 && $value !== 0 && $value !== '1' && $value !== '0' && !is_bool($value)) { + throw ConversionException::conversionFailed($value, $this->getName()); + } + } + try { - $useRaLocationsOption = new UseRaLocationsOption($value); + $useRaLocationsOption = new UseRaLocationsOption($platform->convertFromBoolean($value)); } catch (InvalidArgumentException $e) { // get nice standard message, so we can throw it keeping the exception chain $doctrineExceptionMessage = ConversionException::conversionFailedFormat(