diff --git a/CHANGELOG.md b/CHANGELOG.md index 770c85f..3971419 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Add Transifex translations link to README +### Fixed + +- Fix `Clear` checkbox not erasing the stored SCCM database password and inventory credentials + ## [2.7.0] - 2026-10-06 ### Added diff --git a/inc/config.class.php b/inc/config.class.php index 6622c5f..9d2dda2 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -142,16 +142,15 @@ public function prepareInputForAdd($input): array public function prepareInputForUpdate($input): array { - if (isset($input["sccmdb_password"]) && !empty($input["sccmdb_password"])) { - $input["sccmdb_password"] = (new GLPIKey())->encrypt($input["sccmdb_password"]); - } else { - unset($input["sccmdb_password"]); - } - - if (isset($input["auth_info"]) && !empty($input["auth_info"])) { - $input["auth_info"] = (new GLPIKey())->encrypt($input["auth_info"]); - } else { - unset($input["auth_info"]); + foreach (['sccmdb_password', 'auth_info'] as $field) { + // "Clear" checkbox rendered by GLPI password fields + if (!empty($input['_blank_' . $field])) { + $input[$field] = ''; + } elseif (!empty($input[$field])) { + $input[$field] = (new GLPIKey())->encrypt($input[$field]); + } else { + unset($input[$field]); + } } if (array_key_exists('inventory_server_url', $input) && !empty($input['inventory_server_url'])) { diff --git a/tests/PluginSccmConfigTest.php b/tests/PluginSccmConfigTest.php new file mode 100644 index 0000000..e531b1b --- /dev/null +++ b/tests/PluginSccmConfigTest.php @@ -0,0 +1,79 @@ +. + * ------------------------------------------------------------------------- + * @author François Legastelois + * @copyright Copyright (C) 2014-2023 by SCCM plugin team. + * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/pluginsGLPI/sccm + * ------------------------------------------------------------------------- + */ + +use Glpi\Tests\DbTestCase; +use PHPUnit\Framework\Attributes\DataProvider; + +class PluginSccmConfigTest extends DbTestCase +{ + public static function secretFieldProvider(): iterable + { + yield 'database password' => ['sccmdb_password']; + yield 'inventory credentials' => ['auth_info']; + } + + #[DataProvider('secretFieldProvider')] + public function testSecretIsEncrypted(string $field): void + { + $config = $this->createItem(PluginSccmConfig::class, ['name' => 'test', $field => 'secret'], [$field]); + + $this->assertNotSame('secret', $config->fields[$field]); + $this->assertSame('secret', (new GLPIKey())->decrypt($config->fields[$field])); + } + + #[DataProvider('secretFieldProvider')] + public function testEmptySecretKeepsStoredValue(string $field): void + { + $config = $this->createItem(PluginSccmConfig::class, ['name' => 'test', $field => 'secret'], [$field]); + + $this->updateItem(PluginSccmConfig::class, $config->getID(), [$field => ''], [$field]); + + $this->assertTrue($config->getFromDB($config->getID())); + $this->assertSame('secret', (new GLPIKey())->decrypt($config->fields[$field])); + } + + #[DataProvider('secretFieldProvider')] + public function testClearCheckboxErasesSecret(string $field): void + { + $config = $this->createItem(PluginSccmConfig::class, ['name' => 'test', $field => 'secret'], [$field]); + + // The form still posts the displayed value along with the checkbox + $this->updateItem( + PluginSccmConfig::class, + $config->getID(), + [$field => 'secret', '_blank_' . $field => '1'], + [$field, '_blank_' . $field], + ); + + $this->assertTrue($config->getFromDB($config->getID())); + $this->assertEmpty($config->fields[$field]); + } +}